b38adbc5c1d362209d6879b8e29694f814540c98
[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.handler;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.mockito.Mockito.when;
25
26 import java.util.UUID;
27 import org.junit.jupiter.api.Test;
28 import org.mockito.Mockito;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
32 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
37
38 class ControlLoopElementHandlerTest {
39
40     private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
41     private static final String ID_VERSION = "1.0.1";
42     private static final UUID controlLoopElementId = UUID.randomUUID();
43     private static final ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
44
45     @Test
46     void testSimulatorHandlerExceptions() throws PfModelException {
47         ControlLoopElementHandler handler = getTestingHandler();
48
49         assertDoesNotThrow(() -> handler
50                 .controlLoopElementStateChange(controlLoopId,
51                         controlLoopElementId,
52                         ControlLoopState.UNINITIALISED,
53                         ControlLoopOrderedState.PASSIVE));
54
55         assertDoesNotThrow(() -> handler
56                 .controlLoopElementStateChange(controlLoopId,
57                         controlLoopElementId,
58                         ControlLoopState.RUNNING,
59                         ControlLoopOrderedState.UNINITIALISED));
60
61         assertDoesNotThrow(() -> handler
62                 .controlLoopElementStateChange(controlLoopId,
63                         controlLoopElementId,
64                         ControlLoopState.PASSIVE,
65                         ControlLoopOrderedState.RUNNING));
66         var element = getTestingClElement();
67         var clElementDefinition = Mockito.mock(ToscaNodeTemplate.class);
68
69         assertDoesNotThrow(() -> handler
70                 .controlLoopElementUpdate(controlLoopId, element, clElementDefinition));
71
72         assertDoesNotThrow(() -> handler
73                 .handleStatistics(controlLoopElementId));
74     }
75
76     ControlLoopElementHandler getTestingHandler() {
77         var handler = new ControlLoopElementHandler();
78         var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class);
79         var element = getTestingClElement();
80         when(intermediaryApi.getControlLoopElement(controlLoopElementId)).thenReturn(element);
81         handler.setIntermediaryApi(intermediaryApi);
82         return handler;
83     }
84
85     ControlLoopElement getTestingClElement() {
86         var element = new ControlLoopElement();
87         element.setDefinition(controlLoopId);
88         element.setDescription("Description");
89         element.setId(controlLoopElementId);
90         element.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
91         element.setParticipantId(controlLoopId);
92         element.setState(ControlLoopState.UNINITIALISED);
93         var template = Mockito.mock(ToscaServiceTemplate.class);
94         element.setToscaServiceTemplateFragment(template);
95         return element;
96     }
97
98 }