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