29387959b3f1967410c27aeab63c566fa1993c93
[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.intermediary.handler;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.mockito.Mockito.mock;
27
28 import java.time.Instant;
29 import java.util.UUID;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
37 import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39 import org.springframework.test.context.junit.jupiter.SpringExtension;
40
41 @ExtendWith(SpringExtension.class)
42 class ControlLoopHandlerTest {
43
44     private CommonTestData commonTestData = new CommonTestData();
45
46     @Test
47     void controlLoopHandlerTest() {
48         ControlLoopHandler clh = commonTestData.getMockControlLoopHandler();
49         assertNotNull(clh.getControlLoops());
50
51         assertNotNull(clh.getControlLoopMap());
52         assertNotNull(clh.getElementsOnThisParticipant());
53
54         UUID elementId1 = UUID.randomUUID();
55         ControlLoopElement element = new ControlLoopElement();
56         element.setId(elementId1);
57         element.setDefinition(new ToscaConceptIdentifier(
58                 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "1.0.1"));
59
60         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
61
62         ControlLoopElementListener listener = mock(ControlLoopElementListener.class);
63         clh.registerControlLoopElementListener(listener);
64         assertThat(clh.getListeners()).contains(listener);
65
66     }
67
68     @Test
69     void updateNullControlLoopHandlerTest() {
70         UUID id = UUID.randomUUID();
71
72         ControlLoopHandler clh = commonTestData.getMockControlLoopHandler();
73         assertNull(clh.updateControlLoopElementState(null, null, ControlLoopOrderedState.UNINITIALISED,
74                 ControlLoopState.PASSIVE));
75
76         assertNull(clh.updateControlLoopElementState(null, id, ControlLoopOrderedState.UNINITIALISED,
77                 ControlLoopState.PASSIVE));
78
79         ClElementStatistics clElementStatistics = new ClElementStatistics();
80         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("defName", "0.0.1");
81         clElementStatistics.setParticipantId(controlLoopId);
82         clElementStatistics.setControlLoopState(ControlLoopState.RUNNING);
83         clElementStatistics.setTimeStamp(Instant.now());
84
85         clh.updateControlLoopElementStatistics(id, clElementStatistics);
86         assertNull(clh.updateControlLoopElementState(controlLoopId, id, ControlLoopOrderedState.UNINITIALISED,
87                 ControlLoopState.PASSIVE));
88
89
90
91     }
92
93 }