@Test public void testWriteStoreRequest() throws IOException { final String base64EncodedData = "AAAAAgAgMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAAIDkwMDA" + "wMDAwMDAwMDAwMDAwMDAwMDA4MDAwMDAwMDA1AAAAXgAAAAIAIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA" + "wMDAwACA5MDAwMDAwMDAwMDAwMDAwMDAwMDAwODAwMDAwMDAwNQAAAAEAA2tleQAAAAV2YWx1ZRf0zcZkYXRhZGF" + "0YQ=="; final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000"); final RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005"); HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest( ArtifactInfo.builder() .addRuleKeys(ruleKey, ruleKey2) .setMetadata(ImmutableMap.of("key", "value")) .build(), new ByteSource() { @Override public InputStream openStream() throws IOException { return new ByteArrayInputStream("datadata".getBytes(Charsets.UTF_8)); } }); assertThat(storeRequest.getContentLength(), Matchers.is(178L)); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); StoreWriteResult writeResult = storeRequest.write(byteArrayOutputStream); assertThat( writeResult.getArtifactContentHashCode(), Matchers.equalTo(HashCode.fromString("2c0b14a4"))); assertThat(writeResult.getArtifactSizeBytes(), Matchers.is(8L)); byte[] expectedBytes = BaseEncoding.base64().decode(base64EncodedData); assertThat(byteArrayOutputStream.toByteArray(), Matchers.equalTo(expectedBytes)); }
@Test public void testStoreRequest() throws IOException { final RuleKey ruleKey = new RuleKey("00000000010000000000008000000000"); final RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005"); final String data = "data"; ImmutableMap<String, String> metadata = ImmutableMap.of("metaKey", "metaValue"); HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest( ArtifactInfo.builder().addRuleKeys(ruleKey, ruleKey2).setMetadata(metadata).build(), new ByteSource() { @Override public InputStream openStream() throws IOException { return new ByteArrayInputStream(data.getBytes(Charsets.UTF_8)); } }); ByteArrayOutputStream storeRequestOutputStream = new ByteArrayOutputStream(); storeRequest.write(storeRequestOutputStream); ByteArrayOutputStream storeRequestPayloadStream = new ByteArrayOutputStream(); StoreResponseReadResult readStoreRequest = HttpArtifactCacheBinaryProtocol.readStoreRequest( new DataInputStream(new ByteArrayInputStream(storeRequestOutputStream.toByteArray())), storeRequestPayloadStream); assertThat(readStoreRequest.getRuleKeys(), Matchers.containsInAnyOrder(ruleKey, ruleKey2)); assertThat(readStoreRequest.getMetadata(), Matchers.equalTo(metadata)); assertThat( storeRequestPayloadStream.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8))); }