d0a85f9f248d04d1a8b9745c332ae824329c0845
[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.starter.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.starter.ApexStarterActivator;
41 import org.onap.policy.apex.starter.ApexStarterCommandLineArguments;
42 import org.onap.policy.apex.starter.ApexStarterConstants;
43 import org.onap.policy.apex.starter.exception.ApexStarterException;
44 import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup;
45 import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler;
46 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
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     @Before
67     public void setUp() throws ApexStarterException, FileNotFoundException, IOException {
68         pdpUpdateMessageListener = new PdpUpdateListener();
69         pdpStateChangeListener = new PdpStateChangeListener();
70         Registry.newRegistry();
71         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json",
72             "-p", "src/test/resources/topic.properties" };
73         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
74         ApexStarterParameterGroup apexStarterParameterGroup;
75         // The arguments return a string if there is a message to print and we should
76         // exit
77         final String argumentMessage = arguments.parse(apexStarterConfigParameters);
78         if (argumentMessage != null) {
79             return;
80         }
81         // Validate that the arguments are sane
82         arguments.validate();
83
84         // Read the parameters
85         apexStarterParameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
86
87         // Read the properties
88         final Properties topicProperties = new Properties();
89         final String propFile = arguments.getFullPropertyFilePath();
90         try (FileInputStream stream = new FileInputStream(propFile)) {
91             topicProperties.load(stream);
92         }
93         activator = new ApexStarterActivator(apexStarterParameterGroup, topicProperties);
94         Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
95         activator.initialize();
96     }
97
98     /**
99      * Method for cleanup after each test.
100      *
101      * @throws Exception if an error occurs
102      */
103     @After
104     public void teardown() throws Exception {
105
106         // clear the apex starter activator
107         if (activator != null && activator.isAlive()) {
108             activator.terminate();
109         }
110     }
111
112     /**
113      * @param instance
114      * @return
115      */
116     private PdpUpdate performPdpUpdate(final String instance) {
117         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
118         pdpUpdateMsg.setDescription("dummy pdp status for test");
119         pdpUpdateMsg.setPdpGroup("pdpGroup");
120         pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
121         pdpUpdateMsg.setName(instance);
122         final ToscaPolicy toscaPolicy = new ToscaPolicy();
123         toscaPolicy.setType("apexpolicytype");
124         toscaPolicy.setVersion("1.0");
125         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
126
127         String properties;
128         try {
129             properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
130                     StandardCharsets.UTF_8);
131             propertiesMap.put("content", properties);
132         } catch (final IOException e) {
133             propertiesMap.put("content", "");
134         }
135         toscaPolicy.setProperties(propertiesMap);
136         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
137         toscaPolicies.add(toscaPolicy);
138         pdpUpdateMsg.setPolicies(toscaPolicies);
139         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
140         return pdpUpdateMsg;
141     }
142
143     @Test
144     public void testPdpStateChangeMessageListener_passivetopassive() {
145         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
146         performPdpUpdate(pdpStatus.getName());
147         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
148         pdpStateChangeMsg.setState(PdpState.PASSIVE);
149         pdpStateChangeMsg.setPdpGroup("pdpGroup");
150         pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup");
151         pdpStateChangeMsg.setName(pdpStatus.getName());
152         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
153
154         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
155     }
156
157     @Test
158     public void testPdpStateChangeMessageListener_activetoactive() {
159         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
160         performPdpUpdate(pdpStatus.getName());
161         pdpStatus.setState(PdpState.ACTIVE);
162         final PdpStateChange pdpStateChangeMsg = new PdpStateChange();
163         pdpStateChangeMsg.setState(PdpState.ACTIVE);
164         pdpStateChangeMsg.setPdpGroup("pdpGroup");
165         pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup");
166         pdpStateChangeMsg.setName(pdpStatus.getName());
167         pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
168
169         assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState());
170     }
171 }