コード例 #1
0
  @Test
  public void testFetchResponse() throws IOException {
    RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005");
    final String data = "data";
    ImmutableMap<String, String> metadata = ImmutableMap.of("metaKey", "metaValue");

    HttpArtifactCacheBinaryProtocol.FetchResponse fetchResponse =
        new HttpArtifactCacheBinaryProtocol.FetchResponse(
            ImmutableSet.of(ruleKey, ruleKey2),
            metadata,
            new ByteSource() {
              @Override
              public InputStream openStream() throws IOException {
                return new ByteArrayInputStream(data.getBytes(Charsets.UTF_8));
              }
            });
    assertThat(fetchResponse.getContentLength(), Matchers.is(110L));

    ByteArrayOutputStream fetchResponseOutputStream = new ByteArrayOutputStream();
    fetchResponse.write(fetchResponseOutputStream);

    ByteArrayInputStream fetchResponseInputStream =
        new ByteArrayInputStream(fetchResponseOutputStream.toByteArray());
    ByteArrayOutputStream fetchResponsePayload = new ByteArrayOutputStream();
    FetchResponseReadResult responseReadResult =
        HttpArtifactCacheBinaryProtocol.readFetchResponse(
            new DataInputStream(fetchResponseInputStream), fetchResponsePayload);

    assertThat(responseReadResult.getRuleKeys(), Matchers.containsInAnyOrder(ruleKey, ruleKey2));
    assertThat(responseReadResult.getMetadata(), Matchers.equalTo(metadata));
    assertThat(fetchResponsePayload.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8)));
  }
コード例 #2
0
  @Test
  public void testReadFetchResponse() throws IOException {
    final String base64EncodedData =
        "AAAALgAAAAEAIDAwMDAwMDAwMDEwMDAwMDAwMDAwMDA4MDAwMDAwMDAwAAAAANcwdr5kYXRh";
    final RuleKey ruleKey = new RuleKey("00000000010000000000008000000000");
    final String data = "data";

    byte[] expectedData;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(out)) {
      byte[] metadata =
          HttpArtifactCacheBinaryProtocol.createMetadataHeader(
              ImmutableSet.of(ruleKey),
              ImmutableMap.<String, String>of(),
              ByteSource.wrap(data.getBytes(Charsets.UTF_8)));
      dataOut.writeInt(metadata.length);
      dataOut.write(metadata);
      dataOut.write(data.getBytes(Charsets.UTF_8));
      expectedData = out.toByteArray();
    }
    assertThat(expectedData, Matchers.equalTo(BaseEncoding.base64().decode(base64EncodedData)));

    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(expectedData))) {
      FetchResponseReadResult result =
          HttpArtifactCacheBinaryProtocol.readFetchResponse(inputStream, outputStream);
      assertThat(result.getRuleKeys(), Matchers.contains(ruleKey));
      assertThat(outputStream.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8)));
      assertThat(result.getActualHashCode(), Matchers.equalTo(HashCode.fromString("d73076be")));
      assertThat(result.getExpectedHashCode(), Matchers.equalTo(HashCode.fromString("d73076be")));
      assertThat(result.getMetadata(), Matchers.anEmptyMap());
      assertThat(result.getResponseSizeBytes(), Matchers.equalTo(4L));
    }
  }