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