b08e796b7176d68979e11cc9b203561779367e26
[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.api.impl;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27
28 import java.time.Instant;
29 import java.util.UUID;
30 import org.junit.jupiter.api.Test;
31 import org.mockito.Mockito;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
39 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
40 import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43
44 class ParticipantIntermediaryApiImplTest {
45
46     private CommonTestData commonTestData = new CommonTestData();
47     private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
48     private static final String ID_VERSION = "1.0.1";
49
50     private static final String ID_NAME_E = "org.onap.domain.pmsh.PMSHControlLoopDefinition";
51     private static final String ID_VERSION_E = "1.0.0";
52
53     private static final String ID_NAME_TYPE = "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant";
54     private static final String ID_VERSION_TYPE = "2.3.4";
55
56     @Test
57     void mockParticipantIntermediaryApiImplTest() throws CoderException {
58         var uuid = UUID.randomUUID();
59         var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
60         var participantHandler = commonTestData.getParticipantHandlerControlLoops();
61         var controlLoopHandler = commonTestData.setTestControlLoopHandler(id, uuid);
62         var apiImpl = new ParticipantIntermediaryApiImpl(participantHandler, controlLoopHandler);
63         var clElementListener = Mockito.mock(ControlLoopElementListener.class);
64         apiImpl.registerControlLoopElementListener(clElementListener);
65
66         assertNotNull(apiImpl.getControlLoops(id.getName(), id.getVersion()));
67         assertThat(apiImpl.getClElementDefinitionCommonProperties(id)).isEmpty();
68
69         var participantStatistics = new ParticipantStatistics();
70         participantStatistics.setParticipantId(id);
71         participantStatistics.setTimeStamp(Instant.ofEpochMilli(123456L));
72         participantStatistics.setState(ParticipantState.PASSIVE);
73         participantStatistics.setHealthStatus(ParticipantHealthStatus.HEALTHY);
74         apiImpl.updateParticipantStatistics(participantStatistics);
75
76         var participants = apiImpl.getParticipants(id.getName(), id.getVersion());
77         assertEquals(ParticipantState.UNKNOWN, participants.get(0).getParticipantState());
78
79         var participant = apiImpl.updateParticipantState(id, ParticipantState.TERMINATED);
80         assertEquals(ParticipantState.TERMINATED, participant.getParticipantState());
81
82         var elements = apiImpl.getControlLoopElements(ID_NAME_E, ID_VERSION_E);
83         assertFalse(elements.containsKey(uuid));
84
85         var element = apiImpl.getControlLoopElement(elements.keySet().iterator().next());
86         var idType = new ToscaConceptIdentifier(ID_NAME_TYPE, ID_VERSION_TYPE);
87         assertEquals(idType, element.getParticipantType());
88
89         var clElementStatistics = new ClElementStatistics();
90         var controlLoopId = new ToscaConceptIdentifier("defName", "0.0.1");
91         clElementStatistics.setParticipantId(controlLoopId);
92         clElementStatistics.setControlLoopState(ControlLoopState.RUNNING);
93         clElementStatistics.setTimeStamp(Instant.now());
94
95         apiImpl.updateControlLoopElementStatistics(uuid, clElementStatistics);
96         var clElement = apiImpl.updateControlLoopElementState(id, uuid, ControlLoopOrderedState.UNINITIALISED,
97                 ControlLoopState.PASSIVE, ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK);
98         assertEquals(ControlLoopOrderedState.UNINITIALISED, clElement.getOrderedState());
99         assertEquals(uuid, clElement.getId());
100
101     }
102 }