d9653021971c7441069f5f158c42179fd581006e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.clamp.controlloop.participant.policy.main.handler;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.onap.policy.clamp.controlloop.common.ControlLoopConstants;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
32 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
33 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
34 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
35 import org.onap.policy.clamp.controlloop.participant.policy.main.startstop.Main;
36 import org.onap.policy.clamp.controlloop.participant.policy.main.startstop.ParticipantPolicyActivator;
37 import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.utils.services.Registry;
40
41 public class TestPolicyHandler {
42
43     private static ControlLoopUpdateListener clUpdateListener;
44     private static ParticipantControlLoopUpdate participantControlLoopUpdateMsg;
45     private static final String PARTICIPANTS_ENDPOINT = "participants";
46     private static final String ELEMENTS_ENDPOINT = "elements";
47     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
48     private static final String TOPIC = "my-topic";
49     static CommonTestData commonTestData = new CommonTestData();
50
51     /**
52      * Setup before class, instantiate Main.
53      *
54      */
55     @BeforeClass
56     public static void setUpBeforeClass() throws Exception {
57         Registry.newRegistry();
58         final String[] configParameters = {"-c", "src/test/resources/parameters/TestParameters.json"};
59         Main main = new Main(configParameters);
60         assertTrue(main.isRunning());
61         TestListenerUtils.initParticipantHandler();
62
63         clUpdateListener = new ControlLoopUpdateListener(
64                 PolicyHandler.getInstance()
65                 .getPolicyProvider()
66                 .getIntermediaryApi()
67                 .getParticipantHandler());
68         participantControlLoopUpdateMsg =
69                 TestListenerUtils.createControlLoopUpdateMsg("src/test/resources/utils/servicetemplates");
70         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
71     }
72
73     @Test
74     public void testUpdatePolicies() throws Exception {
75         // Add policy_types to the toscaServiceTemplate
76         TestListenerUtils.addPolicyTypesToToscaServiceTemplate(
77                         participantControlLoopUpdateMsg.getControlLoopDefinition());
78
79         // Add policies to the toscaServiceTemplate
80         TestListenerUtils.addPoliciesToToscaServiceTemplate(participantControlLoopUpdateMsg.getControlLoopDefinition());
81
82         // Verify that the ToscaServicetemplate has policy_types
83         assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition().getPolicyTypes());
84
85         // Verify that the ToscaServicetemplate has policies
86         assertNotNull(participantControlLoopUpdateMsg.getControlLoopDefinition()
87                 .getToscaTopologyTemplate().getPolicies());
88
89         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
90
91         // Verify the result of GET participants with what is stored
92         assertEquals("org.onap.PM_Policy",
93                 TestListenerUtils.getParticipantHandler().getParticipantId().getName());
94     }
95 }