http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/InstanceRequestTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/InstanceRequestTest.java b/catalog/src/test/java/org/apache/atlas/catalog/InstanceRequestTest.java new file mode 100644 index 0000000..5ccec02 --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/InstanceRequestTest.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog; + +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * Unit tests for InstanceRequest. + */ +public class InstanceRequestTest { + @Test + public void testRequestProperties() { + Map<String, Object> properties = new HashMap<>(); + properties.put("foo", "fooValue"); + properties.put("someBoolean", true); + Request request = new InstanceRequest(properties); + + assertEquals(Request.Cardinality.INSTANCE, request.getCardinality()); + assertEquals(properties, request.getProperties()); + assertEquals("fooValue", request.getProperty("foo")); + assertTrue(request.<Boolean>getProperty("someBoolean")); + assertNull(request.getProperty("other")); + assertTrue(request.getAdditionalSelectProperties().isEmpty()); + } + + @Test + public void testSelectProperties() { + Map<String, Object> properties = new HashMap<>(); + properties.put("foo", "fooValue"); + properties.put("someBoolean", true); + Request request = new InstanceRequest(properties); + + Collection<String> additionalSelectProps = new ArrayList<>(); + additionalSelectProps.add("prop1"); + additionalSelectProps.add("prop2"); + request.addAdditionalSelectProperties(additionalSelectProps); + Collection<String> requestAdditionalSelectProps = request.getAdditionalSelectProperties(); + assertEquals(2, requestAdditionalSelectProps.size()); + assertTrue(requestAdditionalSelectProps.contains("prop1")); + assertTrue(requestAdditionalSelectProps.contains("prop2")); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/JsonSerializerTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/JsonSerializerTest.java b/catalog/src/test/java/org/apache/atlas/catalog/JsonSerializerTest.java new file mode 100644 index 0000000..60cc210 --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/JsonSerializerTest.java @@ -0,0 +1,120 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog; + +import org.testng.annotations.Test; + +import javax.ws.rs.core.UriInfo; + +import java.net.URI; +import java.util.*; + +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.testng.Assert.assertEquals; + +/** + * Unit tests for JsonSerializer. + */ +public class JsonSerializerTest { + @Test + public void testSerialize() throws Exception { + UriInfo uriInfo = createStrictMock(UriInfo.class); + URI uri = new URI("http://test.com:8080/"); + expect(uriInfo.getBaseUri()).andReturn(uri); + + replay(uriInfo); + + Collection<Map<String, Object>> resultMaps = new ArrayList<>(); + // result map 1 + ResourceComparator resourceComparator = new ResourceComparator(); + Map<String, Object> resultMap1 = new TreeMap<>(resourceComparator); + resultMaps.add(resultMap1); + + resultMap1.put("prop1", "property 1 value"); + resultMap1.put("booleanProp", true); + resultMap1.put("numberProp", 100); + resultMap1.put("href", "v1/testResources/foo"); + + ArrayList<String> listProp = new ArrayList<>(); + listProp.add("one"); + listProp.add("two"); + resultMap1.put("listProp", listProp); + + Map<String, Object> mapProp = new TreeMap<>(resourceComparator); + mapProp.put("mapProp1", "mapProp1Value"); + ArrayList<String> mapListProp = new ArrayList<>(); + mapListProp.add("mapListOne"); + mapListProp.add("mapListTwo"); + mapProp.put("mapListProp", mapListProp); + mapProp.put("href", "v1/testResources/foobar"); + resultMap1.put("mapProp", mapProp); + + // result map 2 + Map<String, Object> resultMap2 = new TreeMap<>(resourceComparator); + resultMaps.add(resultMap2); + + resultMap2.put("nullProp", null); + resultMap2.put("href", "v1/testResources/bar"); + + ArrayList<Map<String, Object>> listProp2 = new ArrayList<>(); + listProp2.add(Collections.<String, Object>singletonMap("listMapProp", "listMapPropValue")); + resultMap2.put("listProp", listProp2); + + Result result = new Result(resultMaps); + + JsonSerializer serializer = new JsonSerializer(); + String resultJson = serializer.serialize(result, uriInfo); + + assertEquals(resultJson, EXPECTED_JSON); + } + + private static final String EXPECTED_JSON = + "[\n" + + " {\n" + + " \"href\": \"http://test.com:8080/v1/testResources/foo\",\n" + + " \"booleanProp\": true,\n" + + " \"numberProp\": 100,\n" + + " \"prop1\": \"property 1 value\",\n" + + " \"listProp\": [\n" + + " \"one\",\n" + + " \"two\"\n" + + " ],\n" + + " \"mapProp\": {\n" + + " \"href\": \"http://test.com:8080/v1/testResources/foobar\",\n" + + " \"mapProp1\": \"mapProp1Value\",\n" + + " \"mapListProp\": [\n" + + " \"mapListOne\",\n" + + " \"mapListTwo\"\n" + + " ]\n" + + " }\n" + + " },\n" + + " {\n" + + " \"href\": \"http://test.com:8080/v1/testResources/bar\",\n" + + " \"nullProp\": null,\n" + + " \"listProp\": [\n" + + " {\n" + + " \"listMapProp\": \"listMapPropValue\"\n" + + " }\n" + + " ]\n" + + " }\n" + + "]"; + +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/ResourceComparatorTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/ResourceComparatorTest.java b/catalog/src/test/java/org/apache/atlas/catalog/ResourceComparatorTest.java new file mode 100644 index 0000000..146ca0c --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/ResourceComparatorTest.java @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog; + +import org.testng.annotations.Test; + +import java.util.*; + +import static org.testng.Assert.assertEquals; + +/** + * Unit tests for ResourceComparator. + */ +public class ResourceComparatorTest { + @Test + public void testCompare() { + Map<String, Object> map = new TreeMap<>(new ResourceComparator()); + map.put("a", "zzzzz"); + map.put("name", 1); + map.put("z", "fdsfdsds"); + map.put("d", new ArrayList<>()); + map.put("id", 1); + map.put("e", false); + map.put("c", 1); + map.put("href", "dfdfgdf"); + map.put("b", new HashMap<>()); + map.put("description", 1); + map.put("f", 20); + map.put("type", 1); + + Iterator<String> iter = map.keySet().iterator(); + assertEquals(iter.next(), "href"); + assertEquals(iter.next(), "name"); + assertEquals(iter.next(), "id"); + assertEquals(iter.next(), "description"); + assertEquals(iter.next(), "type"); + assertEquals(iter.next(), "a"); + assertEquals(iter.next(), "b"); + assertEquals(iter.next(), "c"); + assertEquals(iter.next(), "d"); + assertEquals(iter.next(), "e"); + assertEquals(iter.next(), "f"); + assertEquals(iter.next(), "z"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/TaxonomyResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/TaxonomyResourceProviderTest.java b/catalog/src/test/java/org/apache/atlas/catalog/TaxonomyResourceProviderTest.java new file mode 100644 index 0000000..9cf3d12 --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/TaxonomyResourceProviderTest.java @@ -0,0 +1,287 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog; + +import org.apache.atlas.catalog.definition.ResourceDefinition; +import org.apache.atlas.catalog.definition.TaxonomyResourceDefinition; +import org.apache.atlas.catalog.exception.InvalidPayloadException; +import org.apache.atlas.catalog.exception.ResourceAlreadyExistsException; +import org.apache.atlas.catalog.exception.ResourceNotFoundException; +import org.apache.atlas.catalog.query.AtlasQuery; +import org.apache.atlas.catalog.query.QueryFactory; +import org.easymock.Capture; +import org.testng.annotations.Test; + +import java.util.*; + +import static org.easymock.EasyMock.*; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * Unit tests for TaxonomyResourceProvider. + */ +public class TaxonomyResourceProviderTest { + @Test + public void testGetResource() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + Collection<Map<String, Object>> queryResult = new ArrayList<>(); + Map<String, Object> queryResultRow = new HashMap<>(); + queryResult.add(queryResultRow); + queryResultRow.put("name", "taxonomyName"); + queryResultRow.put("description", "test taxonomy description"); + queryResultRow.put("creation_time", "04/20/2016"); + + // mock expectations + expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(queryResult); + replay(typeSystem, queryFactory, query); + + TaxonomyResourceProvider provider = new TaxonomyResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("name", "taxonomyName"); + Request userRequest = new InstanceRequest(requestProperties); + + Result result = provider.getResourceById(userRequest); + + assertEquals(1, result.getPropertyMaps().size()); + assertEquals(queryResultRow, result.getPropertyMaps().iterator().next()); + + Request request = requestCapture.getValue(); + assertNull(request.getQueryString()); + assertEquals(0, request.getAdditionalSelectProperties().size()); + assertEquals(requestProperties, request.getProperties()); + + verify(typeSystem, queryFactory, query); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testGetResource_404() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + // empty response should result in a ResourceNotFoundException + Collection<Map<String, Object>> emptyResponse = new ArrayList<>(); + + // mock expectations + expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(emptyResponse); + replay(typeSystem, queryFactory, query); + + TaxonomyResourceProvider provider = new TaxonomyResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("name", "taxonomyName"); + Request request = new InstanceRequest(requestProperties); + + provider.getResourceById(request); + + verify(typeSystem, queryFactory, query); + } + + @Test + public void testGetResources() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + Collection<Map<String, Object>> queryResult = new ArrayList<>(); + Map<String, Object> queryResultRow1 = new HashMap<>(); + queryResult.add(queryResultRow1); + queryResultRow1.put("mame", "taxonomyName1"); + queryResultRow1.put("description", "test taxonomy description"); + queryResultRow1.put("creation_time", "04/20/2016"); + + Map<String, Object> queryResultRow2 = new HashMap<>(); + queryResult.add(queryResultRow2); + queryResultRow2.put("mame", "taxonomyName2"); + queryResultRow2.put("description", "test taxonomy description 2"); + queryResultRow2.put("creation_time", "04/21/2016"); + + // mock expectations + expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(queryResult); + replay(typeSystem, queryFactory, query); + + TaxonomyResourceProvider provider = new TaxonomyResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + Request userRequest = new CollectionRequest(Collections.<String, Object>emptyMap(), "name:taxonomy*"); + Result result = provider.getResources(userRequest); + + assertEquals(2, result.getPropertyMaps().size()); + assertTrue(result.getPropertyMaps().contains(queryResultRow1)); + assertTrue(result.getPropertyMaps().contains(queryResultRow2)); + + Request request = requestCapture.getValue(); + assertEquals("name:taxonomy*", request.getQueryString()); + assertEquals(0, request.getAdditionalSelectProperties().size()); + assertEquals(0, request.getProperties().size()); + + verify(typeSystem, queryFactory, query); + } + + @Test + public void testGetResources_noResults() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + // empty result shouldn't result in exception for collection query + Collection<Map<String, Object>> queryResult = new ArrayList<>(); + + // mock expectations + expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(queryResult); + replay(typeSystem, queryFactory, query); + + TaxonomyResourceProvider provider = new TaxonomyResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + Request userRequest = new CollectionRequest(Collections.<String, Object>emptyMap(), "name:taxonomy*"); + Result result = provider.getResources(userRequest); + + assertEquals(0, result.getPropertyMaps().size()); + + Request request = requestCapture.getValue(); + assertEquals("name:taxonomy*", request.getQueryString()); + assertEquals(0, request.getAdditionalSelectProperties().size()); + assertEquals(0, request.getProperties().size()); + + verify(typeSystem, queryFactory, query); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testCreateResource_invalidRequest__noName() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + + // mock expectations + replay(typeSystem, queryFactory, query); + + // taxonomy create request must contain 'name' property + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("description", "test"); + Request userRequest = new InstanceRequest(requestProperties); + + TaxonomyResourceProvider provider = new TaxonomyResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + provider.createResource(userRequest); + } + + @Test(expectedExceptions = ResourceAlreadyExistsException.class) + public void testCreateResource_invalidRequest__alreadyExists() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + // query is executed to see if resource already exists + Collection<Map<String, Object>> queryResult = new ArrayList<>(); + Map<String, Object> queryResultRow = new HashMap<>(); + queryResult.add(queryResultRow); + queryResultRow.put("mame", "taxonomyName"); + queryResultRow.put("description", "test taxonomy description"); + queryResultRow.put("creation_time", "04/20/2016"); + + // mock expectations + expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); + // returning result for query should result in ResourceAlreadyExistsException + expect(query.execute()).andReturn(queryResult); + replay(typeSystem, queryFactory, query); + + // taxonomy create request must contain 'name' property + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("name", "taxonomyName"); + Request userRequest = new InstanceRequest(requestProperties); + + TaxonomyResourceProvider provider = new TaxonomyResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + provider.createResource(userRequest); + } + + @Test + public void testCreateResource() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<ResourceDefinition> resourceDefinitionCapture = newCapture(); + Capture<Request> requestCapture = newCapture(); + + // empty response indicates that resource doesn't already exist + Collection<Map<String, Object>> queryResult = new ArrayList<>(); + + // mock expectations + expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(queryResult); + typeSystem.createEntity(capture(resourceDefinitionCapture), capture(requestCapture)); + replay(typeSystem, queryFactory, query); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("name", "taxonomyName"); + Request userRequest = new InstanceRequest(requestProperties); + + TaxonomyResourceProvider provider = new TaxonomyResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + provider.createResource(userRequest); + + assertEquals(new TaxonomyResourceDefinition().getTypeName(), + resourceDefinitionCapture.getValue().getTypeName()); + + Request request = requestCapture.getValue(); + assertNull(request.getQueryString()); + assertEquals(requestProperties, request.getProperties()); + + verify(typeSystem, queryFactory, query); + } + + @Test(expectedExceptions = UnsupportedOperationException.class) + public void testCreateResources() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + + // mock expectations + replay(typeSystem, queryFactory); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("name", "taxonomyName"); + Request userRequest = new InstanceRequest(requestProperties); + + TaxonomyResourceProvider provider = new TaxonomyResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + provider.createResources(userRequest); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/TermResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/TermResourceProviderTest.java b/catalog/src/test/java/org/apache/atlas/catalog/TermResourceProviderTest.java new file mode 100644 index 0000000..34222ca --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/TermResourceProviderTest.java @@ -0,0 +1,355 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog; + +import org.apache.atlas.catalog.definition.ResourceDefinition; +import org.apache.atlas.catalog.exception.InvalidPayloadException; +import org.apache.atlas.catalog.exception.ResourceAlreadyExistsException; +import org.apache.atlas.catalog.exception.ResourceNotFoundException; +import org.apache.atlas.catalog.query.AtlasQuery; +import org.apache.atlas.catalog.query.QueryFactory; +import org.easymock.Capture; +import org.easymock.EasyMock; +import org.testng.annotations.Test; + +import java.util.*; + +import static org.easymock.EasyMock.*; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * Unit tests for TermResourceProvider. + */ +public class TermResourceProviderTest { + @Test + public void testGetResource() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + TermPath termPath = new TermPath("testTaxonomy", "termName"); + + Collection<Map<String, Object>> queryResult = new ArrayList<>(); + Map<String, Object> queryResultRow = new HashMap<>(); + queryResult.add(queryResultRow); + queryResultRow.put("name", "testTaxonomy.termName"); + queryResultRow.put("description", "test term description"); + queryResultRow.put("creation_time", "04/20/2016"); + queryResultRow.put("acceptable_use", "anything"); + queryResultRow.put("available_as_tag", true); + Map<String, Object> hierarchyMap = new HashMap<>(); + queryResultRow.put("hierarchy", hierarchyMap); + hierarchyMap.put("path", "/"); + hierarchyMap.put("short_name", "termName"); + hierarchyMap.put("taxonomy", "testTaxonomy"); + + // mock expectations + expect(queryFactory.createTermQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(queryResult); + replay(typeSystem, queryFactory, query); + + TermResourceProvider provider = new TermResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("termPath", termPath); + Request userRequest = new InstanceRequest(requestProperties); + + Result result = provider.getResourceById(userRequest); + + assertEquals(result.getPropertyMaps().size(), 1); + assertEquals(result.getPropertyMaps().iterator().next(), queryResultRow); + + Request request = requestCapture.getValue(); + assertNull(request.getQueryString()); + assertEquals(request.getAdditionalSelectProperties().size(), 0); + assertEquals(request.getProperties().size(), 2); + assertEquals(request.getProperties().get("termPath"), termPath); + assertEquals(request.getProperties().get("name"), termPath.getFullyQualifiedName()); + + verify(typeSystem, queryFactory, query); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testGetResource_404() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + // empty response should result in a ResourceNotFoundException + Collection<Map<String, Object>> emptyResponse = new ArrayList<>(); + + // mock expectations + expect(queryFactory.createTermQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(emptyResponse); + replay(typeSystem, queryFactory, query); + + TermResourceProvider provider = new TermResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("termPath", new TermPath("taxonomyName.badTermName")); + Request request = new InstanceRequest(requestProperties); + + provider.getResourceById(request); + } + + @Test + public void testGetResources() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + TermPath termPath = new TermPath("testTaxonomy", null); + + Collection<Map<String, Object>> queryResult = new ArrayList<>(); + Map<String, Object> queryResultRow1 = new HashMap<>(); + queryResult.add(queryResultRow1); + queryResultRow1.put("name", "testTaxonomy.termName"); + queryResultRow1.put("description", "test term description"); + queryResultRow1.put("creation_time", "04/20/2016"); + queryResultRow1.put("acceptable_use", "anything"); + queryResultRow1.put("available_as_tag", true); + Map<String, Object> hierarchyMap = new HashMap<>(); + queryResultRow1.put("hierarchy", hierarchyMap); + hierarchyMap.put("path", "/"); + hierarchyMap.put("short_name", "termName"); + hierarchyMap.put("taxonomy", "testTaxonomy"); + + Map<String, Object> queryResultRow2 = new HashMap<>(); + queryResult.add(queryResultRow2); + queryResultRow2.put("name", "testTaxonomy.termName2"); + queryResultRow2.put("description", "test term 2 description"); + queryResultRow2.put("creation_time", "04/21/2016"); + queryResultRow2.put("acceptable_use", "anything"); + queryResultRow2.put("available_as_tag", true); + Map<String, Object> hierarchyMap2 = new HashMap<>(); + queryResultRow2.put("hierarchy", hierarchyMap2); + hierarchyMap2.put("path", "/"); + hierarchyMap2.put("short_name", "termName2"); + hierarchyMap2.put("taxonomy", "testTaxonomy"); + + // mock expectations + expect(queryFactory.createTermQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(queryResult); + replay(typeSystem, queryFactory, query); + + TermResourceProvider provider = new TermResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("termPath", termPath); + Request userRequest = new CollectionRequest(requestProperties, "name:taxonomy*"); + // invoke test method + Result result = provider.getResources(userRequest); + + assertEquals(result.getPropertyMaps().size(), 2); + assertTrue(result.getPropertyMaps().contains(queryResultRow1)); + assertTrue(result.getPropertyMaps().contains(queryResultRow2)); + + Request request = requestCapture.getValue(); + assertEquals(request.getQueryString(), "name:taxonomy*"); + assertEquals(request.getAdditionalSelectProperties().size(), 0); + assertEquals(request.getProperties().size(), 1); + + verify(typeSystem, queryFactory, query); + } + + @Test + public void testGetResources_noResults() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<Request> requestCapture = newCapture(); + + TermPath termPath = new TermPath("testTaxonomy", "termName"); + + // empty result shouldn't result in exception for collection query + Collection<Map<String, Object>> queryResult = new ArrayList<>(); + + // mock expectations + expect(queryFactory.createTermQuery(capture(requestCapture))).andReturn(query); + expect(query.execute()).andReturn(queryResult); + replay(typeSystem, queryFactory, query); + + TermResourceProvider provider = new TermResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("termPath", termPath); + Request userRequest = new CollectionRequest(requestProperties, "name:taxonomy*"); + // invoke test method + Result result = provider.getResources(userRequest); + + assertEquals(0, result.getPropertyMaps().size()); + + Request request = requestCapture.getValue(); + assertEquals(request.getQueryString(), "name:taxonomy*"); + assertEquals(request.getAdditionalSelectProperties().size(), 0); + assertEquals(request.getProperties().size(), 1); + + verify(typeSystem, queryFactory, query); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testCreateResource_invalidRequest__noName() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + + // null term name should result in InvalidPayloadException + TermPath termPath = new TermPath("testTaxonomy", null); + + // mock expectations + replay(typeSystem, queryFactory, query); + + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("termPath", termPath); + Request userRequest = new InstanceRequest(requestProperties); + + TermResourceProvider provider = new TermResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + provider.createResource(userRequest); + } + + @Test + public void testCreateResource() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<ResourceDefinition> resourceDefinitionCapture = newCapture(); + ResourceProvider taxonomyResourceProvider = createStrictMock(TaxonomyResourceProvider.class); + Capture<Request> taxonomyRequestCapture = newCapture(); + + Collection<Map<String, Object>> taxonomyQueryResult = new ArrayList<>(); + Map<String, Object> taxonomyQueryResultRow = new HashMap<>(); + taxonomyQueryResult.add(taxonomyQueryResultRow); + taxonomyQueryResultRow.put("name", "testTaxonomy"); + taxonomyQueryResultRow.put("id", "11-22-33"); + Result taxonomyResult = new Result(taxonomyQueryResult); + + Map<String, Object> expectedRequestProps = new HashMap<>(); + expectedRequestProps.put("name", "testTaxonomy.termName"); + // when not specified, the default value of 'true' should be set + expectedRequestProps.put("available_as_tag", true); + + // mock expectations + expect(taxonomyResourceProvider.getResourceById(capture(taxonomyRequestCapture))).andReturn(taxonomyResult); + typeSystem.createTraitType(capture(resourceDefinitionCapture), eq("testTaxonomy.termName"), EasyMock.<String>isNull()); + typeSystem.createTraitInstance("11-22-33", "testTaxonomy.termName", expectedRequestProps); + replay(typeSystem, queryFactory, query, taxonomyResourceProvider); + + TermResourceProvider provider = new TestTermResourceProvider(typeSystem, taxonomyResourceProvider); + provider.setQueryFactory(queryFactory); + + TermPath termPath = new TermPath("testTaxonomy", "termName"); + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("termPath", termPath); + Request userRequest = new InstanceRequest(requestProperties); + + provider.createResource(userRequest); + + Request taxonomyRequest = taxonomyRequestCapture.getValue(); + Map<String, Object> taxonomyRequestProps = taxonomyRequest.getProperties(); + assertEquals(taxonomyRequestProps.size(), 1); + assertEquals(taxonomyRequestProps.get("name"), "testTaxonomy"); + assertEquals(taxonomyRequest.getAdditionalSelectProperties().size(), 1); + assertEquals(taxonomyRequest.getAdditionalSelectProperties().iterator().next(), "id"); + assertNull(taxonomyRequest.getQueryString()); + + ResourceDefinition resourceDefinition = resourceDefinitionCapture.getValue(); + assertEquals(resourceDefinition.getTypeName(), "Term"); + + verify(typeSystem, queryFactory, query, taxonomyResourceProvider); + } + + @Test(expectedExceptions = ResourceAlreadyExistsException.class) + public void testCreateResource_invalidRequest__alreadyExists() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + AtlasQuery query = createStrictMock(AtlasQuery.class); + Capture<ResourceDefinition> resourceDefinitionCapture = newCapture(); + ResourceProvider taxonomyResourceProvider = createStrictMock(TaxonomyResourceProvider.class); + Capture<Request> taxonomyRequestCapture = newCapture(); + + Collection<Map<String, Object>> taxonomyQueryResult = new ArrayList<>(); + Map<String, Object> taxonomyQueryResultRow = new HashMap<>(); + taxonomyQueryResult.add(taxonomyQueryResultRow); + taxonomyQueryResultRow.put("name", "testTaxonomy"); + taxonomyQueryResultRow.put("id", "11-22-33"); + Result taxonomyResult = new Result(taxonomyQueryResult); + + // mock expectations + expect(taxonomyResourceProvider.getResourceById(capture(taxonomyRequestCapture))).andReturn(taxonomyResult); + typeSystem.createTraitType(capture(resourceDefinitionCapture), eq("testTaxonomy.termName"), EasyMock.<String>isNull()); + expectLastCall().andThrow(new ResourceAlreadyExistsException("")); + + replay(typeSystem, queryFactory, query, taxonomyResourceProvider); + + TermResourceProvider provider = new TestTermResourceProvider(typeSystem, taxonomyResourceProvider); + provider.setQueryFactory(queryFactory); + + TermPath termPath = new TermPath("testTaxonomy", "termName"); + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("termPath", termPath); + Request userRequest = new InstanceRequest(requestProperties); + + provider.createResource(userRequest); + } + + @Test(expectedExceptions = UnsupportedOperationException.class) + public void testCreateResources() throws Exception { + AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); + QueryFactory queryFactory = createStrictMock(QueryFactory.class); + + // mock expectations + replay(typeSystem, queryFactory); + + TermPath termPath = new TermPath("testTaxonomy", "termName"); + Map<String, Object> requestProperties = new HashMap<>(); + requestProperties.put("termPath", termPath); + Request userRequest = new InstanceRequest(requestProperties); + + TermResourceProvider provider = new TermResourceProvider(typeSystem); + provider.setQueryFactory(queryFactory); + + provider.createResources(userRequest); + } + + private static class TestTermResourceProvider extends TermResourceProvider { + + private ResourceProvider testTaxonomyResourceProvider; + + public TestTermResourceProvider(AtlasTypeSystem typeSystem, ResourceProvider taxonomyResourceProvider) { + super(typeSystem); + testTaxonomyResourceProvider = taxonomyResourceProvider; + } + + @Override + protected synchronized ResourceProvider getTaxonomyResourceProvider() { + return testTaxonomyResourceProvider; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/VertexWrapperTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/VertexWrapperTest.java b/catalog/src/test/java/org/apache/atlas/catalog/VertexWrapperTest.java new file mode 100644 index 0000000..64935a6 --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/VertexWrapperTest.java @@ -0,0 +1,311 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog; + +import com.tinkerpop.blueprints.Vertex; +import org.apache.atlas.catalog.definition.ResourceDefinition; +import org.apache.atlas.repository.Constants; +import org.testng.annotations.Test; + +import java.util.*; + +import static org.easymock.EasyMock.*; +import static org.testng.Assert.*; + +/** + * Unit tests for VertexWrapper. + */ +public class VertexWrapperTest { + @Test + public void testGetVertex() { + Vertex v = createStrictMock(Vertex.class); + ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class); + + // just return null for these because they aren't used in this test + expect(resourceDefinition.getPropertyMapper()).andReturn(null); + expect(resourceDefinition.getPropertyValueFormatters()).andReturn(null); + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn("testType"); + replay(v, resourceDefinition); + + VertexWrapper vWrapper = new VertexWrapper(v, resourceDefinition); + + assertEquals(vWrapper.getVertex(), v); + verify(v, resourceDefinition); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetProperty() { + String testType = "testType"; + String propName = "propName"; + String qualifiedPropName = "Prefix.propName"; + String propValue = "val"; + String formattedValue = "value"; + Vertex v = createStrictMock(Vertex.class); + PropertyMapper propertyMapper = createStrictMock(PropertyMapper.class); + PropertyValueFormatter formatter = createStrictMock(PropertyValueFormatter.class); + + // mock expectations + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn(testType); + expect(propertyMapper.toFullyQualifiedName(propName, testType)).andReturn(qualifiedPropName); + expect(v.getProperty(qualifiedPropName)).andReturn(propValue); + expect(formatter.format(propValue)).andReturn((formattedValue)); + replay(v, propertyMapper, formatter); + + VertexWrapper vWrapper = new VertexWrapper(v, propertyMapper, Collections.singletonMap(propName, formatter)); + assertEquals(vWrapper.getProperty(propName), formattedValue); + + // now remove prop + vWrapper.removeProperty(propName); + assertNull(vWrapper.getProperty(propName)); + + verify(v, propertyMapper, formatter); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetProperty2() { + String testType = "testType"; + String propName = "propName"; + String qualifiedPropName = "Prefix.propName"; + String propValue = "val"; + String formattedValue = "value"; + Vertex v = createStrictMock(Vertex.class); + ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class); + PropertyMapper propertyMapper = createStrictMock(PropertyMapper.class); + PropertyValueFormatter formatter = createStrictMock(PropertyValueFormatter.class); + + // mock expectations + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn(testType); + expect(resourceDefinition.getPropertyMapper()).andReturn(propertyMapper); + expect(resourceDefinition.getPropertyValueFormatters()).andReturn(Collections.singletonMap(propName, formatter)); + expect(propertyMapper.toFullyQualifiedName(propName, testType)).andReturn(qualifiedPropName); + expect(v.getProperty(qualifiedPropName)).andReturn(propValue); + expect(formatter.format(propValue)).andReturn((formattedValue)); + replay(v, resourceDefinition, propertyMapper, formatter); + + VertexWrapper vWrapper = new VertexWrapper(v, resourceDefinition); + assertEquals(vWrapper.getProperty(propName), formattedValue); + + // now remove prop + vWrapper.removeProperty(propName); + assertNull(vWrapper.getProperty(propName)); + + verify(v, resourceDefinition, propertyMapper, formatter); + } + + @Test + public void testGetProperty_removed() { + String testType = "testType"; + String propName = "propName"; + Vertex v = createStrictMock(Vertex.class); + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn(testType); + // vertex shouldn't be asked for the removed property + replay(v); + + VertexWrapper vWrapper = new VertexWrapper(v, null, Collections.<String, PropertyValueFormatter>emptyMap()); + vWrapper.removeProperty(propName); + + assertNull(vWrapper.getProperty(propName)); + verify(v); + } + + @Test + public void testGetPropertyKeys() { + String testType = "testType"; + // vertex returns unordered set + Set<String> propertyKeys = new HashSet<>(); + propertyKeys.add("foobar"); + propertyKeys.add("Prefix.foo"); + propertyKeys.add("Prefix.bar"); + + Vertex v = createStrictMock(Vertex.class); + PropertyMapper propertyMapper = createMock(PropertyMapper.class); + + // mock expectations + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn(testType); + expect(v.getPropertyKeys()).andReturn(propertyKeys); + expect(propertyMapper.toCleanName("Prefix.bar", testType)).andReturn("bar"); + expect(propertyMapper.toCleanName("Prefix.foo", testType)).andReturn("foo"); + expect(propertyMapper.toCleanName("foobar", testType)).andReturn("foobar"); + + replay(v, propertyMapper); + + VertexWrapper vWrapper = new VertexWrapper(v, propertyMapper, + Collections.<String, PropertyValueFormatter>emptyMap()); + + Collection<String> resultKeys = vWrapper.getPropertyKeys(); + Iterator<String> propIterator = resultKeys.iterator(); + assertEquals(resultKeys.size(), 3); + // natural ordering is applied in vertex wrapper + assertEquals(propIterator.next(), "bar"); + assertEquals(propIterator.next(), "foo"); + assertEquals(propIterator.next(), "foobar"); + + verify(v, propertyMapper); + } + + @Test + public void testGetPropertyKeys_removed() { + String testType = "testType"; + Set<String> propertyKeys = new TreeSet<>(); + propertyKeys.add("Prefix.foo"); + propertyKeys.add("Prefix.bar"); + propertyKeys.add("foobar"); + + Vertex v = createStrictMock(Vertex.class); + PropertyMapper propertyMapper = createStrictMock(PropertyMapper.class); + + // mock expectations + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn(testType); + expect(v.getPropertyKeys()).andReturn(propertyKeys); + // natural ordering provided by TreeSet + expect(propertyMapper.toCleanName("Prefix.bar", testType)).andReturn("bar"); + expect(propertyMapper.toCleanName("Prefix.foo", testType)).andReturn("foo"); + expect(propertyMapper.toCleanName("foobar", testType)).andReturn("foobar"); + + replay(v, propertyMapper); + + VertexWrapper vWrapper = new VertexWrapper(v, propertyMapper, + Collections.<String, PropertyValueFormatter>emptyMap()); + + // remove props + vWrapper.removeProperty("foo"); + vWrapper.removeProperty("foobar"); + + Collection<String> resultKeys = vWrapper.getPropertyKeys(); + assertEquals(resultKeys.size(), 1); + assertTrue(resultKeys.contains("bar")); + + verify(v, propertyMapper); + } + + @Test + public void testGetPropertyMap() { + String testType = "testType"; + Set<String> propertyKeys = new HashSet<>(); + propertyKeys.add("Prefix.foo"); + propertyKeys.add("Prefix.bar"); + propertyKeys.add("foobar"); + + Vertex v = createMock(Vertex.class); + PropertyMapper propertyMapper = createMock(PropertyMapper.class); + PropertyValueFormatter formatter = createMock(PropertyValueFormatter.class); + + Map<String, PropertyValueFormatter> valueFormatters = new HashMap<>(); + valueFormatters.put("foo", formatter); + valueFormatters.put("bar", formatter); + + // mock expectations + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn(testType); + expect(v.getPropertyKeys()).andReturn(propertyKeys); + expect(v.getProperty("Prefix.foo")).andReturn("Prefix.foo:Value"); + expect(v.getProperty("Prefix.bar")).andReturn("Prefix.bar:Value"); + expect(v.getProperty("foobar")).andReturn("foobarValue"); + + expect(propertyMapper.toCleanName("Prefix.bar", testType)).andReturn("bar"); + expect(propertyMapper.toCleanName("Prefix.foo", testType)).andReturn("foo"); + expect(propertyMapper.toCleanName("foobar", testType)).andReturn("foobar"); + + expect(formatter.format("Prefix.foo:Value")).andReturn("fooValue"); + expect(formatter.format("Prefix.bar:Value")).andReturn("barValue"); + + replay(v, propertyMapper, formatter); + + VertexWrapper vWrapper = new VertexWrapper(v, propertyMapper, valueFormatters); + Map<String, Object> resultMap = vWrapper.getPropertyMap(); + + assertEquals(resultMap.size(), 3); + Iterator<Map.Entry<String, Object>> iter = resultMap.entrySet().iterator(); + Map.Entry<String, Object> entry1 = iter.next(); + assertEquals(entry1.getKey(), "bar"); + assertEquals(entry1.getValue(), "barValue"); + Map.Entry<String, Object> entry2 = iter.next(); + assertEquals(entry2.getKey(), "foo"); + assertEquals(entry2.getValue(), "fooValue"); + Map.Entry<String, Object> entry3 = iter.next(); + assertEquals(entry3.getKey(), "foobar"); + assertEquals(entry3.getValue(), "foobarValue"); + + verify(v, propertyMapper, formatter); + } + + @Test + public void testGetPropertyMap_removed() { + String testType = "testType"; + Set<String> propertyKeys = new HashSet<>(); + propertyKeys.add("Prefix.foo"); + propertyKeys.add("Prefix.bar"); + propertyKeys.add("foobar"); + + Vertex v = createMock(Vertex.class); + PropertyMapper propertyMapper = createMock(PropertyMapper.class); + PropertyValueFormatter formatter = createMock(PropertyValueFormatter.class); + + Map<String, PropertyValueFormatter> valueFormatters = new HashMap<>(); + valueFormatters.put("foo", formatter); + valueFormatters.put("bar", formatter); + + // mock expectations + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn(testType); + expect(v.getPropertyKeys()).andReturn(propertyKeys); + expect(v.getProperty("Prefix.bar")).andReturn("Prefix.bar:Value"); + expect(v.getProperty("foobar")).andReturn("foobarValue"); + + expect(propertyMapper.toCleanName("Prefix.bar", testType)).andReturn("bar"); + expect(propertyMapper.toCleanName("Prefix.foo", testType)).andReturn("foo"); + expect(propertyMapper.toCleanName("foobar", testType)).andReturn("foobar"); + + expect(formatter.format("Prefix.bar:Value")).andReturn("barValue"); + + replay(v, propertyMapper, formatter); + + VertexWrapper vWrapper = new VertexWrapper(v, propertyMapper, valueFormatters); + //remove "foo" property + vWrapper.removeProperty("foo"); + + Map<String, Object> resultMap = vWrapper.getPropertyMap(); + assertEquals(resultMap.size(), 2); + + Iterator<Map.Entry<String, Object>> iter = resultMap.entrySet().iterator(); + Map.Entry<String, Object> entry1 = iter.next(); + assertEquals(entry1.getKey(), "bar"); + assertEquals(entry1.getValue(), "barValue"); + Map.Entry<String, Object> entry2 = iter.next(); + assertEquals(entry2.getKey(), "foobar"); + assertEquals(entry2.getValue(), "foobarValue"); + + verify(v, propertyMapper, formatter); + } + + @Test + public void testIsPropertyRemoved() { + String testType = "testType"; + Vertex v = createMock(Vertex.class); + expect(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY)).andReturn(testType); + replay(v); + + VertexWrapper vWrapper = new VertexWrapper(v, null, + Collections.<String, PropertyValueFormatter>emptyMap()); + + vWrapper.removeProperty("foo"); + assertTrue(vWrapper.isPropertyRemoved("foo")); + assertFalse(vWrapper.isPropertyRemoved("bar")); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/definition/EntityResourceDefinitionTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/definition/EntityResourceDefinitionTest.java b/catalog/src/test/java/org/apache/atlas/catalog/definition/EntityResourceDefinitionTest.java new file mode 100644 index 0000000..303e2ba --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/definition/EntityResourceDefinitionTest.java @@ -0,0 +1,140 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog.definition; + +import org.apache.atlas.catalog.CollectionRequest; +import org.apache.atlas.catalog.InstanceRequest; +import org.apache.atlas.catalog.Request; +import org.apache.atlas.catalog.projection.Projection; +import org.apache.atlas.catalog.projection.Relation; +import org.testng.annotations.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * Unit tests for EntityResourceDefinition. + */ +public class EntityResourceDefinitionTest { + @Test + public void testGetIdPropertyName() { + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + assertEquals(entityDefinition.getIdPropertyName(), "id"); + } + + @Test + public void testGetTypeName() { + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + assertNull(entityDefinition.getTypeName()); + } + + @Test + public void testResolveHref() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "foo"); + + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + String href = entityDefinition.resolveHref(resourceProps); + assertEquals(href, "v1/entities/111-222-333"); + } + + // Because we don't currently support entity creation, this method is basically a no-op. + @Test + public void testValidate() throws Exception { + Request request = new InstanceRequest(Collections.<String, Object>emptyMap()); + + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + entityDefinition.validate(request); + } + + // Because we don't currently support entity creation, no properties are registered + @Test + public void testGetPropertyDefinitions() { + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + assertTrue(entityDefinition.getPropertyDefinitions().isEmpty()); + } + + @Test + public void testFilterProperties_Instance() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "nameVal"); + resourceProps.put("type", "someType"); + resourceProps.put("foo", "fooVal"); + resourceProps.put("bar", "barVal"); + resourceProps.put("fooBar", "fooBarVal"); + resourceProps.put("other", "otherVal"); + + Request request = new InstanceRequest(resourceProps); + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + // no filtering should occur for entity instances + assertEquals(entityDefinition.filterProperties(request, resourceProps), resourceProps); + } + + @Test + public void testFilterProperties_Collection() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "nameVal"); + resourceProps.put("type", "someType"); + resourceProps.put("foo", "fooVal"); + resourceProps.put("bar", "barVal"); + resourceProps.put("fooBar", "fooBarVal"); + resourceProps.put("other", "otherVal"); + + Request request = new CollectionRequest(resourceProps, "someProperty:someValue"); + request.addAdditionalSelectProperties(Collections.singleton("foo")); + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + // no filtering should occur for entity instances + Map<String, Object> filteredProps = entityDefinition.filterProperties(request, resourceProps); + assertEquals(filteredProps.size(), 4); + // registered collection props + assertTrue(filteredProps.containsKey("name")); + assertTrue(filteredProps.containsKey("id")); + assertTrue(filteredProps.containsKey("type")); + + // added prop + assertTrue(filteredProps.containsKey("foo")); + } + + @Test + public void testGetProjections() { + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + Map<String, Projection> projections = entityDefinition.getProjections(); + assertEquals(projections.size(), 3); + assertTrue(projections.containsKey("tags")); + assertTrue(projections.containsKey("traits")); + assertTrue(projections.containsKey("default")); + } + + @Test + public void testGetRelations() { + ResourceDefinition entityDefinition = new EntityResourceDefinition(); + Map<String, Relation> relations = entityDefinition.getRelations(); + assertEquals(relations.size(), 2); + assertTrue(relations.containsKey("tags")); + assertTrue(relations.containsKey("traits")); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/definition/EntityTagResourceDefinitionTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/definition/EntityTagResourceDefinitionTest.java b/catalog/src/test/java/org/apache/atlas/catalog/definition/EntityTagResourceDefinitionTest.java new file mode 100644 index 0000000..954262f --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/definition/EntityTagResourceDefinitionTest.java @@ -0,0 +1,177 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog.definition; + +import org.apache.atlas.catalog.CollectionRequest; +import org.apache.atlas.catalog.InstanceRequest; +import org.apache.atlas.catalog.Request; +import org.apache.atlas.catalog.exception.InvalidPayloadException; +import org.apache.atlas.catalog.projection.Projection; +import org.apache.atlas.catalog.projection.Relation; +import org.apache.atlas.typesystem.types.AttributeDefinition; +import org.testng.annotations.Test; + +import java.util.*; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * Unit tests for EntityTagResourceDefinition. + */ +public class EntityTagResourceDefinitionTest { + @Test + public void testGetIdPropertyName() { + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + assertEquals(entityTagDefinition.getIdPropertyName(), "name"); + } + + @Test + public void testGetTypeName() { + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + assertNull(entityTagDefinition.getTypeName()); + } + + @Test + public void testResolveHref() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("name", "taxonomy1.term1.term11"); + resourceProps.put(EntityTagResourceDefinition.ENTITY_GUID_PROPERTY, "11-22-33"); + + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + String href = entityTagDefinition.resolveHref(resourceProps); + assertEquals(href, "v1/entities/11-22-33/tags/taxonomy1.term1.term11"); + } + + @Test + public void testValidate() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("name", "taxonomy1.termName"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + entityTagDefinition.validate(request); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testValidate_missingName() throws Exception { + Map<String, Object> properties = new HashMap<>(); + + Request request = new InstanceRequest(properties); + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + entityTagDefinition.validate(request); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testValidate_invalidProperty() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("name", "foo"); + properties.put("description", "desc"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + entityTagDefinition.validate(request); + } + + @Test + public void testGetPropertyDefinitions() { + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + Collection<AttributeDefinition> propertyDefinitions = entityTagDefinition.getPropertyDefinitions(); + + assertEquals(propertyDefinitions.size(), 1); + Set<String> defNames = new HashSet<>(); + for (AttributeDefinition def : propertyDefinitions) { + defNames.add(def.name); + } + assertTrue(defNames.contains("name")); + } + + @Test + public void testFilterProperties_Instance() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "nameVal"); + resourceProps.put("type", "someType"); + resourceProps.put("foo", "fooVal"); + resourceProps.put("bar", "barVal"); + resourceProps.put("description", "desc"); + resourceProps.put("creation_time", "2016:10:10"); + resourceProps.put("acceptable_use", "something"); + resourceProps.put("available_as_tag", true); + resourceProps.put("other", "otherVal"); + + Request request = new InstanceRequest(resourceProps); + request.addAdditionalSelectProperties(Collections.singleton("foo")); + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + + Map<String, Object> filteredProperties = entityTagDefinition.filterProperties(request, resourceProps); + assertEquals(filteredProperties.size(), 4); + // registered collection props + assertTrue(filteredProperties.containsKey("name")); + assertTrue(filteredProperties.containsKey("description")); + assertTrue(filteredProperties.containsKey("creation_time")); + // added prop + assertTrue(filteredProperties.containsKey("foo")); + } + + @Test + public void testFilterProperties_Collection() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "nameVal"); + resourceProps.put("type", "someType"); + resourceProps.put("foo", "fooVal"); + resourceProps.put("bar", "barVal"); + resourceProps.put("description", "desc"); + resourceProps.put("creation_time", "2016:10:10"); + resourceProps.put("acceptable_use", "something"); + resourceProps.put("available_as_tag", true); + resourceProps.put("other", "otherVal"); + + Request request = new CollectionRequest(resourceProps, "someProperty:someValue"); + request.addAdditionalSelectProperties(Collections.singleton("foo")); + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + + Map<String, Object> filteredProps = entityTagDefinition.filterProperties(request, resourceProps); + assertEquals(filteredProps.size(), 3); + // registered collection props + assertTrue(filteredProps.containsKey("name")); + assertTrue(filteredProps.containsKey("description")); + // added prop + assertTrue(filteredProps.containsKey("foo")); + } + + @Test + public void testGetProjections() { + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + Map<String, Projection> projections = entityTagDefinition.getProjections(); + assertEquals(projections.size(), 1); + assertTrue(projections.containsKey("terms")); + } + + @Test + public void testGetRelations() { + ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition(); + Map<String, Relation> relations = entityTagDefinition.getRelations(); + assertTrue(relations.isEmpty()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/definition/TaxonomyResourceDefinitionTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/definition/TaxonomyResourceDefinitionTest.java b/catalog/src/test/java/org/apache/atlas/catalog/definition/TaxonomyResourceDefinitionTest.java new file mode 100644 index 0000000..bc6f74c --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/definition/TaxonomyResourceDefinitionTest.java @@ -0,0 +1,174 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog.definition; + +import org.apache.atlas.catalog.CollectionRequest; +import org.apache.atlas.catalog.InstanceRequest; +import org.apache.atlas.catalog.Request; +import org.apache.atlas.catalog.exception.InvalidPayloadException; +import org.apache.atlas.catalog.projection.Projection; +import org.apache.atlas.catalog.projection.Relation; +import org.apache.atlas.typesystem.types.AttributeDefinition; +import org.testng.annotations.Test; + +import java.util.*; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +/** + * Unit tests for TaxonomyResourceDefinition. + */ +public class TaxonomyResourceDefinitionTest { + @Test + public void testGetIdPropertyName() { + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + assertEquals(taxonomyDefinition.getIdPropertyName(), "name"); + } + + @Test + public void testGetTypeName() { + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + assertEquals(taxonomyDefinition.getTypeName(), "Taxonomy"); + } + + @Test + public void testResolveHref() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "foo"); + + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + String href = taxonomyDefinition.resolveHref(resourceProps); + assertEquals(href, "v1/taxonomies/foo"); + } + + @Test + public void testValidate() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("name", "taxonomyName"); + properties.put("description", "foo"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + taxonomyDefinition.validate(request); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testValidate_missingName() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("description", "foo"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + taxonomyDefinition.validate(request); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testValidate_invalidProperty() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("name", "foo"); + properties.put("unknownProperty", "value"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + taxonomyDefinition.validate(request); + } + + @Test + public void testGetPropertyDefinitions() { + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + Collection<AttributeDefinition> propertyDefinitions = taxonomyDefinition.getPropertyDefinitions(); + + assertEquals(propertyDefinitions.size(), 2); + Set<String> defNames = new HashSet<>(); + for (AttributeDefinition def : propertyDefinitions) { + defNames.add(def.name); + } + assertTrue(defNames.contains("name")); + assertTrue(defNames.contains("description")); + } + + @Test + public void testFilterProperties_Instance() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "nameVal"); + resourceProps.put("type", "someType"); + resourceProps.put("foo", "fooVal"); + resourceProps.put("bar", "barVal"); + resourceProps.put("description", "desc"); + resourceProps.put("creation_time", "2016:10:10"); + + Request request = new InstanceRequest(resourceProps); + request.addAdditionalSelectProperties(Collections.singleton("foo")); + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + + Map<String, Object> filteredProperties = taxonomyDefinition.filterProperties(request, resourceProps); + assertEquals(filteredProperties.size(), 4); + // registered collection props + assertTrue(filteredProperties.containsKey("name")); + assertTrue(filteredProperties.containsKey("description")); + assertTrue(filteredProperties.containsKey("creation_time")); + // added prop + assertTrue(filteredProperties.containsKey("foo")); + } + + @Test + public void testFilterProperties_Collection() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "nameVal"); + resourceProps.put("type", "someType"); + resourceProps.put("foo", "fooVal"); + resourceProps.put("bar", "barVal"); + resourceProps.put("description", "desc"); + resourceProps.put("creation_time", "2016:10:10"); + + Request request = new CollectionRequest(resourceProps, "someProperty:someValue"); + request.addAdditionalSelectProperties(Collections.singleton("foo")); + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + + Map<String, Object> filteredProps = taxonomyDefinition.filterProperties(request, resourceProps); + assertEquals(filteredProps.size(), 3); + // registered collection props + assertTrue(filteredProps.containsKey("name")); + assertTrue(filteredProps.containsKey("description")); + // added prop + assertTrue(filteredProps.containsKey("foo")); + } + + @Test + public void testGetProjections() { + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + Map<String, Projection> projections = taxonomyDefinition.getProjections(); + assertEquals(projections.size(), 1); + assertTrue(projections.containsKey("terms")); + } + + @Test + public void testGetRelations() { + ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition(); + Map<String, Relation> relations = taxonomyDefinition.getRelations(); + assertTrue(relations.isEmpty()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/aaf2971a/catalog/src/test/java/org/apache/atlas/catalog/definition/TermResourceDefinitionTest.java ---------------------------------------------------------------------- diff --git a/catalog/src/test/java/org/apache/atlas/catalog/definition/TermResourceDefinitionTest.java b/catalog/src/test/java/org/apache/atlas/catalog/definition/TermResourceDefinitionTest.java new file mode 100644 index 0000000..52deadf --- /dev/null +++ b/catalog/src/test/java/org/apache/atlas/catalog/definition/TermResourceDefinitionTest.java @@ -0,0 +1,210 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.catalog.definition; + +import org.apache.atlas.catalog.CollectionRequest; +import org.apache.atlas.catalog.InstanceRequest; +import org.apache.atlas.catalog.Request; +import org.apache.atlas.catalog.exception.InvalidPayloadException; +import org.apache.atlas.catalog.projection.Projection; +import org.apache.atlas.catalog.projection.Relation; +import org.apache.atlas.typesystem.types.AttributeDefinition; +import org.testng.annotations.Test; + +import java.util.*; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +/** + * Unit tests for TermResourceDefinition. + */ +public class TermResourceDefinitionTest { + @Test + public void testGetIdPropertyName() { + ResourceDefinition termDefinition = new TermResourceDefinition(); + assertEquals(termDefinition.getIdPropertyName(), "name"); + } + + @Test + public void testGetTypeName() { + ResourceDefinition termDefinition = new TermResourceDefinition(); + assertEquals(termDefinition.getTypeName(), "Term"); + } + + @Test + public void testResolveHref() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("name", "taxonomy1.term1.term11"); + + ResourceDefinition termDefinition = new TermResourceDefinition(); + String href = termDefinition.resolveHref(resourceProps); + assertEquals(href, "v1/taxonomies/taxonomy1/terms/term1/terms/term11"); + } + + @Test + public void testValidate() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("name", "taxonomy1.termName"); + properties.put("description", "foo"); + properties.put("available_as_tag", true); + properties.put("acceptable_use", "something"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition termDefinition = new TermResourceDefinition(); + termDefinition.validate(request); + } + + @Test + public void testValidate_nameOnly() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("name", "taxonomy1.termName"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition termDefinition = new TermResourceDefinition(); + termDefinition.validate(request); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testValidate_invalidTermName() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("name", "NotQualifiedTermName"); + properties.put("description", "foo"); + properties.put("available_as_tag", true); + + Request request = new InstanceRequest(properties); + + ResourceDefinition termDefinition = new TermResourceDefinition(); + termDefinition.validate(request); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testValidate_missingName() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("description", "foo"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition termDefinition = new TermResourceDefinition(); + termDefinition.validate(request); + } + + @Test(expectedExceptions = InvalidPayloadException.class) + public void testValidate_invalidProperty() throws Exception { + Map<String, Object> properties = new HashMap<>(); + properties.put("name", "foo"); + properties.put("unknownProperty", "value"); + + Request request = new InstanceRequest(properties); + + ResourceDefinition termDefinition = new TermResourceDefinition(); + termDefinition.validate(request); + } + + @Test + public void testGetPropertyDefinitions() { + ResourceDefinition termDefinition = new TermResourceDefinition(); + Collection<AttributeDefinition> propertyDefinitions = termDefinition.getPropertyDefinitions(); + + assertEquals(propertyDefinitions.size(), 4); + Set<String> defNames = new HashSet<>(); + for (AttributeDefinition def : propertyDefinitions) { + defNames.add(def.name); + } + assertTrue(defNames.contains("name")); + assertTrue(defNames.contains("description")); + assertTrue(defNames.contains("available_as_tag")); + assertTrue(defNames.contains("acceptable_use")); + } + + @Test + public void testFilterProperties_Instance() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "nameVal"); + resourceProps.put("type", "someType"); + resourceProps.put("foo", "fooVal"); + resourceProps.put("bar", "barVal"); + resourceProps.put("description", "desc"); + resourceProps.put("creation_time", "2016:10:10"); + resourceProps.put("acceptable_use", "something"); + resourceProps.put("available_as_tag", true); + resourceProps.put("other", "otherVal"); + + Request request = new InstanceRequest(resourceProps); + request.addAdditionalSelectProperties(Collections.singleton("foo")); + ResourceDefinition termDefinition = new TermResourceDefinition(); + + Map<String, Object> filteredProperties = termDefinition.filterProperties(request, resourceProps); + assertEquals(filteredProperties.size(), 6); + // registered collection props + assertTrue(filteredProperties.containsKey("name")); + assertTrue(filteredProperties.containsKey("description")); + assertTrue(filteredProperties.containsKey("available_as_tag")); + assertTrue(filteredProperties.containsKey("acceptable_use")); + assertTrue(filteredProperties.containsKey("creation_time")); + // added prop + assertTrue(filteredProperties.containsKey("foo")); + } + + @Test + public void testFilterProperties_Collection() { + Map<String, Object> resourceProps = new HashMap<>(); + resourceProps.put("id", "111-222-333"); + resourceProps.put("name", "nameVal"); + resourceProps.put("type", "someType"); + resourceProps.put("foo", "fooVal"); + resourceProps.put("bar", "barVal"); + resourceProps.put("description", "desc"); + resourceProps.put("creation_time", "2016:10:10"); + resourceProps.put("acceptable_use", "something"); + resourceProps.put("available_as_tag", true); + resourceProps.put("other", "otherVal"); + + Request request = new CollectionRequest(resourceProps, "someProperty:someValue"); + request.addAdditionalSelectProperties(Collections.singleton("foo")); + ResourceDefinition termDefinition = new TermResourceDefinition(); + + Map<String, Object> filteredProps = termDefinition.filterProperties(request, resourceProps); + assertEquals(filteredProps.size(), 3); + // registered collection props + assertTrue(filteredProps.containsKey("name")); + assertTrue(filteredProps.containsKey("description")); + // added prop + assertTrue(filteredProps.containsKey("foo")); + } + + @Test + public void testGetProjections() { + ResourceDefinition termDefinition = new TermResourceDefinition(); + Map<String, Projection> projections = termDefinition.getProjections(); + assertEquals(projections.size(), 2); + assertTrue(projections.containsKey("terms")); + assertTrue(projections.containsKey("hierarchy")); + } + + @Test + public void testGetRelations() { + ResourceDefinition termDefinition = new TermResourceDefinition(); + Map<String, Relation> relations = termDefinition.getRelations(); + assertTrue(relations.isEmpty()); + } +}
