921cfd2836910c32da92ebac9e04fd325f1799b8
[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.PdpUpdateListener;
44 import org.onap.policy.apex.services.onappf.exception.ApexStarterException;
45 import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler;
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.PdpStatus;
51 import org.onap.policy.models.pdp.concepts.PdpUpdate;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53
54 /**
55  * Class to perform unit test of {@link PdpUpdateListener}.
56  *
57  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
58  */
59 public class TestPdpUpdateListener {
60     private PdpUpdateListener pdpUpdateMessageListener;
61     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
62     private static final String TOPIC = "my-topic";
63     private ApexStarterActivator activator;
64
65     @Before
66     public void setUp() throws ApexStarterException, FileNotFoundException, IOException {
67         Registry.newRegistry();
68         final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json",
69             "-p", "src/test/resources/topic.properties" };
70         final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments();
71         ApexStarterParameterGroup apexStarterParameterGroup;
72         // The arguments return a string if there is a message to print and we should
73         // exit
74         final String argumentMessage = arguments.parse(apexStarterConfigParameters);
75         if (argumentMessage != null) {
76             return;
77         }
78         // Validate that the arguments are sane
79         arguments.validate();
80
81         // Read the parameters
82         apexStarterParameterGroup = new ApexStarterParameterHandler().getParameters(arguments);
83
84         // Read the properties
85         final Properties topicProperties = new Properties();
86         final String propFile = arguments.getFullPropertyFilePath();
87         try (FileInputStream stream = new FileInputStream(propFile)) {
88             topicProperties.load(stream);
89         }
90         activator = new ApexStarterActivator(apexStarterParameterGroup, topicProperties);
91         Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator);
92         activator.initialize();
93         pdpUpdateMessageListener = new PdpUpdateListener();
94     }
95
96     /**
97      * Method for cleanup after each test.
98      *
99      * @throws Exception if an error occurs
100      */
101     @After
102     public void teardown() throws Exception {
103
104         // clear the apex starter activator
105         if (activator != null && activator.isAlive()) {
106             activator.terminate();
107         }
108     }
109
110     @Test
111     public void testPdpUpdateMssageListener() {
112         final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
113         final PdpUpdate pdpUpdateMsg = new PdpUpdate();
114         pdpUpdateMsg.setDescription("dummy pdp status for test");
115         pdpUpdateMsg.setPdpGroup("pdpGroup");
116         pdpUpdateMsg.setPdpSubgroup("pdpSubgroup");
117         pdpUpdateMsg.setName(pdpStatus.getName());
118         pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000));
119         final ToscaPolicy toscaPolicy = new ToscaPolicy();
120         toscaPolicy.setType("apexpolicytype");
121         toscaPolicy.setVersion("1.0");
122         toscaPolicy.setName("apex policy name");
123         final Map<String, Object> propertiesMap = new LinkedHashMap<>();
124         String properties;
125         try {
126             properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")),
127                     StandardCharsets.UTF_8);
128             propertiesMap.put("content", properties);
129         } catch (final IOException e) {
130             propertiesMap.put("content", "");
131         }
132         toscaPolicy.setProperties(propertiesMap);
133         final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
134         toscaPolicies.add(toscaPolicy);
135         pdpUpdateMsg.setPolicies(toscaPolicies);
136         pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
137         assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
138         assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
139         assertEquals(pdpStatus.getPolicies(),
140                 new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
141     }
142 }