2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.services.onappf.comm;
23 import static org.junit.Assert.assertEquals;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
34 import java.util.Properties;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.apex.services.onappf.ApexStarterActivator;
40 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
41 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
42 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
43 import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler;
44 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
45 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
46 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
47 import org.onap.policy.common.endpoints.utils.ParameterUtils;
48 import org.onap.policy.common.utils.services.Registry;
49 import org.onap.policy.models.pdp.concepts.PdpStatus;
50 import org.onap.policy.models.pdp.concepts.PdpUpdate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
54 * Class to perform unit test of {@link PdpUpdateListener}.
56 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
58 public class TestPdpUpdateListener {
59 private PdpUpdateListener pdpUpdateMessageListener;
60 private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
61 private static final String TOPIC = "my-topic";
62 private ApexStarterActivator activator;
65 * Method for setup before each test.
67 * @throws ApexStarterException if some error occurs while starting up the apex starter
68 * @throws FileNotFoundException if the file is missing
69 * @throws IOException if IO exception occurs
72 public void setUp() throws ApexStarterException, FileNotFoundException, IOException {
73 Registry.newRegistry();
74 final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" };
75 final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
76 ApexStarterParameterGroup parameterGroup;
77 // The arguments return a string if there is a message to print and we should
79 final String argumentMessage = arguments.parse(apexStarterConfigParameters);
80 if (argumentMessage != null) {
83 // Validate that the arguments are sane
86 // Read the parameters
87 parameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
89 // Read the properties
90 Properties topicProperties = ParameterUtils.getTopicProperties(parameterGroup.getTopicParameterGroup());
91 activator = new ApexStarterActivator(parameterGroup, topicProperties);
92 Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
93 activator.initialize();
94 pdpUpdateMessageListener = new PdpUpdateListener();
98 * Method for cleanup after each test.
100 * @throws Exception if an error occurs
103 public void teardown() throws Exception {
105 // clear the apex starter activator
106 if (activator != null && activator.isAlive()) {
107 activator.terminate();
112 public void testPdpUpdateMssageListener() {
113 final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
114 final PdpUpdate pdpUpdateMsg = new PdpUpdate();
115 pdpUpdateMsg.setDescription("dummy pdp status for test");
116 pdpUpdateMsg.setPdpGroup("pdpGroup");
117 pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
118 pdpUpdateMsg.setName(pdpStatus.getName());
119 pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
120 final ToscaPolicy toscaPolicy = new ToscaPolicy();
121 toscaPolicy.setType("apexpolicytype");
122 toscaPolicy.setVersion("1.0");
123 toscaPolicy.setName("apex policy name");
124 final Map<String, Object> propertiesMap = new LinkedHashMap<>();
127 properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
128 StandardCharsets.UTF_8);
129 propertiesMap.put("content", properties);
130 } catch (final IOException e) {
131 propertiesMap.put("content", "");
133 toscaPolicy.setProperties(propertiesMap);
134 final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
135 toscaPolicies.add(toscaPolicy);
136 pdpUpdateMsg.setPolicies(toscaPolicies);
137 pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
138 assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
139 assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
140 assertEquals(pdpStatus.getPolicies(),
141 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));