b63fd1546793732b3ad365cdb25b19ac804d038d
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2024 Nordix Foundation
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.sim.pdp.comm;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
27 import java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.utils.services.Registry;
40 import org.onap.policy.models.pdp.concepts.PdpStatus;
41 import org.onap.policy.models.pdp.concepts.PdpUpdate;
42 import org.onap.policy.models.sim.pdp.PdpSimulatorActivator;
43 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
44 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
45 import org.onap.policy.models.sim.pdp.handler.PdpMessageHandler;
46 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
47 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49
50 /**
51  * Class to perform unit test of {@link PdpUpdateListener}.
52  *
53  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
54  */
55 class TestPdpUpdateListener {
56     private PdpUpdateListener pdpUpdateMessageListener;
57     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
58     private static final String TOPIC = "my-topic";
59     private PdpSimulatorActivator activator;
60
61     /**
62      * Method for setup before each test.
63      *
64      * @throws Exception if an error occurs
65      */
66     @BeforeEach
67     void setUp() throws Exception {
68         Registry.newRegistry();
69         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
70         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
71         PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
72         // The arguments return a string if there is a message to print and we should
73         // exit
74         final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
75         if (argumentMessage != null) {
76             return;
77         }
78         // Validate that the arguments are sane
79         arguments.validate();
80
81         // Read the parameters
82         pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
83
84         activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup);
85         Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
86         activator.initialize();
87         pdpUpdateMessageListener = new PdpUpdateListener();
88     }
89
90     /**
91      * Method for cleanup after each test.
92      *
93      * @throws Exception if an error occurs
94      */
95     @AfterEach
96     void teardown() throws Exception {
97
98         // clear the pdp simulator activator
99         if (activator != null && activator.isAlive()) {
100             activator.terminate();
101         }
102     }
103
104     @Test
105     void testPdpUpdateMssageListener() {
106         final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
107         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
108         pdpUpdateMsg.setDescription("dummy pdp status for test");
109         pdpUpdateMsg.setPdpGroup("pdpGroup");
110         pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
111         pdpUpdateMsg.setName(pdpStatus.getName());
112         pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
113         final ToscaPolicy toscaPolicy = new ToscaPolicy();
114         toscaPolicy.setType("apexpolicytype");
115         toscaPolicy.setVersion("1.0");
116         toscaPolicy.setName("apex policy name");
117         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
118         String properties;
119         try {
120             properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
121                     StandardCharsets.UTF_8);
122             propertiesMap.put("content", properties);
123         } catch (final IOException e) {
124             propertiesMap.put("content", "");
125         }
126         toscaPolicy.setProperties(propertiesMap);
127         final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
128         toscaPolicies.add(toscaPolicy);
129         pdpUpdateMsg.setPoliciesToBeDeployed(toscaPolicies);
130         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
131         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
132         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
133         assertEquals(pdpStatus.getPolicies(),
134                 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
135     }
136 }