From: aditya Date: Wed, 3 Jan 2018 23:25:58 +0000 (-0600) Subject: Add unit tests to increase sonar coverage X-Git-Tag: v1.2.0~27 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fdata-router.git;a=commitdiff_plain;h=c1bbee3c6a59422f94e4d2bc2c61b0fe203745bf Add unit tests to increase sonar coverage Add unit tests to entity, exception, search and util packages Issue-ID: AAI-499 Change-Id: I8e2ca9055f97ac2db16c256d4d4ae417a158e66c Signed-off-by: Aditya Gajulapalli --- diff --git a/src/test/java/org/onap/aai/datarouter/entity/AaiEventEntityTest.java b/src/test/java/org/onap/aai/datarouter/entity/AaiEventEntityTest.java new file mode 100644 index 0000000..413fd74 --- /dev/null +++ b/src/test/java/org/onap/aai/datarouter/entity/AaiEventEntityTest.java @@ -0,0 +1,166 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.datarouter.entity; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.aai.datarouter.util.CrossEntityReference; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class AaiEventEntityTest { + + @Test + public void testAddCrossEntityReferenceValue(){ + AaiEventEntity eventEntity = new AaiEventEntity(); + Assert.assertTrue(eventEntity.crossEntityReferenceCollection.size()==0); + eventEntity.addCrossEntityReferenceValue("reference-1"); + Assert.assertTrue(eventEntity.crossEntityReferenceCollection.size()==1); + } + + @Test + public void testOtherMethods(){ + AaiEventEntity eventEntity = new AaiEventEntity(); + eventEntity.setEntityType("type-1"); + Assert.assertEquals(eventEntity.getEntityType(), "type-1"); + + eventEntity.setSearchTagCollection(new ArrayList()); + Assert.assertTrue(eventEntity.getSearchTagCollection().size()==0); + + eventEntity.setSearchTags("tag-1"); + Assert.assertEquals(eventEntity.getSearchTags(), "tag-1"); + + eventEntity.setSearchTagIDs("tagid-1"); + Assert.assertEquals(eventEntity.getSearchTagIDs(), "tagid-1"); + + eventEntity.setId("id-1"); + Assert.assertEquals(eventEntity.getId(), "id-1"); + + eventEntity.setSearchTagIdCollection(new ArrayList()); + Assert.assertTrue(eventEntity.getSearchTagIdCollection().size()==0); + + eventEntity.setLastmodTimestamp("2017-28-12"); + Assert.assertEquals(eventEntity.getLastmodTimestamp(), "2017-28-12"); + + eventEntity.setLink("link-1"); + Assert.assertEquals(eventEntity.getLink(), "link-1"); + + eventEntity.setCrossReferenceEntityValues("cross-entity"); + Assert.assertEquals(eventEntity.getCrossReferenceEntityValues(), "cross-entity"); + + Assert.assertNotNull(eventEntity.toString()); + } + + @Test + public void testAggregationEntity(){ + AggregationEntity entity = new AggregationEntity(); + entity.setLastmodTimestamp("2017-28-12"); + Assert.assertEquals(entity.getLastmodTimestamp(), "2017-28-12"); + + entity.addAttributeKeyValuePair("key1", "value1"); + + Map entityMap = new HashMap(); + entityMap.put("relationship", "relationship-value"); + entityMap.put("relationship-list", new ArrayList()); + entity.copyAttributeKeyValuePair(entityMap); + + Assert.assertTrue(entity.attributes.containsKey("relationship")); + Assert.assertFalse(entity.attributes.containsKey("relationship-list")); + + entity.setId("id-1"); + entity.setLink("link-1"); + Assert.assertNotNull(entity.toString()); + } + + @Test + public void testOxmEntityDescriptor(){ + OxmEntityDescriptor descriptor = new OxmEntityDescriptor(); + descriptor.setEntityName("entity-1"); + Assert.assertEquals(descriptor.getEntityName(), "entity-1"); + + descriptor.setPrimaryKeyAttributeName(new ArrayList()); + Assert.assertTrue(descriptor.getPrimaryKeyAttributeName().size()==0); + + Assert.assertFalse(descriptor.hasSearchableAttributes()); + + List searchableAttr = new ArrayList(); + searchableAttr.add("search"); + descriptor.setSearchableAttributes(searchableAttr); + Assert.assertTrue(descriptor.getSearchableAttributes().size()==1); + + Assert.assertTrue(descriptor.hasSearchableAttributes()); + + CrossEntityReference ref = new CrossEntityReference(); + descriptor.setCrossEntityReference(ref); + Assert.assertEquals(descriptor.getCrossEntityReference(), ref); + + descriptor.setSuggestableEntity(true); + Assert.assertTrue(descriptor.isSuggestableEntity()); + + Assert.assertNotNull(descriptor.toString()); + } + + @Test + public void testPolicyResponse(){ + PolicyResponse response = new PolicyResponse(PolicyResponse.ResponseType.SUCCESS, "response-data"); + + Assert.assertEquals(response.getResponseType(), PolicyResponse.ResponseType.SUCCESS); + Assert.assertEquals(response.getResponseData(), "response-data"); + + response.setHttpResponseCode(200); + Assert.assertTrue(response.getHttpResponseCode() == 200); + + Assert.assertNotNull(response.toString()); + } + + @Test + public void testTopographicalEntity() throws NoSuchAlgorithmException, IOException { + TopographicalEntity entity = new TopographicalEntity(); + String retStr = entity.generateUniqueShaDigest("entity", "name-1", "value-1"); + Assert.assertNotNull(retStr); + + entity.setEntityType("type-1"); + Assert.assertEquals(entity.getEntityType(),"type-1"); + entity.setEntityPrimaryKeyName("key-1"); + Assert.assertEquals(entity.getEntityPrimaryKeyName(), "key-1"); + entity.setEntityPrimaryKeyValue("value-1"); + Assert.assertEquals(entity.getEntityPrimaryKeyValue(),"value-1"); + entity.setLatitude("37.0902"); + Assert.assertEquals(entity.getLatitude(),"37.0902"); + entity.setLongitude("95.7129"); + Assert.assertEquals(entity.getLongitude(),"95.7129"); + entity.setId("id-1"); + Assert.assertEquals(entity.getId(),"id-1"); + entity.setSelfLink("link-1"); + Assert.assertEquals(entity.getSelfLink(),"link-1"); + + Assert.assertNotNull(TopographicalEntity.getSerialversionuid()); + Assert.assertNotNull(entity.getAsJson()); + Assert.assertNotNull(entity.toString()); + } +} diff --git a/src/test/java/org/onap/aai/datarouter/exception/DataRouterExceptionTest.java b/src/test/java/org/onap/aai/datarouter/exception/DataRouterExceptionTest.java new file mode 100644 index 0000000..ecf845c --- /dev/null +++ b/src/test/java/org/onap/aai/datarouter/exception/DataRouterExceptionTest.java @@ -0,0 +1,51 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.datarouter.exception; + +import org.junit.Assert; +import org.junit.Test; + +import javax.ws.rs.core.Response; + +public class DataRouterExceptionTest { + + @Test + public void testDataRouterError(){ + DataRouterError error1 = DataRouterError.DL_PARSE_100; + Assert.assertEquals("DL-100", error1.getId()); + Assert.assertNotNull(error1.getMessage()); + Assert.assertEquals(Response.Status.BAD_REQUEST, error1.getHttpStatus()); + } + + @Test + public void testBaseDataRouterException(){ + BaseDataRouterException exp1 = new BaseDataRouterException("id-1"); + Assert.assertEquals(exp1.getId(), "id-1"); + + BaseDataRouterException exp2 = new BaseDataRouterException("id-1", "test-error"); + Assert.assertEquals(exp2.getId(), "id-1"); + + BaseDataRouterException exp3 = new BaseDataRouterException("id-1", "test-error", new Throwable()); + Assert.assertEquals(exp3.getId(), "id-1"); + } +} diff --git a/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyConfigTest.java b/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyConfigTest.java new file mode 100644 index 0000000..7324071 --- /dev/null +++ b/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyConfigTest.java @@ -0,0 +1,69 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.datarouter.policy; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class EntityEventPolicyConfigTest { + + @Test + public void testAllMethods() { + EntityEventPolicyConfig eConfig = new EntityEventPolicyConfig(); + eConfig.setSourceDomain("source-domain"); + Assert.assertEquals(eConfig.getSourceDomain(),"source-domain"); + + eConfig.setSearchBaseUrl("http://base-url"); + Assert.assertEquals(eConfig.getSearchBaseUrl(),"http://base-url"); + + eConfig.setSearchEndpoint("end-point"); + Assert.assertEquals(eConfig.getSearchEndpoint(),"end-point"); + + eConfig.setSearchEndpointDocuments("end-document"); + Assert.assertEquals(eConfig.getSearchEndpointDocuments(),"end-document"); + + eConfig.setSearchEntitySearchIndex("search-index"); + Assert.assertEquals(eConfig.getSearchEntitySearchIndex(),"search-index"); + + eConfig.setSearchTopographySearchIndex("topography"); + Assert.assertEquals(eConfig.getSearchTopographySearchIndex(),"topography"); + + eConfig.setSearchEntityAutoSuggestIndex("auto-suggest"); + Assert.assertEquals(eConfig.getSearchEntityAutoSuggestIndex(),"auto-suggest"); + + eConfig.setSearchCertName("cert-name"); + Assert.assertEquals(eConfig.getSearchCertName(),"cert-name"); + + eConfig.setSearchKeystore("key-store"); + Assert.assertEquals(eConfig.getSearchKeystore(),"key-store"); + + eConfig.setSearchKeystorePwd("key-store-pass"); + Assert.assertEquals(eConfig.getSearchKeystorePwd(),"key-store-pass"); + + eConfig.setSearchAggregationVnfIndex("vnf-index"); + Assert.assertEquals(eConfig.getSearchAggregationVnfIndex(),"vnf-index"); + } + +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/datarouter/search/filters/config/FiltersConfigTest.java b/src/test/java/org/onap/aai/datarouter/search/filters/config/FiltersConfigTest.java new file mode 100644 index 0000000..073e306 --- /dev/null +++ b/src/test/java/org/onap/aai/datarouter/search/filters/config/FiltersConfigTest.java @@ -0,0 +1,80 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.datarouter.search.filters.config; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class FiltersConfigTest { + + @Test + public void testUiFiltersConfig_AllMethods(){ + + UiFilterDataSourceConfig dsConfig = getDataSourceConfig(); + UiFilterConfig filterConfig = getFilterConfig(dsConfig); + + List filters = new ArrayList(); + filters.add(filterConfig); + UiFiltersConfig config1 = new UiFiltersConfig(filters); + Assert.assertNotNull(config1); + UiFiltersConfig config2 = new UiFiltersConfig(); + config2.setFilters(filters); + Assert.assertTrue(config2.getFilters().size()==1); + Assert.assertNotNull(config2.toString()); + } + + private UiFilterConfig getFilterConfig(UiFilterDataSourceConfig dsConfig) { + UiFilterConfig filterConfig = new UiFilterConfig("id-1", "name-1", + "display-1", "dataType-1", dsConfig); + filterConfig.setFilterId("id-1"); + Assert.assertEquals("id-1", filterConfig.getFilterId()); + filterConfig.setFilterName("name-1"); + Assert.assertEquals("name-1", filterConfig.getFilterName()); + filterConfig.setDisplayName("display-1"); + Assert.assertEquals("display-1", filterConfig.getDisplayName()); + filterConfig.setDataType("dataType-1"); + Assert.assertEquals("dataType-1", filterConfig.getDataType()); + filterConfig.setDataSource(dsConfig); + Assert.assertEquals(dsConfig, filterConfig.getDataSource()); + Assert.assertNotNull(filterConfig.toString()); + return filterConfig; + } + + private UiFilterDataSourceConfig getDataSourceConfig() { + UiFilterDataSourceConfig dsConfig = new UiFilterDataSourceConfig("index-1", "doctype-1", + "field-1", "path-1"); + dsConfig.setIndexName("index-1"); + Assert.assertEquals("index-1", dsConfig.getIndexName()); + dsConfig.setDocType("doctype-1"); + Assert.assertEquals("doctype-1", dsConfig.getDocType()); + dsConfig.setFieldName("field-1"); + Assert.assertEquals("field-1", dsConfig.getFieldName()); + dsConfig.setPathToField("path-1"); + Assert.assertEquals("path-1", dsConfig.getPathToField()); + Assert.assertNotNull(dsConfig.toString()); + return dsConfig; + } +} diff --git a/src/test/java/org/onap/aai/datarouter/util/CrossEntityReferenceTest.java b/src/test/java/org/onap/aai/datarouter/util/CrossEntityReferenceTest.java new file mode 100644 index 0000000..79a3141 --- /dev/null +++ b/src/test/java/org/onap/aai/datarouter/util/CrossEntityReferenceTest.java @@ -0,0 +1,51 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.datarouter.util; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +public class CrossEntityReferenceTest { + + @Test + public void testCrossEntityReference() { + + CrossEntityReference reference = new CrossEntityReference(); + + reference.setTargetEntityType("entity-type"); + Assert.assertEquals(reference.getTargetEntityType(),"entity-type"); + + reference.setAttributeNames(new ArrayList()); + Assert.assertTrue(reference.getAttributeNames().size()==0); + + reference.addAttributeName("attribute"); + Assert.assertEquals(reference.getAttributeNames().get(0),"attribute"); + } + + +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/datarouter/util/DataRouterPropertiesTest.java b/src/test/java/org/onap/aai/datarouter/util/DataRouterPropertiesTest.java new file mode 100644 index 0000000..84616d4 --- /dev/null +++ b/src/test/java/org/onap/aai/datarouter/util/DataRouterPropertiesTest.java @@ -0,0 +1,35 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.datarouter.util; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class DataRouterPropertiesTest { + + @Test + public void testGet(){ + DataRouterProperties.get("key"); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/datarouter/util/NodeUtilsTest.java b/src/test/java/org/onap/aai/datarouter/util/NodeUtilsTest.java new file mode 100644 index 0000000..19af9eb --- /dev/null +++ b/src/test/java/org/onap/aai/datarouter/util/NodeUtilsTest.java @@ -0,0 +1,100 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.datarouter.util; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static org.junit.Assert.*; + +public class NodeUtilsTest { + + @Test + public void testGenerateUniqueShaDigest_NullParam(){ + NodeUtils utils = new NodeUtils(); + String keys=null; + String hashdId = NodeUtils.generateUniqueShaDigest(); + Assert.assertNull(hashdId); + } + + @Test + public void testExtractFieldValueFromObject_NullNode_ExpectNullString(){ + JsonNode node = null; + String valueNode = NodeUtils.extractFieldValueFromObject(node, "field-1"); + Assert.assertNull(valueNode); + } + + @Test + public void testExtractFieldValueFromObject_NotNullNode_ExpectNotNullString() throws IOException { + String jsonStr = "{\"key\":\"value\"}"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(jsonStr); + String valueNode = NodeUtils.extractFieldValueFromObject(node, "key"); + Assert.assertEquals(valueNode, "value"); + } + + @Test + public void testConvertJsonStrToJsonNode_NullJsonStr() throws IOException { + String jsonStr = null; + JsonNode jsonNode = NodeUtils.convertJsonStrToJsonNode(jsonStr); + Assert.assertNull(jsonNode); + } + + @Test + public void testExtractObjectsByKey() throws IOException { + String jsonStr = "{\n" + + " \"id\" : 1,\n" + + " \"name\" : {\n" + + " \"first\" : \"user\",\n" + + " \"last\" : \"name\"\n" + + " },\n" + + " \"contact\" : [\n" + + " { \"type\" : \"phone/home\", \"ref\" : \"111-111-1234\"},\n" + + " { \"type\" : \"phone/work\", \"ref\" : \"222-222-2222\"}\n" + + " ]\n" + + "}"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(jsonStr); + List arrList = new ArrayList(); + NodeUtils.extractObjectsByKey(node, "name", arrList); + } + + @Test + public void testConvertObjectToJson() throws IOException { + String jsonStr = "{\"key\":\"value\"}"; + ObjectMapper mapper = new ObjectMapper(); + Object node = mapper.readTree(jsonStr); + String retNode1 = NodeUtils.convertObjectToJson(node, false); + Assert.assertNotNull(retNode1); + String retNode2 = NodeUtils.convertObjectToJson(node, true); + Assert.assertNotNull(retNode2); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/datarouter/util/RouterServiceUtilTest.java b/src/test/java/org/onap/aai/datarouter/util/RouterServiceUtilTest.java new file mode 100644 index 0000000..a49427e --- /dev/null +++ b/src/test/java/org/onap/aai/datarouter/util/RouterServiceUtilTest.java @@ -0,0 +1,143 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.aai.datarouter.util; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.junit.Test; + +import javax.json.Json; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class RouterServiceUtilTest { + + @Test + public void testParseJsonPayloadIntoMap(){ + String jsonStr = "{\"key1\":\"value1\", \"key2\":\"value2\", \"key3\":\"value3\"}"; + Map retMap = RouterServiceUtil.parseJsonPayloadIntoMap(jsonStr); + Assert.assertNotNull(retMap); + Assert.assertEquals(retMap.get("key1"), "value1"); + } + + @Test + public void testGetNodeFieldAsText() throws IOException { + String jsonStr = "{\"key\":\"value\"}"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(jsonStr); + String field = RouterServiceUtil.getNodeFieldAsText(node, "key"); + } + + @Test + public void testConcatArray_NullList(){ + List strList = null; + String result = RouterServiceUtil.concatArray(strList); + Assert.assertTrue(result.isEmpty()); + } + + /*Method in RouterServiceUtil is wrong. It should be corrected + @Test + public void testConcatArray_NotNullList(){ + List strList = new ArrayList(); + strList.add("str1"); + strList.add("str2"); + strList.add("str3"); + String result = RouterServiceUtil.concatArray(strList); + Assert.assertEquals(result, "str1 str2 str3"); + }*/ + + @Test + public void testConcatArray_NullArray(){ + String[] values = new String[]{}; + //String[] values = new String[]{"value1", "value2", "value3"}; + String result = RouterServiceUtil.concatArray(values); + Assert.assertTrue(result.isEmpty()); + } + + @Test + public void testConcatArray_NotNullArray(){ + String[] values = new String[]{"value1", "value2", "value3"}; + String result = RouterServiceUtil.concatArray(values); + Assert.assertEquals(result, "value1.value2.value3"); + } + + @Test + public void testRecursivelyLookupJsonPayload_JsonArray() throws IOException { + String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(jsonStr); + String result = RouterServiceUtil.recursivelyLookupJsonPayload(node, "key1"); + Assert.assertEquals(result, "value1"); + } + + @Test + public void testExtractObjectsByKey() throws IOException { + String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(jsonStr); + List nodeList = new ArrayList(); + RouterServiceUtil.extractObjectsByKey(node, "key1", nodeList); + Assert.assertTrue(nodeList.size() == 1); + } + + @Test + public void testConvertArrayIntoList() throws IOException { + String jsonStr = "[{\"key1\":\"value1\"}, {\"key2\":\"value2\"}]"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(jsonStr); + List nodeList = new ArrayList(); + RouterServiceUtil.convertArrayIntoList(node, nodeList); + Assert.assertTrue(nodeList.size() == 2); + } + + @Test + public void testExtractFieldValueFromObject() throws IOException { + String jsonStr = "{\"key1\":\"value1\"}"; + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(jsonStr); + List attrList = new ArrayList(); + attrList.add("key1"); + List valueList = new ArrayList(); + RouterServiceUtil.extractFieldValuesFromObject(node,attrList,valueList); + Assert.assertTrue(valueList.size()==1); + Assert.assertTrue(valueList.get(0).equals("value1")); + } + + @Test + public void testObjToJson() throws IOException { + String jsonStr = "{\"key1\":\"value1\"}"; + ObjectMapper mapper = new ObjectMapper(); + Object node = mapper.readTree(jsonStr); + String retJson = RouterServiceUtil.objToJson(node); + Assert.assertNotNull("localhost/aai/data-router/"); + } + + @Test + public void testConcatSubUri(){ + String finalSubUri = RouterServiceUtil.concatSubUri("localhost/","aai/","data-router"); + Assert.assertEquals(finalSubUri, "localhost/aai/data-router/"); + } +}