9efe2e27324288fe9be23247e0f4ea983745266c
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 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.acm.participant.intermediary.api.impl;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Map;
30 import java.util.UUID;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
33 import org.onap.policy.clamp.acm.participant.intermediary.handler.AutomationCompositionOutHandler;
34 import org.onap.policy.clamp.acm.participant.intermediary.handler.CacheProvider;
35 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
41 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45
46 class ParticipantIntermediaryApiImplTest {
47
48     private static final String USE_STATE = "useState";
49     private static final String OPERATIONAL_STATE = "operationState";
50     private static final Map<String, Object> MAP = Map.of("key", 1);
51     private static final UUID AUTOMATION_COMPOSITION_ID = UUID.randomUUID();
52     private static final UUID ELEMENT_ID = UUID.randomUUID();
53     private static final UUID COMPOSITION_ID = UUID.randomUUID();
54     private static final ToscaConceptIdentifier DEFINITION_ELEMENT_ID = new ToscaConceptIdentifier("code", "0.0.1");
55
56     @Test
57     void testUpdateAutomationCompositionElementState() {
58         var automationComposiitonHandler = mock(AutomationCompositionOutHandler.class);
59         var apiImpl = new ParticipantIntermediaryApiImpl(automationComposiitonHandler, mock(CacheProvider.class));
60         apiImpl.updateAutomationCompositionElementState(AUTOMATION_COMPOSITION_ID, ELEMENT_ID, DeployState.UNDEPLOYED,
61                 LockState.NONE, StateChangeResult.NO_ERROR, null);
62         verify(automationComposiitonHandler).updateAutomationCompositionElementState(AUTOMATION_COMPOSITION_ID,
63                 ELEMENT_ID, DeployState.UNDEPLOYED, LockState.NONE, StateChangeResult.NO_ERROR, null);
64     }
65
66     @Test
67     void testUpdateCompositionState() {
68         var automationComposiitonHandler = mock(AutomationCompositionOutHandler.class);
69         var apiImpl = new ParticipantIntermediaryApiImpl(automationComposiitonHandler, mock(CacheProvider.class));
70         apiImpl.updateCompositionState(COMPOSITION_ID, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "");
71         verify(automationComposiitonHandler).updateCompositionState(COMPOSITION_ID, AcTypeState.PRIMED,
72                 StateChangeResult.NO_ERROR, "");
73     }
74
75     @Test
76     void testSendAcElementInfo() {
77         var automationComposiitonHandler = mock(AutomationCompositionOutHandler.class);
78         var apiImpl = new ParticipantIntermediaryApiImpl(automationComposiitonHandler, mock(CacheProvider.class));
79         apiImpl.sendAcElementInfo(AUTOMATION_COMPOSITION_ID, ELEMENT_ID, USE_STATE, OPERATIONAL_STATE, MAP);
80         verify(automationComposiitonHandler).sendAcElementInfo(AUTOMATION_COMPOSITION_ID, ELEMENT_ID, USE_STATE,
81                 OPERATIONAL_STATE, MAP);
82     }
83
84     @Test
85     void testSendAcDefinitionInfo() {
86         var automationComposiitonHandler = mock(AutomationCompositionOutHandler.class);
87         var apiImpl = new ParticipantIntermediaryApiImpl(automationComposiitonHandler, mock(CacheProvider.class));
88         apiImpl.sendAcDefinitionInfo(COMPOSITION_ID, DEFINITION_ELEMENT_ID, MAP);
89         verify(automationComposiitonHandler).sendAcDefinitionInfo(COMPOSITION_ID, DEFINITION_ELEMENT_ID, MAP);
90     }
91
92     @Test
93     void testGetAutomationCompositionElement() {
94         var automationComposition = new AutomationComposition();
95         automationComposition.setInstanceId(AUTOMATION_COMPOSITION_ID);
96         var map = Map.of(AUTOMATION_COMPOSITION_ID, automationComposition);
97         var cacheProvider = mock(CacheProvider.class);
98         when(cacheProvider.getAutomationCompositions()).thenReturn(map);
99         var acElement = new AutomationCompositionElement();
100         acElement.setId(ELEMENT_ID);
101         automationComposition.setElements(Map.of(ELEMENT_ID, acElement));
102
103         var automationComposiitonHandler = mock(AutomationCompositionOutHandler.class);
104         var apiImpl = new ParticipantIntermediaryApiImpl(automationComposiitonHandler, cacheProvider);
105         var mapResult = apiImpl.getAutomationCompositions();
106         assertEquals(map, mapResult);
107
108         var result = apiImpl.getAutomationComposition(UUID.randomUUID());
109         assertThat(result).isNull();
110
111         result = apiImpl.getAutomationComposition(AUTOMATION_COMPOSITION_ID);
112         assertEquals(automationComposition, result);
113
114         var element = apiImpl.getAutomationCompositionElement(UUID.randomUUID(), UUID.randomUUID());
115         assertThat(element).isNull();
116
117         element = apiImpl.getAutomationCompositionElement(AUTOMATION_COMPOSITION_ID, UUID.randomUUID());
118         assertThat(element).isNull();
119
120         element = apiImpl.getAutomationCompositionElement(AUTOMATION_COMPOSITION_ID, ELEMENT_ID);
121         assertEquals(acElement, element);
122     }
123
124     @Test
125     void testGetAcElementsDefinitions() {
126         var cacheProvider = mock(CacheProvider.class);
127         var acElementDefinition = new AutomationCompositionElementDefinition();
128         acElementDefinition.setAcElementDefinitionId(DEFINITION_ELEMENT_ID);
129         acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(new ToscaNodeTemplate());
130         var elementsDefinitions = Map.of(DEFINITION_ELEMENT_ID, acElementDefinition);
131         var map = Map.of(COMPOSITION_ID, elementsDefinitions);
132         when(cacheProvider.getAcElementsDefinitions()).thenReturn(map);
133         var automationComposiitonHandler = mock(AutomationCompositionOutHandler.class);
134         var apiImpl = new ParticipantIntermediaryApiImpl(automationComposiitonHandler, cacheProvider);
135         var mapResult = apiImpl.getAcElementsDefinitions();
136         assertEquals(map, mapResult);
137
138         var result = apiImpl.getAcElementsDefinitions(UUID.randomUUID());
139         assertThat(result).isEmpty();
140
141         result = apiImpl.getAcElementsDefinitions(COMPOSITION_ID);
142         assertEquals(elementsDefinitions, result);
143
144         var element = apiImpl.getAcElementDefinition(UUID.randomUUID(), new ToscaConceptIdentifier("wrong", "0.0.1"));
145         assertThat(element).isNull();
146
147         element = apiImpl.getAcElementDefinition(COMPOSITION_ID, new ToscaConceptIdentifier("wrong", "0.0.1"));
148         assertThat(element).isNull();
149
150         element = apiImpl.getAcElementDefinition(COMPOSITION_ID, DEFINITION_ELEMENT_ID);
151         assertEquals(acElementDefinition, element);
152     }
153
154     @Test
155     void testInstanceElementDto() {
156         // test InstanceElementDto with toscaServiceTemplateFragment
157         var instanceElementDto = new InstanceElementDto(COMPOSITION_ID, ELEMENT_ID, new ToscaServiceTemplate(),
158                 Map.of(), Map.of());
159         assertEquals(COMPOSITION_ID, instanceElementDto.instanceId());
160         assertEquals(ELEMENT_ID, instanceElementDto.elementId());
161     }
162 }