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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.sim.pdp.comm;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
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;
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;
51 * Class to perform unit test of {@link PdpUpdateListener}.
53 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
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;
62 * Method for setup before each test.
64 * @throws Exception if an error occurs
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
74 final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
75 if (argumentMessage != null) {
78 // Validate that the arguments are sane
81 // Read the parameters
82 pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
84 activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup);
85 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
86 activator.initialize();
87 pdpUpdateMessageListener = new PdpUpdateListener();
91 * Method for cleanup after each test.
93 * @throws Exception if an error occurs
96 void teardown() throws Exception {
98 // clear the pdp simulator activator
99 if (activator != null && activator.isAlive()) {
100 activator.terminate();
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<>();
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", "");
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()));