@Override public URI getUri() { URIBuilder builder = new URIBuilder(dataset.getUri()); for (Map.Entry<String, String> entry : constraints.toQueryMap().entrySet()) { builder.with(entry.getKey(), entry.getValue()); } return builder.build(); }
@Test public void testRepoUriHasQueryString() { URI uri = new URIBuilder("repo:file:/tmp/data?repoParam=x", "ns", "test-ds") .with("username", "bob") .build(); Assert.assertEquals(URI.create("view:file:/tmp/data/ns/test-ds?repoParam=x&username=bob"), uri); } }
@Test public void testDatasetUriAddEquals() { Assert.assertEquals("Should produce an equivalent dataset URI", URI.create("view:file:/datasets/test-name?prop=value"), new URIBuilder("dataset:file:/datasets/test-name") .with("prop", "value") .build()); Assert.assertEquals("Should produce an equivalent dataset URI", URI.create("view:file:/datasets/test-name?prop=value&num=34"), new URIBuilder("dataset:file:/datasets/test-name") .with("prop", "value") .with("num", 34) .build()); }
@Test public void testRepoUriAndNameAddEquals() { Assert.assertEquals("Should construct the correct dataset URI", URI.create("view:file:/datasets/ns/test-name?prop=value"), new URIBuilder("repo:file:/datasets", "ns", "test-name") .with("prop", "value") .build()); // order should be preserved Assert.assertEquals("Should construct the correct dataset URI", URI.create("view:file:/datasets/ns/test-name?prop=value&num=34"), new URIBuilder("repo:file:/datasets", "ns", "test-name") .with("prop", "value") .with("num", 34) .build()); }
@Override public void run() { Datasets.list(new URIBuilder(repoUri, "ns", "test") .with("field", 34) .build()); } });
@Test public void testRepositoryForViewStringUri() { URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("field", 34) .build(); Assert.assertEquals(repo, DatasetRepositories.repositoryFor(datasetUri.toString())); verifyNoMoreInteractions(repo); }
@Test public void testViewUriAddEquals() throws Exception { Assert.assertEquals("Should produce an equivalent dataset URI", URI.create("view:file:/datasets/test-name?prop=value&field=v2"), new URIBuilder("view:file:/datasets/test-name?prop=value") .with("field", "v2") .build()); Assert.assertEquals("Should produce an equivalent dataset URI", URI.create("view:file:/datasets/test-name?prop=value&field=v2&num=34"), new URIBuilder("view:file:/datasets/test-name?prop=value") .with("field", "v2") .with("num", 34) .build()); Assert.assertEquals("Should produce an equivalent dataset URI", URI.create("view:file:/datasets/test-name?field=a/b"), new URIBuilder("view:file:/datasets/test-name") .with("field", "a/b") .build()); Assert.assertEquals("Should produce an equivalent dataset URI", URI.create("view:file:/datasets/test-name?field=a%2Fb"), new URIBuilder("view:file:/datasets/test-name") .with("field", URLEncoder.encode("a/b", "UTF-8")) .build()); }
@Test public void testDeleteRejectsViewUri() { final URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("field", 34) .build(); TestHelpers.assertThrows("Should reject view URI", IllegalArgumentException.class, new Runnable() { @Override public void run() { Datasets.delete(datasetUri); } }); verifyNoMoreInteractions(repo); }
@Test public void testMoveToTrashRejectsViewUri() { final URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("field", 34) .build(); TestHelpers.assertThrows("Should reject view URI", IllegalArgumentException.class, new Runnable() { @Override public void run() { Datasets.moveToTrash(datasetUri); } }); verifyNoMoreInteractions(repo); }
@Test public void testExistsRejectsViewUri() { final URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("field", 34) .build(); TestHelpers.assertThrows("Should reject view URI", IllegalArgumentException.class, new Runnable() { @Override public void run() { Datasets.exists(datasetUri); } }); verifyNoMoreInteractions(repo); }
@Test public void testRepositoryForView() { URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("field", 34) .build(); Assert.assertEquals(repo, DatasetRepositories.repositoryFor(datasetUri)); verifyNoMoreInteractions(repo); }
@Test public void testCreateViewStringUriWithoutType() throws Exception { DatasetDescriptor descriptor = new DatasetDescriptor.Builder() .schemaUri("resource:schema/user.avsc") .build(); Constraints constraints = new Constraints(descriptor.getSchema(), null) .with("username", "user1") .with("email", "user1@example.com"); AbstractDataset<GenericRecord> ds = mock(AbstractDataset.class); when(repo.create("ns", "test", descriptor, GenericRecord.class)).thenReturn(ds); when(ds.getDescriptor()).thenReturn(descriptor); AbstractRefinableView<GenericRecord> userAndEmailView = mock(AbstractRefinableView.class); when(ds.filter(constraints)).thenReturn(userAndEmailView); URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("username", "user1") .with("email", "user1@example.com") .with("ignoredOption", "abc") .build(); View<GenericRecord> view = Datasets.create(datasetUri.toString(), descriptor); verify(repo).create("ns", "test", descriptor, GenericRecord.class); verifyNoMoreInteractions(repo); verify(ds).getDescriptor(); verify(ds).filter(constraints); verifyNoMoreInteractions(ds); verifyNoMoreInteractions(userAndEmailView); Assert.assertEquals(userAndEmailView, view); }
@Test public void testLoadViewStringUri() throws Exception { DatasetDescriptor descriptor = new DatasetDescriptor.Builder() .schemaUri("resource:schema/user.avsc") .build(); Constraints constraints = new Constraints(descriptor.getSchema(), null) .with("username", "user1") .with("email", "user1@example.com"); AbstractDataset<Object> ds = mock(AbstractDataset.class); when(repo.load("ns", "test", Object.class)).thenReturn(ds); when(ds.getDescriptor()).thenReturn(descriptor); AbstractRefinableView<Object> userAndEmailView = mock(AbstractRefinableView.class); when(ds.filter(constraints)).thenReturn(userAndEmailView); URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("username", "user1") .with("email", "user1@example.com") .with("ignoredOption", "abc") .build(); RefinableView<Object> view = Datasets.<Object, RefinableView<Object>> load(datasetUri.toString(), Object.class); verify(repo).load("ns", "test", Object.class); verifyNoMoreInteractions(repo); verify(ds).getDescriptor(); verify(ds).filter(constraints); verifyNoMoreInteractions(ds); verifyNoMoreInteractions(userAndEmailView); Assert.assertEquals(userAndEmailView, view); }
@Test public void testLoadViewWithoutType() throws Exception { DatasetDescriptor descriptor = new DatasetDescriptor.Builder() .schemaUri("resource:schema/user.avsc") .build(); Constraints constraints = new Constraints(descriptor.getSchema(), null) .with("username", "user1") .with("email", "user1@example.com"); AbstractDataset<GenericRecord> ds = mock(AbstractDataset.class); when(repo.load("ns", "test", GenericRecord.class)).thenReturn(ds); when(ds.getDescriptor()).thenReturn(descriptor); AbstractRefinableView<GenericRecord> userAndEmailView = mock(AbstractRefinableView.class); when(ds.filter(constraints)).thenReturn(userAndEmailView); URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("username", "user1") .with("email", "user1@example.com") .with("ignoredOption", "abc") .build(); RefinableView<GenericRecord> view = Datasets.load(datasetUri); verify(repo).load("ns", "test", GenericRecord.class); verifyNoMoreInteractions(repo); verify(ds).getDescriptor(); verify(ds).filter(constraints); verifyNoMoreInteractions(ds); verifyNoMoreInteractions(userAndEmailView); Assert.assertEquals(userAndEmailView, view); }
@Test public void testCreateView() throws Exception { DatasetDescriptor descriptor = new DatasetDescriptor.Builder() .schemaUri("resource:schema/user.avsc") .build(); Constraints constraints = new Constraints(descriptor.getSchema(), null) .with("username", "user1") .with("email", "user1@example.com"); AbstractDataset<Object> ds = mock(AbstractDataset.class); when(repo.create("ns", "test", descriptor, Object.class)).thenReturn(ds); when(ds.getDescriptor()).thenReturn(descriptor); AbstractRefinableView<Object> userAndEmailView = mock(AbstractRefinableView.class); when(ds.filter(constraints)).thenReturn(userAndEmailView); URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("username", "user1") .with("email", "user1@example.com") .with("ignoredOption", "abc") .build(); RefinableView<Object> view = Datasets.<Object, RefinableView<Object>> create(datasetUri, descriptor, Object.class); verify(repo).create("ns", "test", descriptor, Object.class); verifyNoMoreInteractions(repo); verify(ds).getDescriptor(); verify(ds).filter(constraints); verifyNoMoreInteractions(ds); verifyNoMoreInteractions(userAndEmailView); Assert.assertEquals(userAndEmailView, view); }
@Test public void testLoadView() throws Exception { DatasetDescriptor descriptor = new DatasetDescriptor.Builder() .schemaUri("resource:schema/user.avsc") .build(); Constraints constraints = new Constraints(descriptor.getSchema(), null) .with("username", "user1") .with("email", "user1@example.com"); AbstractDataset<Object> ds = mock(AbstractDataset.class); when(repo.load("ns", "test", Object.class)).thenReturn(ds); when(ds.getDescriptor()).thenReturn(descriptor); AbstractRefinableView<Object> userAndEmailView = mock(AbstractRefinableView.class); when(ds.filter(constraints)).thenReturn(userAndEmailView); URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("username", "user1") .with("email", "user1@example.com") .with("ignoredOption", "abc") .build(); RefinableView<Object> view = Datasets.<Object, RefinableView<Object>> load(datasetUri, Object.class); verify(repo).load("ns", "test", Object.class); verifyNoMoreInteractions(repo); verify(ds).getDescriptor(); verify(ds).filter(constraints); verifyNoMoreInteractions(ds); verifyNoMoreInteractions(userAndEmailView); Assert.assertEquals(userAndEmailView, view); }
@Test public void testCreateViewStringUri() throws Exception { DatasetDescriptor descriptor = new DatasetDescriptor.Builder() .schemaUri("resource:schema/user.avsc") .build(); Constraints constraints = new Constraints(descriptor.getSchema(), null) .with("username", "user1") .with("email", "user1@example.com"); AbstractDataset<Object> ds = mock(AbstractDataset.class); when(repo.create("ns", "test", descriptor, Object.class)).thenReturn(ds); when(ds.getDescriptor()).thenReturn(descriptor); AbstractRefinableView<Object> userAndEmailView = mock(AbstractRefinableView.class); when(ds.filter(constraints)).thenReturn(userAndEmailView); URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("username", "user1") .with("email", "user1@example.com") .with("ignoredOption", "abc") .build(); RefinableView<Object> view = Datasets.<Object, RefinableView<Object>> create(datasetUri.toString(), descriptor, Object.class); verify(repo).create("ns", "test", descriptor, Object.class); verifyNoMoreInteractions(repo); verify(ds).getDescriptor(); verify(ds).filter(constraints); verifyNoMoreInteractions(ds); verifyNoMoreInteractions(userAndEmailView); Assert.assertEquals(userAndEmailView, view); }
@Test public void testCreateViewWithoutType() throws Exception { DatasetDescriptor descriptor = new DatasetDescriptor.Builder() .schemaUri("resource:schema/user.avsc") .build(); Constraints constraints = new Constraints(descriptor.getSchema(), null) .with("username", "user1") .with("email", "user1@example.com"); AbstractDataset<GenericRecord> ds = mock(AbstractDataset.class); when(repo.create("ns", "test", descriptor, GenericRecord.class)).thenReturn(ds); when(ds.getDescriptor()).thenReturn(descriptor); AbstractRefinableView<GenericRecord> userAndEmailView = mock(AbstractRefinableView.class); when(ds.filter(constraints)).thenReturn(userAndEmailView); URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("username", "user1") .with("email", "user1@example.com") .with("ignoredOption", "abc") .build(); View<GenericRecord> view = Datasets.create(datasetUri, descriptor); verify(repo).create("ns", "test", descriptor, GenericRecord.class); verifyNoMoreInteractions(repo); verify(ds).getDescriptor(); verify(ds).filter(constraints); verifyNoMoreInteractions(ds); verifyNoMoreInteractions(userAndEmailView); Assert.assertEquals(userAndEmailView, view); }
@Test public void testUpdateRejectsViewUri() { final URI datasetUri = new URIBuilder(repoUri, "ns", "test") .with("field", 34) .build(); final DatasetDescriptor descriptor = new DatasetDescriptor.Builder() .schemaLiteral("\"string\"") .build(); TestHelpers.assertThrows("Should reject view URI", IllegalArgumentException.class, new Runnable() { @Override public void run() { Datasets.<Object, Dataset<Object>>update(datasetUri, descriptor, Object.class); } }); verifyNoMoreInteractions(repo); }