34025a1ad40cf1cd6eaccbcdadd880789a6f7abe
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.services.onappf.comm;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
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 java.util.Properties;
36
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.apex.services.onappf.ApexStarterActivator;
41 import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments;
42 import org.onap.policy.apex.services.onappf.ApexStarterConstants;
43 import org.onap.policy.apex.services.onappf.comm.PdpStateChangeListener;
44 import org.onap.policy.apex.services.onappf.comm.PdpUpdateListener;
45 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
46 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup;
47 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
48 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
49 import org.onap.policy.common.utils.services.Registry;
50 import org.onap.policy.models.pdp.concepts.PdpStateChange;
51 import org.onap.policy.models.pdp.concepts.PdpStatus;
52 import org.onap.policy.models.pdp.concepts.PdpUpdate;
53 import org.onap.policy.models.pdp.enums.PdpState;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
55
56 /**
57  * Class to perform unit test of {@link PdpStateChangeListener}.
58  *
59  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
60  */
61 public class TestPdpStateChangeListener {
62     private PdpUpdateListener pdpUpdateMessageListener;
63     private PdpStateChangeListener pdpStateChangeListener;
64     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
65     private static final String TOPIC = "my-topic";
66     private ApexStarterActivator activator;
67
68     @Before
69     public void setUp() throws ApexStarterException, FileNotFoundException, IOException {
70         pdpUpdateMessageListener = new PdpUpdateListener();
71         pdpStateChangeListener = new PdpStateChangeListener();
72         Registry.newRegistry();
73         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json",
74             "-p", "src/test/resources/topic.properties" };
75         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
76         ApexStarterParameterGroup apexStarterParameterGroup;
77         // The arguments return a string if there is a message to print and we should
78         // exit
79         final String argumentMessage = arguments.parse(apexStarterConfigParameters);
80         if (argumentMessage != null) {
81             return;
82         }
83         // Validate that the arguments are sane
84         arguments.validate();
85
86         // Read the parameters
87         apexStarterParameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
88
89         // Read the properties
90         final Properties topicProperties = new Properties();
91         final String propFile = arguments.getFullPropertyFilePath();
92         try (FileInputStream stream = new FileInputStream(propFile)) {
93             topicProperties.load(stream);
94         }
95         activator = new ApexStarterActivator(apexStarterParameterGroup, topicProperties);
96         Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
97         activator.initialize();
98     }
99
100     /**
101      * Method for cleanup after each test.
102      *
103      * @throws Exception if an error occurs
104      */
105     @After
106     public void teardown() throws Exception {
107
108         // clear the apex starter activator
109         if (activator != null && activator.isAlive()) {
110             activator.terminate();
111         }
112     }
113
114     /**
115      * @param instance
116      * @return
117      */
118     private PdpUpdate performPdpUpdate(final String instance) {
119         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
120         pdpUpdateMsg.setDescription("dummy pdp status for test");
121         pdpUpdateMsg.setPdpGroup("pdpGroup");
122         pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
123         pdpUpdateMsg.setName(instance);
124         final ToscaPolicy toscaPolicy = new ToscaPolicy();
125         toscaPolicy.setType("apexpolicytype");
126         toscaPolicy.setVersion("1.0");
127         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
128
129         String properties;
130         try {
131             properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
132                     StandardCharsets.UTF_8);
133             propertiesMap.put("content", properties);
134         } catch (final IOException e) {
135             propertiesMap.put("content", "");
136         }
137         toscaPolicy.setProperties(propertiesMap);
138         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
139         toscaPolicies.add(toscaPolicy);
140         pdpUpdateMsg.setPolicies(toscaPolicies);
141         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
142         return pdpUpdateMsg;
143     }
144
145     @Test
146     public void testPdpStateChangeMessageListener_passivetopassive() {
147         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
148         performPdpUpdate(pdpStatus.getName());
149         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
150         pdpStateChangeMsg.setState(PdpState.PASSIVE);
151         pdpStateChangeMsg.setPdpGroup("pdpGroup");
152         pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup");
153         pdpStateChangeMsg.setName(pdpStatus.getName());
154         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
155
156         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
157     }
158
159     @Test
160     public void testPdpStateChangeMessageListener_activetoactive() {
161         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
162         performPdpUpdate(pdpStatus.getName());
163         pdpStatus.setState(PdpState.ACTIVE);
164         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
165         pdpStateChangeMsg.setState(PdpState.ACTIVE);
166         pdpStateChangeMsg.setPdpGroup("pdpGroup");
167         pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup");
168         pdpStateChangeMsg.setName(pdpStatus.getName());
169         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
170
171         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
172     }
173 }