2b3704738f43b6bed1163ae37249fe4a2370ff1b
[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  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.pdp.comm;
23
24 import static org.junit.Assert.assertEquals;
25
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;
33 import java.util.Map;
34 import java.util.Properties;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.endpoints.utils.ParameterUtils;
40 import org.onap.policy.common.utils.services.Registry;
41 import org.onap.policy.models.pdp.concepts.PdpStateChange;
42 import org.onap.policy.models.pdp.concepts.PdpStatus;
43 import org.onap.policy.models.pdp.concepts.PdpUpdate;
44 import org.onap.policy.models.pdp.enums.PdpState;
45 import org.onap.policy.models.sim.pdp.PdpSimulatorActivator;
46 import org.onap.policy.models.sim.pdp.PdpSimulatorCommandLineArguments;
47 import org.onap.policy.models.sim.pdp.PdpSimulatorConstants;
48 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
49 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
51
52 /**
53  * Class to perform unit test of {@link PdpStateChangeListener}.
54  *
55  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
56  */
57 public class TestPdpStateChangeListener {
58     private static final String PDP_SUBGROUP = "pdpSubgroup";
59     private static final String PDP_GROUP = "pdpGroup";
60     private PdpUpdateListener pdpUpdateMessageListener;
61     private PdpStateChangeListener pdpStateChangeListener;
62     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
63     private static final String TOPIC = "my-topic";
64     private PdpSimulatorActivator activator;
65
66     /**
67      * Method for setup before each test.
68      *
69      * @throws Exception if an error occurs
70      */
71     @Before
72     public void setUp() throws Exception {
73         pdpUpdateMessageListener = new PdpUpdateListener();
74         pdpStateChangeListener = new PdpStateChangeListener();
75         Registry.newRegistry();
76         final String[] pdpSimulatorConfigParameters = { "-c", "src/test/resources/PdpSimulatorConfigParameters.json" };
77         final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
78         PdpSimulatorParameterGroup pdpSimulatorParameterGroup;
79         // The arguments return a string if there is a message to print and we should
80         // exit
81         final String argumentMessage = arguments.parse(pdpSimulatorConfigParameters);
82         if (argumentMessage != null) {
83             return;
84         }
85         // Validate that the arguments are sane
86         arguments.validate();
87
88         // Read the parameters
89         pdpSimulatorParameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
90
91         // Read the properties
92         final Properties topicProperties =
93             ParameterUtils.getTopicProperties(pdpSimulatorParameterGroup.getTopicParameterGroup());
94         activator = new PdpSimulatorActivator(pdpSimulatorParameterGroup, topicProperties);
95         Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
96         activator.initialize();
97     }
98
99     /**
100      * Method for cleanup after each test.
101      *
102      * @throws Exception if an error occurs
103      */
104     @After
105     public void teardown() throws Exception {
106
107         // clear the pdp simulator activator
108         if (activator != null && activator.isAlive()) {
109             activator.terminate();
110         }
111     }
112
113     /**
114      * Method to initiate a PdpUpdate.
115      *
116      * @param instance the instance id
117      * @return PdpUpdate the pdp update message
118      */
119     private PdpUpdate performPdpUpdate(final String instance) {
120         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
121         pdpUpdateMsg.setDescription("dummy pdp status for test");
122         pdpUpdateMsg.setPdpGroup(PDP_GROUP);
123         pdpUpdateMsg.setPdpSubgroup(PDP_SUBGROUP);
124         pdpUpdateMsg.setName(instance);
125         final ToscaPolicy toscaPolicy = new ToscaPolicy();
126         toscaPolicy.setType("apexpolicytype");
127         toscaPolicy.setVersion("1.0");
128         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
129
130         String properties;
131         try {
132             properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
133                     StandardCharsets.UTF_8);
134             propertiesMap.put("content", properties);
135         } catch (final IOException e) {
136             propertiesMap.put("content", "");
137         }
138         toscaPolicy.setProperties(propertiesMap);
139         final List<ToscaPolicy> toscaPolicies = new ArrayList<>();
140         toscaPolicies.add(toscaPolicy);
141         pdpUpdateMsg.setPolicies(toscaPolicies);
142         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
143         return pdpUpdateMsg;
144     }
145
146     @Test
147     public void testPdpStateChangeMessageListener_passivetopassive() {
148         final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
149         performPdpUpdate(pdpStatus.getName());
150         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
151         pdpStateChangeMsg.setState(PdpState.PASSIVE);
152         pdpStateChangeMsg.setPdpGroup(PDP_GROUP);
153         pdpStateChangeMsg.setPdpSubgroup(PDP_SUBGROUP);
154         pdpStateChangeMsg.setName(pdpStatus.getName());
155         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
156
157         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
158     }
159
160     @Test
161     public void testPdpStateChangeMessageListener_activetoactive() {
162         final PdpStatus pdpStatus = Registry.get(PdpSimulatorConstants.REG_PDP_STATUS_OBJECT);
163         performPdpUpdate(pdpStatus.getName());
164         pdpStatus.setState(PdpState.ACTIVE);
165         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
166         pdpStateChangeMsg.setState(PdpState.ACTIVE);
167         pdpStateChangeMsg.setPdpGroup(PDP_GROUP);
168         pdpStateChangeMsg.setPdpSubgroup(PDP_SUBGROUP);
169         pdpStateChangeMsg.setName(pdpStatus.getName());
170         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
171
172         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
173     }
174 }