S3LocationResolver implements LocationResolver { @Override public URI resolveLocation(String location, boolean create) { List<String> elements = new ArrayList<>(); if (StringUtils.isNotBlank(prefix)) { elements.add(prefix); } elements.add(location); String path = elements.stream().collect(joining("/")); if (create) { ByteArrayInputStream content = new ByteArrayInputStream(new byte[0]); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); s3.putObject(bucket, path + "/", content, metadata); } try { return new URI("s3", bucket, "/" + path, null); } catch (URISyntaxException e) { throw new MetaStoreException( String.format("Could not construct location URI for: bucket='%s', prefix='%s', location='%s'", bucket, prefix, location), e); } }  @Override URI resolveLocation(String location, boolean create);  }
@Test public void typical() throws URISyntaxException { S3LocationResolver resolver = new S3LocationResolver(s3, "bucket", "prefix"); URI uri = resolver.resolveLocation("tableName", false); assertThat(uri, is(new URI("s3: }
@Test public void noPrefix() throws URISyntaxException { S3LocationResolver resolver = new S3LocationResolver(s3, "bucket", null); URI uri = resolver.resolveLocation("tableName", false); assertThat(uri, is(new URI("s3: }
@Test public void create() throws URISyntaxException { S3LocationResolver resolver = new S3LocationResolver(s3, "bucket", "prefix"); resolver.resolveLocation("tableName", true); ArgumentCaptor<ByteArrayInputStream> contentCaptor = ArgumentCaptor.forClass(ByteArrayInputStream.class); ArgumentCaptor<ObjectMetadata> metadataCaptor = ArgumentCaptor.forClass(ObjectMetadata.class); verify(s3).putObject(eq("bucket"), eq("prefix/tableName/"), contentCaptor.capture(), metadataCaptor.capture()); assertThat(contentCaptor.getValue().available(), is(0)); assertThat(metadataCaptor.getValue().getContentLength(), is(0L)); }