diff options
author | Krysiak Adam <adam.krysiak@nokia.com> | 2019-01-29 15:59:00 +0100 |
---|---|---|
committer | Krysiak Adam <adam.krysiak@nokia.com> | 2019-02-06 11:55:09 +0100 |
commit | cdfe48cf32c83f3e0a3921499cd6c494a4b40107 (patch) | |
tree | 815500b03910baf57c4487e3902b5bc30057ec90 /src/test/java | |
parent | 3e118724141917299ad3e2f535544500b5c459b0 (diff) |
Replace jackson usages with GSON
Issue-ID: CLAMP-286
Change-Id: I3492811e248cbcf502656a992cea4f76deed46dc
Signed-off-by: Krysiak Adam <adam.krysiak@nokia.com>
Diffstat (limited to 'src/test/java')
18 files changed, 476 insertions, 213 deletions
diff --git a/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java b/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java index c828f78a9..1b01f544b 100644 --- a/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java +++ b/src/test/java/org/onap/clamp/clds/client/DcaeDispatcherServicesTest.java @@ -23,9 +23,8 @@ package org.onap.clamp.clds.client; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableMap; +import com.google.gson.JsonObject; import java.io.IOException; import org.assertj.core.api.Assertions; import org.junit.Before; @@ -61,9 +60,13 @@ public class DcaeDispatcherServicesTest { @InjectMocks DcaeDispatcherServices dcaeDispatcherServices; - private final String STATUS_RESPONSE_PROCESSING = "{\"operationType\": \"deploy\",\"status\": \"processing\"}"; - private final String STATUS_RESPONSE_ACTIVE = "{\"operationType\": \"deploy\",\"status\": \"succeeded\"}"; + private static final String STATUS_RESPONSE_PROCESSING = "{\"operationType\": \"deploy\"," + + "\"status\": \"processing\"}"; + private static final String STATUS_RESPONSE_ACTIVE = "{\"operationType\": \"deploy\",\"status\": \"succeeded\"}"; + /** + * Setup method. + */ @Before public void setUp() { ImmutableMap.<String, String>builder() @@ -126,10 +129,10 @@ public class DcaeDispatcherServicesTest { @Test public void shouldTriggerDeploymentCreation() throws IOException { //given - String deploymentID = "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId"; + String deploymentId = "closedLoop_152367c8-b172-47b3-9e58-c53add75d869_deploymentId"; String serviceTypeId = "e2ba40f7-bf42-41e7-acd7-48fd07586d90"; Mockito.when(clampProperties.getJsonTemplate("dcae.deployment.template")) - .thenReturn(new ObjectMapper().readTree("{}")); + .thenReturn(new JsonObject()); Mockito.when(dcaeHttpConnectionManager .doDcaeHttpQuery(DCAE_URL @@ -138,11 +141,11 @@ public class DcaeDispatcherServicesTest { "{\"serviceTypeId\":\"e2ba40f7-bf42-41e7-acd7-48fd07586d90\",\"inputs\":{}}", "application/json")) .thenReturn(DEPLOY_RESPONSE_STRING); - JsonNode blueprintInputJson = new ObjectMapper().readTree("{}"); + JsonObject blueprintInputJson = new JsonObject(); //when String operationStatus = dcaeDispatcherServices - .createNewDeployment(deploymentID, serviceTypeId, blueprintInputJson); + .createNewDeployment(deploymentId, serviceTypeId, blueprintInputJson); //then Assertions.assertThat(operationStatus).isEqualTo("http://deployment-handler.onap:8443/" diff --git a/src/test/java/org/onap/clamp/clds/client/req/policy/GuardPolicyAttributesConstructorTest.java b/src/test/java/org/onap/clamp/clds/client/req/policy/GuardPolicyAttributesConstructorTest.java index f16345676..317858012 100644 --- a/src/test/java/org/onap/clamp/clds/client/req/policy/GuardPolicyAttributesConstructorTest.java +++ b/src/test/java/org/onap/clamp/clds/client/req/policy/GuardPolicyAttributesConstructorTest.java @@ -44,6 +44,9 @@ public class GuardPolicyAttributesConstructorTest { private ModelProperties modelProperties; private List<PolicyChain> policyChains; + /** + * @throws Exception thrown if resources not found. + */ @Before public void setUp() throws Exception { String modelProp = ResourceFileUtil diff --git a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java index 293f0e625..4d78cf816 100644 --- a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java +++ b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java @@ -23,14 +23,11 @@ package org.onap.clamp.clds.client.req.policy; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableMap; - +import com.google.gson.JsonElement; import java.io.IOException; import java.net.URLDecoder; import java.util.Map; - import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Test; @@ -39,6 +36,7 @@ import org.mockito.Mockito; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.model.properties.ModelProperties; import org.onap.clamp.clds.model.properties.PolicyChain; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.ResourceFileUtil; import org.onap.policy.api.AttributeType; import org.onap.policy.controlloop.policy.ControlLoopPolicy; @@ -54,6 +52,9 @@ public class OperationalPolicyAttributesConstructorTest { private ModelProperties modelProperties; private PolicyChain policyChain; + /** + * @throws Exception thrown if resource not found. + */ @Before public void setUp() throws Exception { String modelProp = ResourceFileUtil @@ -140,7 +141,7 @@ public class OperationalPolicyAttributesConstructorTest { private PolicyChain readPolicyChainFromResources() throws IOException { String policyChainText = ResourceFileUtil .getResourceAsString("example/operational-policy/json-policy-chain.json"); - JsonNode policyChainNode = new ObjectMapper().readTree(policyChainText); + JsonElement policyChainNode = JsonUtils.GSON.fromJson(policyChainText, JsonElement.class); return new PolicyChain(policyChainNode); } } diff --git a/src/test/java/org/onap/clamp/clds/client/req/tca/TcaRequestFormatterTest.java b/src/test/java/org/onap/clamp/clds/client/req/tca/TcaRequestFormatterTest.java new file mode 100644 index 000000000..095df9d84 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/client/req/tca/TcaRequestFormatterTest.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.client.req.tca; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.gson.JsonObject; +import java.io.IOException; +import org.junit.Test; +import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.clds.model.properties.ModelProperties; +import org.onap.clamp.clds.model.properties.Tca; +import org.onap.clamp.clds.model.properties.TcaItem; +import org.onap.clamp.clds.util.JsonUtils; + +public class TcaRequestFormatterTest { + + private static final String TCA_POLICY_PROPERTIES_TEMPLATE = "{" + + " \"domain\": \"measurementsForVfScaling\"," + + " \"metricsPerEventName\": [" + + " {" + + " \"eventName\": \"???\"," + + " \"controlLoopSchemaType\": \"VNF\"," + + " \"policyScope\": \"DCAE\"," + + " \"policyName\": \"???\"," + + " \"policyVersion\": \"v0.0.1\"," + + " \"thresholds\": [" + + " ]" + + " }" + + " ]" + + "}"; + + @Test + public void shouldReturnFormattedTcaPolicyRequest() throws IOException { + //given + String service = "TestService"; + String policy = "TestService_scope.PolicyName"; + ClampProperties clampProperties = mock(ClampProperties.class); + String expectedRequestText = + "{ " + + " \"domain\": \"measurementsForVfScaling\", " + + " \"metricsPerEventName\": [ " + + " { " + + " \"eventName\": \"vLoadBalancer\", " + + " \"controlLoopSchemaType\": \"VNF\", " + + " \"policyScope\": \"DCAE\", " + + " \"policyName\": \"TestService_scope.PolicyName\", " + + " \"policyVersion\": \"v0.0.1\", " + + " \"thresholds\": [] " + + " } " + + " ] " + + "}"; + + JsonObject tcaPolicyPropertiesTemplate = JsonUtils.GSON + .fromJson(TCA_POLICY_PROPERTIES_TEMPLATE, JsonObject.class); + + JsonObject expectedRequest = JsonUtils.GSON.fromJson(expectedRequestText, JsonObject.class); + + ModelProperties modelProperties = mock(ModelProperties.class); + Tca tca = mock(Tca.class); + TcaItem tcaItem = mock(TcaItem.class); + when(clampProperties.getJsonTemplate(any(), any())).thenReturn(tcaPolicyPropertiesTemplate); + when(tca.getTcaItem()).thenReturn(tcaItem); + when(tcaItem.getEventName()).thenReturn("vLoadBalancer"); + when(tcaItem.getControlLoopSchemaType()).thenReturn("VNF"); + + //when + JsonObject policyContent = TcaRequestFormatter + .createPolicyContent(clampProperties, modelProperties, service, policy, tca); + + //then + assertThat(expectedRequest).isEqualTo(policyContent); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/clamp/clds/config/CldsUserJsonDecoderTest.java b/src/test/java/org/onap/clamp/clds/config/CldsUserJsonDecoderTest.java index c04357ded..a32a60351 100644 --- a/src/test/java/org/onap/clamp/clds/config/CldsUserJsonDecoderTest.java +++ b/src/test/java/org/onap/clamp/clds/config/CldsUserJsonDecoderTest.java @@ -25,10 +25,7 @@ package org.onap.clamp.clds.config; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import java.util.Collections; import org.junit.Test; import org.onap.clamp.clds.service.CldsUser; diff --git a/src/test/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfigurationTest.java b/src/test/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfigurationTest.java index 3f0a078bb..d27690bfa 100644 --- a/src/test/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfigurationTest.java +++ b/src/test/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfigurationTest.java @@ -29,13 +29,13 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - +import com.google.gson.JsonObject; import java.io.IOException; - +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import org.junit.Test; import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.ResourceFileUtil; /** @@ -43,13 +43,19 @@ import org.onap.clamp.clds.util.ResourceFileUtil; */ public class SdcSingleControllerConfigurationTest { + /** + * @param fileName file for sdc controller configuration. + * @param sdcControllerName sdc controller name. + * @return instance of SdcSingleControllerConfiguration. + */ public static SdcSingleControllerConfiguration loadControllerConfiguration(String fileName, - String sdcControllerName) throws IOException { - JsonNode jsonNode = new ObjectMapper().readValue(ResourceFileUtil.getResourceAsStream(fileName), - JsonNode.class); - SdcSingleControllerConfiguration sdcSingleControllerConfiguration = new SdcSingleControllerConfiguration( - jsonNode, sdcControllerName); - return sdcSingleControllerConfiguration; + String sdcControllerName) { + + InputStreamReader streamReader = new InputStreamReader(ResourceFileUtil.getResourceAsStream(fileName), + StandardCharsets.UTF_8); + JsonObject jsonNode = JsonUtils.GSON.fromJson(streamReader, JsonObject.class); + + return new SdcSingleControllerConfiguration(jsonNode, sdcControllerName); } @Test diff --git a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java index 85218abb5..6ab800b36 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java @@ -160,7 +160,7 @@ public class CldsServiceItCase { } @Test - public void testGetCLDSDetails() throws IOException { + public void testGetCldsDetails() throws IOException { List<CldsMonitoringDetails> cldsMonitoringDetailsList = cldsService.getCldsDetails(); assertNotNull(cldsMonitoringDetailsList); } diff --git a/src/test/java/org/onap/clamp/clds/it/DcaeHttpConnectionManagerItCase.java b/src/test/java/org/onap/clamp/clds/it/DcaeHttpConnectionManagerItCase.java index 12e8dd90d..8e03153a2 100644 --- a/src/test/java/org/onap/clamp/clds/it/DcaeHttpConnectionManagerItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/DcaeHttpConnectionManagerItCase.java @@ -69,22 +69,22 @@ public class DcaeHttpConnectionManagerItCase { @Autowired DcaeHttpConnectionManager dcaeHttpConnectionManager; - private static TrustManager[] trustAllCerts = new TrustManager[] { - new X509TrustManager() { + private static TrustManager[] trustAllCerts = new TrustManager[]{ + new X509TrustManager() { - @Override - public java.security.cert.X509Certificate[] getAcceptedIssuers() { - return null; - } + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } - @Override - public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { - } + @Override + public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } - @Override - public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { - } + @Override + public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } + } }; private void enableSslNoCheck() throws NoSuchAlgorithmException, KeyManagementException { diff --git a/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java b/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java index 31594cbac..3acc439a4 100644 --- a/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java @@ -24,7 +24,9 @@ package org.onap.clamp.clds.it; +import com.google.gson.reflect.TypeToken; import java.io.IOException; +import java.lang.reflect.Type; import java.util.List; import java.util.Map; @@ -47,7 +49,7 @@ import org.onap.clamp.clds.model.properties.Policy; import org.onap.clamp.clds.model.properties.PolicyItem; import org.onap.clamp.clds.model.properties.Tca; import org.onap.clamp.clds.transform.XslTransformer; -import org.onap.clamp.clds.util.JacksonUtils; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.clds.util.LoggingUtils; import org.onap.clamp.clds.util.ResourceFileUtil; import org.onap.policy.api.AttributeType; @@ -67,6 +69,8 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest public class PolicyClientItCase { + private static final Type MAP_OF_STRING_TO_OBJECT_TYPE = new TypeToken<Map<String, Object>>() {}.getType(); + @Autowired private CldsDao cldsDao; @Autowired @@ -82,6 +86,11 @@ public class PolicyClientItCase { String modelBpmnPropJson; ModelProperties prop; + /** + * Setup method. + * @throws IOException thrown if resources not found + * @throws TransformerException thrown if invalid xml given to transformation + */ @Before public void setUp() throws IOException, TransformerException { modelProp = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-properties.json"); @@ -94,7 +103,7 @@ public class PolicyClientItCase { } @Test - public void testSendGuardPolicy() throws TransformerException, IOException { + public void testSendGuardPolicy() { // Normally there is only one Guard List<PolicyItem> policyItems = GuardPolicyAttributesConstructor .getAllPolicyGuardsFromPolicyChain(prop.getType(Policy.class).getPolicyChains().get(0)); @@ -105,8 +114,7 @@ public class PolicyClientItCase { String response = policyClient.sendGuardPolicy( GuardPolicyAttributesConstructor.formatAttributes(prop, policyItem), prop, LoggingUtils.getRequestId(), policyItem); - Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class); + Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE); Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Decision"), Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0_Guard_6TtHGPq"), @@ -124,14 +132,13 @@ public class PolicyClientItCase { } @Test - public void testSendBrmsPolicy() - throws TransformerException, BuilderException, IllegalArgumentException, IOException { + public void testSendBrmsPolicy() throws BuilderException, IllegalArgumentException, IOException { Map<AttributeType, Map<String, String>> attributes = OperationalPolicyAttributesConstructor.formatAttributes( refProp, prop, prop.getType(Policy.class).getId(), prop.getType(Policy.class).getPolicyChains().get(0)); String response = policyClient.sendBrmsPolicy(attributes, prop, LoggingUtils.getRequestId()); - Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class); + Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE); + Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"), Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"), Assertions.entry("policyConfigType", PolicyConfigType.BRMS_PARAM.name()), @@ -144,14 +151,13 @@ public class PolicyClientItCase { } @Test - public void testSendMicroServiceInJson() - throws TransformerException, BuilderException, IllegalArgumentException, IOException { + public void testSendMicroServiceInJson() throws IllegalArgumentException { prop.setCurrentModelElementId(prop.getType(Policy.class).getId()); String jsonToSend = "{\"test\":\"test\"}"; String response = policyClient.sendMicroServiceInJson(jsonToSend, prop, LoggingUtils.getRequestId()); - Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class); + Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE); + Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"), Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h"), Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()), @@ -165,8 +171,8 @@ public class PolicyClientItCase { public void testSendBasePolicyInOther() throws IllegalArgumentException, IOException { String body = "test"; String response = policyClient.sendBasePolicyInOther(body, "myPolicy", prop, LoggingUtils.getRequestId()); - Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class); + Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE); + Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"), Assertions.entry("policyName", "myPolicy"), Assertions.entry("policyConfigType", PolicyConfigType.Base.name()), @@ -181,8 +187,8 @@ public class PolicyClientItCase { String tcaJson = TcaRequestFormatter.createPolicyJson(refProp, prop); String response = policyClient.sendMicroServiceInOther(tcaJson, prop); - Map<String, Object> mapNodes = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree(response), Map.class); + Map<String, Object> mapNodes = JsonUtils.GSON.fromJson(response, MAP_OF_STRING_TO_OBJECT_TYPE); + Assertions.assertThat(mapNodes).contains(Assertions.entry("policyClass", "Config"), Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"), Assertions.entry("policyConfigType", PolicyConfigType.MicroService.name()), @@ -196,10 +202,10 @@ public class PolicyClientItCase { String[] responses = policyClient.deleteMicrosService(prop).split("\\}\\{"); // There are 2 responses appended to the result, one for PDP one for PAP ! - Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class); - Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class); + Map<String, Object> mapNodesPdp = JsonUtils.GSON.fromJson(responses[0] + "}", + MAP_OF_STRING_TO_OBJECT_TYPE); + Map<String, Object> mapNodesPap = JsonUtils.GSON.fromJson("{" + responses[1], + MAP_OF_STRING_TO_OBJECT_TYPE); Assertions.assertThat(mapNodesPdp).contains( Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_TCA_1d13unw"), @@ -221,11 +227,10 @@ public class PolicyClientItCase { prop.setGuardUniqueId(policyItems.get(0).getId()); String[] responses = policyClient.deleteGuard(prop).split("\\}\\{"); - // There are 2 responses appended to the result, one for PDP one for PAP ! - Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class); - Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class); + Map<String, Object> mapNodesPdp = JsonUtils.GSON.fromJson(responses[0] + "}", + MAP_OF_STRING_TO_OBJECT_TYPE); + Map<String, Object> mapNodesPap = JsonUtils.GSON.fromJson("{" + responses[1], + MAP_OF_STRING_TO_OBJECT_TYPE); Assertions.assertThat(mapNodesPdp).contains( Assertions.entry("policyName", @@ -245,11 +250,10 @@ public class PolicyClientItCase { prop.setCurrentModelElementId(prop.getType(Policy.class).getId()); String[] responses = policyClient.deleteBrms(prop).split("\\}\\{"); - // There are 2 responses appended to the result, one for PDP one for PAP ! - Map<String, Object> mapNodesPdp = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree(responses[0] + "}"), Map.class); - Map<String, Object> mapNodesPap = JacksonUtils.getObjectMapperInstance() - .convertValue(JacksonUtils.getObjectMapperInstance().readTree("{" + responses[1]), Map.class); + Map<String, Object> mapNodesPdp = JsonUtils.GSON.fromJson(responses[0] + "}", + MAP_OF_STRING_TO_OBJECT_TYPE); + Map<String, Object> mapNodesPap = JsonUtils.GSON.fromJson("{" + responses[1], + MAP_OF_STRING_TO_OBJECT_TYPE); Assertions.assertThat(mapNodesPdp).contains( Assertions.entry("policyName", modelName.replace("-", "_") + "." + controlName + "_Policy_12lup3h_0"), @@ -273,7 +277,8 @@ public class PolicyClientItCase { String tosca = policyClient.importToscaModel(cldsToscaModel); Assertions.assertThat(tosca).contains( - "{\"serviceName\":\"tca-policy-test\",\"description\":\"tca-policy-test\",\"requestID\":null,\"filePath\":\"/tmp/tosca-models/tca-policy-test.yml\","); + "{\"serviceName\":\"tca-policy-test\",\"description\":\"tca-policy-test\"," + + "\"requestID\":null,\"filePath\":\"/tmp/tosca-models/tca-policy-test.yml\","); Assertions.assertThat(tosca).contains(toscaModelYaml); } } diff --git a/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java b/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java index 9d58ba8f0..630e8c028 100644 --- a/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java @@ -28,8 +28,8 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.gson.JsonElement; import java.io.IOException; import java.util.List; @@ -62,24 +62,34 @@ public class CldsReferencePropertiesItCase { assertNull(refProp.getStringValue("does.not.exist")); } - /** - * Test getting prop value as a JSON Node / template. - * - * @throws IOException - * when JSON parsing fails - */ @Test - public void testGetJsonTemplate() throws IOException { - // ui.location.default={"DC1":"Data Center 1","DC2":"Data Center - // 2","DC3":"Data Center 3"} - ObjectNode root = (ObjectNode) refProp.getJsonTemplate("ui.location.default"); + public void shouldReturnJsonFromTemplate() throws IOException { + //when + JsonElement root = refProp.getJsonTemplate("ui.location.default"); + + //then assertNotNull(root); - assertEquals(root.get("DC1").asText(), "Data Center 1"); - // Test composite key - root = (ObjectNode) refProp.getJsonTemplate("ui.location", "default"); + assertTrue(root.isJsonObject()); + assertEquals(root.getAsJsonObject().get("DC1").getAsString(), "Data Center 1"); + } + + @Test + public void shouldReturnJsonFromTemplate_2() throws IOException { + //when + JsonElement root = refProp.getJsonTemplate("ui.location", "default"); + + //then assertNotNull(root); - assertEquals(root.get("DC1").asText(), "Data Center 1"); - root = (ObjectNode) refProp.getJsonTemplate("ui.location", ""); + assertTrue(root.isJsonObject()); + assertEquals(root.getAsJsonObject().get("DC1").getAsString(), "Data Center 1"); + } + + @Test + public void shouldReturnNullIfPropertyNotFound() throws IOException { + //when + JsonElement root = refProp.getJsonTemplate("ui.location", ""); + + //then assertNull(root); } diff --git a/src/test/java/org/onap/clamp/clds/it/config/SdcControllersConfigurationItCase.java b/src/test/java/org/onap/clamp/clds/it/config/SdcControllersConfigurationItCase.java index 0cab4b9d3..8fd817cbe 100644 --- a/src/test/java/org/onap/clamp/clds/it/config/SdcControllersConfigurationItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/config/SdcControllersConfigurationItCase.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.gson.JsonSyntaxException; import java.io.IOException; import java.util.Map; @@ -71,7 +72,7 @@ public class SdcControllersConfigurationItCase { sdcControllersConfiguration.getSdcSingleControllerConfiguration("sdc-controller2").getSdcControllerName()); } - @Test(expected = IOException.class) + @Test(expected = JsonSyntaxException.class) public void testBadJsonLoading() throws IOException { loadFile("classpath:/clds/sdc-controllers-config-bad.json"); fail("Should have raised an exception"); diff --git a/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java b/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java index 9eaca5f73..c6dbce4c5 100644 --- a/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/sdc/controller/SdcSingleControllerItCase.java @@ -23,7 +23,6 @@ package org.onap.clamp.clds.it.sdc.controller; -import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -89,8 +88,11 @@ public class SdcSingleControllerItCase { return notifData; } + /** + * Initialization method. + */ @Before - public void init() throws IOException { + public void init() { sdcSingleController = new SdcSingleController(clampProp, Mockito.mock(CsarInstaller.class), SdcSingleControllerConfigurationTest.loadControllerConfiguration("clds/sdc-controller-config-TLS.json", "sdc-controller1"), diff --git a/src/test/java/org/onap/clamp/clds/model/prop/CustomModelElement.java b/src/test/java/org/onap/clamp/clds/model/prop/CustomModelElement.java index 7d3a21583..871d0a651 100644 --- a/src/test/java/org/onap/clamp/clds/model/prop/CustomModelElement.java +++ b/src/test/java/org/onap/clamp/clds/model/prop/CustomModelElement.java @@ -18,16 +18,16 @@ * limitations under the License.
* ============LICENSE_END============================================
* ===================================================================
- *
+ *
*/
package org.onap.clamp.clds.model.prop;
-import com.fasterxml.jackson.databind.JsonNode;
-
+import com.google.gson.JsonObject;
import org.onap.clamp.clds.model.properties.AbstractModelElement;
import org.onap.clamp.clds.model.properties.ModelBpmn;
import org.onap.clamp.clds.model.properties.ModelProperties;
+import org.onap.clamp.clds.util.JsonUtils;
/**
* A CustomModelElement to test the capability to add new elements on the fly.
@@ -40,10 +40,10 @@ public class CustomModelElement extends AbstractModelElement { /**
* Main Constructor.
*/
- public CustomModelElement(ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
+ public CustomModelElement(ModelProperties modelProp, ModelBpmn modelBpmn, JsonObject modelJson) {
super(CUSTOM_TYPE, modelProp, modelBpmn, modelJson);
- topicPublishes = getValueByName("topicPublishes");
- test = this.getValueByName("test");
+ topicPublishes = JsonUtils.getStringValueByName(modelElementJsonNode, "topicPublishes");
+ test = JsonUtils.getStringValueByName(modelElementJsonNode, "test");
}
public static final String getType() {
diff --git a/src/test/java/org/onap/clamp/clds/model/prop/ModelPropertiesTest.java b/src/test/java/org/onap/clamp/clds/model/prop/ModelPropertiesTest.java index 295ccc59f..4195f907c 100644 --- a/src/test/java/org/onap/clamp/clds/model/prop/ModelPropertiesTest.java +++ b/src/test/java/org/onap/clamp/clds/model/prop/ModelPropertiesTest.java @@ -23,6 +23,7 @@ package org.onap.clamp.clds.model.prop; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -36,6 +37,7 @@ import org.onap.clamp.clds.model.CldsModel; import org.onap.clamp.clds.model.properties.Holmes; import org.onap.clamp.clds.model.properties.ModelProperties; import org.onap.clamp.clds.model.properties.Policy; +import org.onap.clamp.clds.model.properties.PolicyItem; import org.onap.clamp.clds.model.properties.Tca; import org.onap.clamp.clds.util.ResourceFileUtil; @@ -61,11 +63,12 @@ public class ModelPropertiesTest { assertEquals(1, policy.getPolicyChains().size()); assertEquals("0", policy.getPolicyChains().get(0).getPolicyId()); assertEquals(1, policy.getPolicyChains().get(0).getPolicyItems().size()); - assertEquals("resourceid", policy.getPolicyChains().get(0).getPolicyItems().get(0).getTargetResourceId()); - assertEquals(180, policy.getPolicyChains().get(0).getPolicyItems().get(0).getRetryTimeLimit()); - assertEquals(3, policy.getPolicyChains().get(0).getPolicyItems().get(0).getMaxRetries()); - assertEquals("", policy.getPolicyChains().get(0).getPolicyItems().get(0).getParentPolicy()); - assertEquals(null, policy.getPolicyChains().get(0).getPolicyItems().get(0).getParentPolicyConditions()); + PolicyItem firstPolicyItem = policy.getPolicyChains().get(0).getPolicyItems().get(0); + assertEquals("resourceid", firstPolicyItem.getTargetResourceId()); + assertEquals(180, firstPolicyItem.getRetryTimeLimit()); + assertEquals(3, firstPolicyItem.getMaxRetries()); + assertEquals("", firstPolicyItem.getParentPolicy()); + assertThat(firstPolicyItem.getParentPolicyConditions()).isEmpty(); Tca tca = prop.getType(Tca.class); assertNotNull(tca); assertTrue(tca.isFound()); @@ -91,8 +94,8 @@ public class ModelPropertiesTest { prop.getGlobal().getResourceVf().toArray())); assertTrue(Arrays.equals(new String[] { "SNDGCA64", "ALPRGAED", "LSLEILAA", "MDTWNJC1" }, prop.getGlobal().getLocation().toArray())); - assertEquals("value1", prop.getGlobal().getDeployParameters().get("input1").asText()); - assertEquals("value2", prop.getGlobal().getDeployParameters().get("input2").asText()); + assertEquals("value1", prop.getGlobal().getDeployParameters().get("input1").getAsString()); + assertEquals("value2", prop.getGlobal().getDeployParameters().get("input2").getAsString()); } @Test @@ -107,11 +110,12 @@ public class ModelPropertiesTest { assertEquals(1, policy.getPolicyChains().size()); assertEquals("0", policy.getPolicyChains().get(0).getPolicyId()); assertEquals(1, policy.getPolicyChains().get(0).getPolicyItems().size()); - assertEquals("resourceid", policy.getPolicyChains().get(0).getPolicyItems().get(0).getTargetResourceId()); - assertEquals(180, policy.getPolicyChains().get(0).getPolicyItems().get(0).getRetryTimeLimit()); - assertEquals(3, policy.getPolicyChains().get(0).getPolicyItems().get(0).getMaxRetries()); - assertEquals("", policy.getPolicyChains().get(0).getPolicyItems().get(0).getParentPolicy()); - assertEquals(null, policy.getPolicyChains().get(0).getPolicyItems().get(0).getParentPolicyConditions()); + PolicyItem firstPolicyItem = policy.getPolicyChains().get(0).getPolicyItems().get(0); + assertEquals("resourceid", firstPolicyItem.getTargetResourceId()); + assertEquals(180, firstPolicyItem.getRetryTimeLimit()); + assertEquals(3, firstPolicyItem.getMaxRetries()); + assertEquals("", firstPolicyItem.getParentPolicy()); + assertThat(firstPolicyItem.getParentPolicyConditions()).isEmpty(); Holmes holmes = prop.getType(Holmes.class); assertNotNull(holmes); assertTrue(holmes.isFound()); @@ -124,8 +128,8 @@ public class ModelPropertiesTest { prop.getGlobal().getResourceVf().toArray())); assertTrue(Arrays.equals(new String[] { "SNDGCA64", "ALPRGAED", "LSLEILAA", "MDTWNJC1" }, prop.getGlobal().getLocation().toArray())); - assertEquals("value1", prop.getGlobal().getDeployParameters().get("input1").asText()); - assertEquals("value2", prop.getGlobal().getDeployParameters().get("input2").asText()); + assertEquals("value1", prop.getGlobal().getDeployParameters().get("input1").getAsString()); + assertEquals("value2", prop.getGlobal().getDeployParameters().get("input2").getAsString()); } @Test diff --git a/src/test/java/org/onap/clamp/clds/model/sdc/SdcResourceBasicInfoTest.java b/src/test/java/org/onap/clamp/clds/model/sdc/SdcResourceBasicInfoTest.java index 119fd2181..5adb0647a 100644 --- a/src/test/java/org/onap/clamp/clds/model/sdc/SdcResourceBasicInfoTest.java +++ b/src/test/java/org/onap/clamp/clds/model/sdc/SdcResourceBasicInfoTest.java @@ -31,15 +31,17 @@ public class SdcResourceBasicInfoTest { @Test public void testHashCode() { SdcResourceBasicInfo sdc1a = new SdcResourceBasicInfo(); - SdcResourceBasicInfo sdc1b = new SdcResourceBasicInfo(); - SdcResourceBasicInfo sdc2 = new SdcResourceBasicInfo(); sdc1a.setName("test1"); sdc1a.setVersion("1.0"); - sdc1b.setName("test1"); - sdc1b.setVersion("2.0"); + + SdcResourceBasicInfo sdc2 = new SdcResourceBasicInfo(); sdc2.setName("test2"); sdc2.setVersion("2.0"); + SdcResourceBasicInfo sdc1b = new SdcResourceBasicInfo(); + sdc1b.setName("test1"); + sdc1b.setVersion("2.0"); + Assertions.assertThat(sdc1a.hashCode()).isNotEqualTo(sdc1b.hashCode()); Assertions.assertThat(sdc1b.hashCode()).isNotEqualTo(sdc2.hashCode()); sdc1b.setVersion("1.0"); @@ -50,12 +52,14 @@ public class SdcResourceBasicInfoTest { @Test public void testCompareTo() { SdcResourceBasicInfo sdc1a = new SdcResourceBasicInfo(); - SdcResourceBasicInfo sdc1b = new SdcResourceBasicInfo(); - SdcResourceBasicInfo sdc2 = new SdcResourceBasicInfo(); sdc1a.setName("test1"); sdc1a.setVersion("1.0"); + + SdcResourceBasicInfo sdc1b = new SdcResourceBasicInfo(); sdc1b.setName("test1"); sdc1b.setVersion("2.0"); + + SdcResourceBasicInfo sdc2 = new SdcResourceBasicInfo(); sdc2.setName("test2"); sdc2.setVersion("2.0"); @@ -68,12 +72,14 @@ public class SdcResourceBasicInfoTest { @Test public void testEquals() { SdcResourceBasicInfo sdc1a = new SdcResourceBasicInfo(); - SdcResourceBasicInfo sdc1b = new SdcResourceBasicInfo(); - SdcResourceBasicInfo sdc2 = new SdcResourceBasicInfo(); sdc1a.setName("test1"); sdc1a.setVersion("1.0"); + + SdcResourceBasicInfo sdc1b = new SdcResourceBasicInfo(); sdc1b.setName("test1"); sdc1b.setVersion("2.0"); + + SdcResourceBasicInfo sdc2 = new SdcResourceBasicInfo(); sdc2.setName("test2"); sdc2.setVersion("2.0"); diff --git a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImplTest.java b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImplTest.java new file mode 100644 index 000000000..a995c523d --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImplTest.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.sdc.controller.installer; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.gson.JsonObject; +import java.io.IOException; +import org.junit.Test; +import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.clds.util.ResourceFileUtil; + +public class CsarInstallerImplTest { + + @Test + public void shouldReturnInputParametersFromBlueprint() throws IOException { + //given + String expectedBlueprintInputsText = "{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\"" + + ",\"aaiEnrichmentPort\":\"8443\"" + + ",\"enableAAIEnrichment\":true" + + ",\"dmaap_host\":\"message-router\"" + + ",\"dmaap_port\":\"3904\"" + + ",\"enableRedisCaching\":false" + + ",\"redisHosts\":\"dcae-redis:6379\"" + + ",\"tag_version\":\"nexus3.onap.org:10001/onap/org.onap.dcaegen2.deployments.tca-cdap-container:1.1.0\"" + + ",\"consul_host\":\"consul-server\"" + + ",\"consul_port\":\"8500\",\"cbs_host\":\"{\\\"test\\\":" + + "{\\\"test\\\":\\\"test\\\"}}\",\"cbs_port\":\"10000\"" + + ",\"external_port\":\"32010\",\"policy_id\":\"AUTO_GENERATED_POLICY_ID_AT_SUBMIT\"}"; + + JsonObject expectedBlueprintInputs = JsonUtils.GSON.fromJson(expectedBlueprintInputsText, JsonObject.class); + String dceaBlueprint = ResourceFileUtil.getResourceAsString("tosca/dcea_blueprint.yml"); + BlueprintArtifact blueprintArtifact = mock(BlueprintArtifact.class); + when(blueprintArtifact.getDcaeBlueprint()).thenReturn(dceaBlueprint); + CsarInstallerImpl csarInstaller = new CsarInstallerImpl(); + + //when + String parametersInJson = csarInstaller.getAllBlueprintParametersInJson(blueprintArtifact); + + //then + assertThat(JsonUtils.GSON.fromJson(parametersInJson, JsonObject.class)).isEqualTo(expectedBlueprintInputs); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/clamp/clds/util/JacksonUtilsTest.java b/src/test/java/org/onap/clamp/clds/util/JacksonUtilsTest.java deleted file mode 100644 index 1d9e4e79d..000000000 --- a/src/test/java/org/onap/clamp/clds/util/JacksonUtilsTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * 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============================================ - * =================================================================== - * - */ - -package org.onap.clamp.clds.util; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JsonMappingException; - -import java.io.IOException; - -import org.junit.Test; - -public class JacksonUtilsTest { - - public static class TestClass extends TestObject { - - String test2; - TestObject2 object2; - - public TestClass(String value1, String value2) { - super(value1); - test2 = value2; - } - - public TestClass() { - } - - public String getTest2() { - return test2; - } - - public void setTest2(String test2) { - this.test2 = test2; - } - - public TestObject2 getObject2() { - return object2; - } - - public void setObject2(TestObject2 object2) { - this.object2 = object2; - } - } - - @Test - public void testGetObjectMapperInstance() { - assertNotNull(JacksonUtils.getObjectMapperInstance()); - } - - /** - * This method test that the security hole in Jackson is not enabled in the - * default ObjectMapper. - * - * @throws JsonParseException - * In case of issues - * @throws JsonMappingException - * In case of issues - * @throws IOException - * In case of issues - */ - @Test - public void testCreateBeanDeserializer() throws JsonParseException, JsonMappingException, IOException { - TestClass test = new TestClass("value1", "value2"); - test.setObject2(new TestObject2("test3")); - Object testObject = JacksonUtils.getObjectMapperInstance() - .readValue("[\"org.onap.clamp.clds.util.JacksonUtilsTest$TestClass\"" - + ",{\"test\":\"value1\",\"test2\":\"value2\",\"object2\":[\"org.onap.clamp.clds.util.TestObject2\"," - + "{\"test3\":\"test3\"}]}]", Object.class); - assertNotNull(testObject); - assertFalse(testObject instanceof TestObject); - assertFalse(testObject instanceof TestClass); - } -} diff --git a/src/test/java/org/onap/clamp/clds/util/JsonUtilsTest.java b/src/test/java/org/onap/clamp/clds/util/JsonUtilsTest.java new file mode 100644 index 000000000..3e11b8a23 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/util/JsonUtilsTest.java @@ -0,0 +1,157 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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============================================ + * Modifications copyright (c) 2019 Nokia + * =================================================================== + * + */ + +package org.onap.clamp.clds.util; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.io.IOException; + +import java.util.List; +import org.junit.Test; + +public class JsonUtilsTest { + + public static class TestClass extends TestObject { + + String test2; + TestObject2 object2; + + TestClass(String value1, String value2) { + super(value1); + test2 = value2; + } + + void setObject2(TestObject2 object2) { + this.object2 = object2; + } + } + + private static final JsonObject DEPLOY_PARAMETERS = JsonUtils.GSON.fromJson( + "{\n" + + " \"aaiEnrichmentHost\": \"aai.onap.svc.cluster.local\",\n" + + " \"aaiEnrichmentPort\": \"8443\",\n" + + " \"enableAAIEnrichment\": true,\n" + + " \"dmaap_host\": \"message-router\",\n" + + " \"dmaap_port\": \"3904\",\n" + + " \"enableRedisCaching\": false,\n" + + " \"redisHosts\": \"dcae-redis:6379\",\n" + + " \"tag_version\": \"nexus3.onap.org:10001/onap/org.onap.dcaegen2." + + "deployments.tca-cdap-container:1.1.0\",\n" + + " \"consul_host\": \"consul-server\",\n" + + " \"consul_port\": \"8500\",\n" + + " \"cbs_host\": \"config-binding-service\",\n" + + " \"cbs_port\": \"10000\",\n" + + " \"external_port\": \"32010\",\n" + + " \"policy_id\": \"AUTO_GENERATED_POLICY_ID_AT_SUBMIT\"\n" + + " }", JsonObject.class); + + + @Test + public void testGetObjectMapperInstance() { + assertNotNull(JsonUtils.GSON); + } + + /** + * This method test that the security hole in Jackson is not enabled in the default ObjectMapper. + */ + @Test + public void testCreateBeanDeserializer() { + TestClass test = new TestClass("value1", "value2"); + test.setObject2(new TestObject2("test3")); + Object testObject = JsonUtils.GSON.fromJson("[\"org.onap.clamp.clds.util.JsonUtilsTest$TestClass\"" + + ",{\"test\":\"value1\",\"test2\":\"value2\",\"object2\":[\"org.onap.clamp.clds.util.TestObject2\"," + + "{\"test3\":\"test3\"}]}]", Object.class); + assertNotNull(testObject); + assertFalse(testObject instanceof TestObject); + } + + + @Test + public void shouldReturnJsonValueByName() throws IOException { + //given + String modelProperties = ResourceFileUtil + .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json"); + JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global"); + + //when + String locationName = JsonUtils.getStringValueByName(globalElement, "location"); + String timeoutValue = JsonUtils.getStringValueByName(globalElement, "timeout"); + + //then + assertThat(locationName).isEqualTo("SNDGCA64"); + assertThat(timeoutValue).isEqualTo("500"); + } + + @Test + public void shouldReturnJsonObjectByPropertyName() throws IOException { + //given + String modelProperties = ResourceFileUtil + .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json"); + JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global"); + + //when + JsonObject deployParameters = JsonUtils.getJsonObjectByName(globalElement, "deployParameters"); + + //then + assertThat(deployParameters).isEqualToComparingFieldByField(DEPLOY_PARAMETERS); + } + + @Test + public void shouldReturnJsonValuesByPropertyName() throws IOException { + //given + String modelProperties = ResourceFileUtil + .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json"); + JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global"); + + //when + List<String> vfs = JsonUtils.getStringValuesByName(globalElement, "vf"); + + //then + assertThat(vfs).containsExactly( + "6c7aaec2-59eb-41d9-8681-b7f976ab668d", + "8sadsad0-a98s-6a7s-fd12-sadji9sa8d12", + "8sfd71ad-a90d-asd9-as87-8a7sd81adsaa" + ); + } + + + @Test + public void shouldReturnJsonValueAsInteger() throws IOException { + //given + String modelProperties = ResourceFileUtil + .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json"); + JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global"); + + //when + Integer timeoutValue = JsonUtils.getIntValueByName(globalElement, "timeout"); + + //then + assertThat(timeoutValue).isEqualTo(500); + } +} |