a307d3457c68bfe9b980c1e18368c6a817a29371
[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.simulator.main.intermediary;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
34 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
35 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
36 import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.TestListenerUtils;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.utils.coder.CoderException;
39
40 /**
41  * Class to perform unit test of {@link ControlLoopUpdateListener}.
42  */
43 public class TestControlLoopUpdateListener {
44     private static ControlLoopUpdateListener clUpdateListener;
45     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
46     private static final String TOPIC = "my-topic";
47     static CommonTestData commonTestData = new CommonTestData();
48
49     /**
50      * Method for setup.
51      *
52      * @throws ParticipantException if some error occurs while starting up the participant
53      * @throws FileNotFoundException if the file is missing
54      * @throws IOException if IO exception occurs
55      */
56     @BeforeClass
57     public static void setUp() throws ControlLoopException, FileNotFoundException, IOException {
58         TestListenerUtils.initParticipantHandler();
59         clUpdateListener = new ControlLoopUpdateListener(TestListenerUtils.getParticipantHandler());
60     }
61
62     @Test
63     public void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException {
64         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = prepareMsg("DummyName");
65         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
66
67         // Verify the content in participantHandler
68         assertNotEquals(participantControlLoopUpdateMsg.getParticipantId().getName(),
69                 TestListenerUtils.getParticipantHandler().getParticipantId().getName());
70     }
71
72     @Test
73     public void testControlLoopUpdateListener() throws CoderException {
74         ParticipantControlLoopUpdate participantControlLoopUpdateMsg = prepareMsg("org.onap.PM_CDS_Blueprint");
75         clUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantControlLoopUpdateMsg);
76
77         // Verify the content in participantHandler
78         assertEquals(TestListenerUtils.getParticipantHandler().getParticipantId(),
79                 participantControlLoopUpdateMsg.getParticipantId());
80         assertThat(TestListenerUtils.getParticipantHandler().getControlLoopHandler().getControlLoops()
81                 .getControlLoopList()).hasSize(1);
82     }
83
84     private ParticipantControlLoopUpdate prepareMsg(final String participantName) {
85         ParticipantControlLoopUpdate participantControlLoopUpdateMsg;
86         participantControlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg();
87         participantControlLoopUpdateMsg.getParticipantId().setName(participantName);
88         participantControlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE);
89         return participantControlLoopUpdateMsg;
90     }
91 }