From d17e8f4bec4dc158cb206199d153e85f49e5d53c Mon Sep 17 00:00:00 2001 From: lapentafd Date: Tue, 9 Nov 2021 10:45:04 +0000 Subject: Code Coverage clamp common Code Coverage and Sonar issue in CommonUtils.java implicit public constructor Issue-ID: POLICY-3452 Change-Id: I55ab96ac7bdd098d7ad4daeb8e89a108e0ab6c4b Signed-off-by: lapentafd --- .../controlloop/common/utils/CommonUtils.java | 6 +- .../common/rest/CoderHttpMessageConverterTest.java | 60 ++++++++ .../rest/RequestResponseLoggingFilterTest.java | 49 +++++++ .../controlloop/common/utils/CommonUtilsTest.java | 151 +++++++++++++++++++++ .../simulator/main/rest/TestListenerUtils.java | 1 - 5 files changed, 264 insertions(+), 3 deletions(-) create mode 100644 common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMessageConverterTest.java create mode 100644 common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilterTest.java create mode 100644 common/src/test/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtilsTest.java diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java index 4b0e41f23..4ebd0aaa8 100644 --- a/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java +++ b/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java @@ -38,8 +38,10 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; * */ public class CommonUtils { - private static final String POLICY_TYPE_ID = "policy_type_id"; - private static final String POLICY_ID = "policy_id"; + + private CommonUtils() { + throw new IllegalStateException("Utility class"); + } /** * Prepare participant updates map. diff --git a/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMessageConverterTest.java b/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMessageConverterTest.java new file mode 100644 index 000000000..58015ae3d --- /dev/null +++ b/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMessageConverterTest.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.common.rest; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.clamp.controlloop.common.startstop.CommonCommandLineArguments; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; + +class CoderHttpMessageConverterTest { + + + @Test + void coderHttpMesageConverterTest() throws ControlLoopRuntimeException, IOException { + var y = new CoderHttpMesageConverter<>("yaml"); + var j = new CoderHttpMesageConverter<>("json"); + + assertTrue(y.supports(CommonCommandLineArguments.class)); + assertTrue(j.supports(CommonCommandLineArguments.class)); + var testInputStream = new ByteArrayInputStream("testdata".getBytes()); + HttpInputMessage input = Mockito.mock(HttpInputMessage.class); + Mockito.when(input.getBody()).thenReturn(testInputStream); + assertThrows(ControlLoopRuntimeException.class, () -> { + y.readInternal(RequestResponseLoggingFilterTest.class, input); + }); + + var testOutputStream = new ByteArrayOutputStream(); + HttpOutputMessage output = Mockito.mock(HttpOutputMessage.class); + Mockito.when(output.getBody()).thenReturn(testOutputStream); + assertThrows(ControlLoopRuntimeException.class, () -> { + j.writeInternal(String.class, output); + }); + } +} diff --git a/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilterTest.java b/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilterTest.java new file mode 100644 index 000000000..7621c9219 --- /dev/null +++ b/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilterTest.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Modifications Copyright (C) 2021 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.common.rest; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import java.io.IOException; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +class RequestResponseLoggingFilterTest { + + @Test + void initTest() throws IOException, ServletException { + var e = new RequestResponseLoggingFilter(); + var res = Mockito.mock(HttpServletResponse.class); + var req = Mockito.mock(HttpServletRequest.class); + var chain = Mockito.mock(FilterChain.class); + Mockito.when(req + .getHeader(RequestResponseLoggingFilter.REQUEST_ID_NAME)) + .thenReturn("id"); + + assertDoesNotThrow(() -> e.doFilter(req, res, chain)); + } +} diff --git a/common/src/test/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtilsTest.java b/common/src/test/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtilsTest.java new file mode 100644 index 000000000..52e86cf61 --- /dev/null +++ b/common/src/test/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtilsTest.java @@ -0,0 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.common.utils; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; + +class CommonUtilsTest { + + private ToscaConceptIdentifier id = new ToscaConceptIdentifier("id", "1.0.0"); + private ToscaConceptIdentifier idNode = new ToscaConceptIdentifier( + "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", "0.0.0"); + + @Test + void testCommonUtilsParticipantUpdate() { + var clElement = new ControlLoopElement(); + List participantUpdates = new ArrayList<>(); + assertThat(participantUpdates).isEmpty(); + + CommonUtils.prepareParticipantUpdate(clElement, participantUpdates); + assertThat(participantUpdates).isNotEmpty(); + assertEquals(clElement, participantUpdates.get(0).getControlLoopElementList().get(0)); + + CommonUtils.prepareParticipantUpdate(clElement, participantUpdates); + assertNotEquals(id, participantUpdates.get(0).getParticipantId()); + + clElement.setParticipantId(id); + clElement.setParticipantType(id); + CommonUtils.prepareParticipantUpdate(clElement, participantUpdates); + assertEquals(id, participantUpdates.get(1).getParticipantId()); + } + + @Test + void testCommonUtilsServiceTemplate() { + var clElement = new ControlLoopElement(); + var toscaServiceTemplate = getDummyToscaServiceTemplate(); + CommonUtils.setServiceTemplatePolicyInfo(clElement, toscaServiceTemplate); + assertEquals(getDummyToscaDataTypeMap(), clElement.getToscaServiceTemplateFragment().getDataTypes()); + } + + @Test + void testCommonUtilsDefinitionUpdate() { + var toscaServiceTemplate = getDummyToscaServiceTemplate(); + List participantDefinitionUpdates = new ArrayList<>(); + assertThat(participantDefinitionUpdates).isEmpty(); + + checkParticipantDefinitionUpdate(toscaServiceTemplate, participantDefinitionUpdates); + assertThat(participantDefinitionUpdates).isNotEmpty(); + assertEquals(id, participantDefinitionUpdates.get(0).getParticipantType()); + + checkParticipantDefinitionUpdate(toscaServiceTemplate, participantDefinitionUpdates); + assertEquals(idNode, participantDefinitionUpdates.get(0) + .getControlLoopElementDefinitionList().get(0) + .getClElementDefinitionId()); + } + + private ToscaServiceTemplate getDummyToscaServiceTemplate() { + var toscaServiceTemplate = new ToscaServiceTemplate(); + var policyTypes = getDummyPolicyTypesMap(); + toscaServiceTemplate.setPolicyTypes(policyTypes); + + var dataTypes = getDummyToscaDataTypeMap(); + dataTypes.put("onap.datatypes.ToscaConceptIdentifier", new ToscaDataType()); + toscaServiceTemplate.setDataTypes(dataTypes); + + var toscaTopologyTemplate = new ToscaTopologyTemplate(); + Map policy = new HashMap<>(); + toscaTopologyTemplate.setPolicies(List.of(policy)); + var nodeTemplates = getDummyNodeTemplates(); + toscaTopologyTemplate.setNodeTemplates(nodeTemplates); + + toscaServiceTemplate.setToscaTopologyTemplate(toscaTopologyTemplate); + toscaServiceTemplate.setDerivedFrom("tosca.nodetypes.Root"); + toscaServiceTemplate.setDescription("description"); + toscaServiceTemplate.setMetadata(null); + toscaServiceTemplate.setName("name"); + toscaServiceTemplate.setToscaDefinitionsVersion("1.0.0"); + toscaServiceTemplate.setVersion("1.0.1"); + return toscaServiceTemplate; + } + + private Map getDummyPolicyTypesMap() { + Map policyTypes = new HashMap<>(); + policyTypes.put("onap.policies.Match", new ToscaPolicyType()); + return policyTypes; + } + + private Map getDummyToscaDataTypeMap() { + Map dataTypes = new HashMap<>(); + dataTypes.put("onap.datatypes.ToscaConceptIdentifier", new ToscaDataType()); + return dataTypes; + } + + private Map getDummyNodeTemplates() { + Map nodeTemplates = new HashMap<>(); + var nodeTemplate = new ToscaNodeTemplate(); + nodeTemplate.setType("org.onap.policy.clamp.controlloop.ControlLoopElement"); + nodeTemplates.put("org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", nodeTemplate); + return nodeTemplates; + } + + private void checkParticipantDefinitionUpdate( + ToscaServiceTemplate toscaServiceTemplate, + List participantDefinitionUpdates) { + + for (Map.Entry toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() + .getNodeTemplates().entrySet()) { + if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), + toscaServiceTemplate)) { + CommonUtils.prepareParticipantDefinitionUpdate(id, toscaInputEntry.getKey(), + toscaInputEntry.getValue(), participantDefinitionUpdates, null); + } + } + } +} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java index c9848248e..9f6a31e28 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java @@ -191,7 +191,6 @@ public final class TestListenerUtils { ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead(); // Add policies to the toscaServiceTemplate - List participantDefinitionUpdates = new ArrayList<>(); for (Map.Entry toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() .getNodeTemplates().entrySet()) { -- cgit 1.2.3-korg