fc7bd8fb26f497ccd4f8b529948d74fae78cdebd
[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.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;
33 import java.util.Map;
34 import java.util.Properties;
35
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.parameters.ApexStarterParameterGroup;
44 import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler;
45 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
46 import org.onap.policy.common.endpoints.utils.ParameterUtils;
47 import org.onap.policy.common.utils.services.Registry;
48 import org.onap.policy.models.pdp.concepts.PdpStateChange;
49 import org.onap.policy.models.pdp.concepts.PdpStatus;
50 import org.onap.policy.models.pdp.concepts.PdpUpdate;
51 import org.onap.policy.models.pdp.enums.PdpState;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53
54 /**
55  * Class to perform unit test of {@link PdpStateChangeListener}.
56  *
57  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
58  */
59 public class TestPdpStateChangeListener {
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 ApexStarterActivator activator;
65
66     /**
67      * Method for setup before each test.
68      *
69      * @throws ApexStarterException if some error occurs while starting up the apex starter
70      * @throws FileNotFoundException if the file is missing
71      * @throws IOException if IO exception occurs
72      */
73     @Before
74     public void setUp() throws ApexStarterException, FileNotFoundException, IOException {
75         pdpUpdateMessageListener = new PdpUpdateListener();
76         pdpStateChangeListener = new PdpStateChangeListener();
77         Registry.newRegistry();
78         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" };
79         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
80         ApexStarterParameterGroup parameterGroup;
81         // The arguments return a string if there is a message to print and we should
82         // exit
83         final String argumentMessage = arguments.parse(apexStarterConfigParameters);
84         if (argumentMessage != null) {
85             return;
86         }
87         // Validate that the arguments are sane
88         arguments.validate();
89
90         // Read the parameters
91         parameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
92
93         // Read the properties
94         Properties topicProperties = ParameterUtils.getTopicProperties(parameterGroup.getTopicParameterGroup());
95         activator = new ApexStarterActivator(parameterGroup, 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      * Method to initiate a PdpUpdate.
116      *
117      * @param instance the instance id
118      * @return PdpUpdate the pdp update message
119      */
120     private PdpUpdate performPdpUpdate(final String instance) {
121         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
122         pdpUpdateMsg.setDescription("dummy pdp status for test");
123         pdpUpdateMsg.setPdpGroup("pdpGroup");
124         pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
125         pdpUpdateMsg.setName(instance);
126         final ToscaPolicy toscaPolicy = new ToscaPolicy();
127         toscaPolicy.setType("apexpolicytype");
128         toscaPolicy.setVersion("1.0");
129         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
130
131         String properties;
132         try {
133             properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
134                     StandardCharsets.UTF_8);
135             propertiesMap.put("content", properties);
136         } catch (final IOException e) {
137             propertiesMap.put("content", "");
138         }
139         toscaPolicy.setProperties(propertiesMap);
140         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
141         toscaPolicies.add(toscaPolicy);
142         pdpUpdateMsg.setPolicies(toscaPolicies);
143         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
144         return pdpUpdateMsg;
145     }
146
147     @Test
148     public void testPdpStateChangeMessageListener_passivetopassive() {
149         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
150         performPdpUpdate(pdpStatus.getName());
151         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
152         pdpStateChangeMsg.setState(PdpState.PASSIVE);
153         pdpStateChangeMsg.setPdpGroup("pdpGroup");
154         pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup");
155         pdpStateChangeMsg.setName(pdpStatus.getName());
156         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
157
158         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
159     }
160
161     @Test
162     public void testPdpStateChangeMessageListener_activetoactive() {
163         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
164         performPdpUpdate(pdpStatus.getName());
165         pdpStatus.setState(PdpState.ACTIVE);
166         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
167         pdpStateChangeMsg.setState(PdpState.ACTIVE);
168         pdpStateChangeMsg.setPdpGroup("pdpGroup");
169         pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup");
170         pdpStateChangeMsg.setName(pdpStatus.getName());
171         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
172
173         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
174     }
175 }