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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.intermediary.api.impl;
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;
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;
44 class ParticipantIntermediaryApiImplTest {
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";
50 private static final String ID_NAME_E = "org.onap.domain.pmsh.PMSHControlLoopDefinition";
51 private static final String ID_VERSION_E = "1.0.0";
53 private static final String ID_NAME_TYPE = "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant";
54 private static final String ID_VERSION_TYPE = "2.3.4";
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);
66 assertNotNull(apiImpl.getControlLoops(id.getName(), id.getVersion()));
67 assertThat(apiImpl.getClElementDefinitionCommonProperties(id)).isEmpty();
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);
76 var participants = apiImpl.getParticipants(id.getName(), id.getVersion());
77 assertEquals(ParticipantState.UNKNOWN, participants.get(0).getParticipantState());
79 var participant = apiImpl.updateParticipantState(id, ParticipantState.TERMINATED);
80 assertEquals(ParticipantState.TERMINATED, participant.getParticipantState());
82 var elements = apiImpl.getControlLoopElements(ID_NAME_E, ID_VERSION_E);
83 assertFalse(elements.containsKey(uuid));
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());
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());
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());