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