6112488bf45199ab62a745d06298749e33d9eb9a
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.kserve.handler;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.mock;
31
32 import io.kubernetes.client.openapi.ApiException;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.UUID;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Future;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Spy;
47 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
48 import org.onap.policy.clamp.acm.participant.kserve.exception.KserveException;
49 import org.onap.policy.clamp.acm.participant.kserve.k8s.KserveClient;
50 import org.onap.policy.clamp.acm.participant.kserve.utils.CommonTestData;
51 import org.onap.policy.clamp.acm.participant.kserve.utils.ToscaUtils;
52 import org.onap.policy.models.base.PfModelException;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
54 import org.springframework.test.context.junit.jupiter.SpringExtension;
55
56 @ExtendWith(SpringExtension.class)
57 class AcElementHandlerTest {
58
59     private final KserveClient kserveClient = mock(KserveClient.class);
60
61     private ParticipantIntermediaryApi participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
62
63     @InjectMocks
64     @Spy
65     private AutomationCompositionElementHandler automationCompositionElementHandler =
66             new AutomationCompositionElementHandler(participantIntermediaryApi, kserveClient);
67
68     @Mock
69     private ExecutorService executor;
70     @Mock
71     private Future<String> result;
72
73     private final CommonTestData commonTestData = new CommonTestData();
74
75     private static ToscaServiceTemplate serviceTemplate;
76     private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
77             "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
78
79     @BeforeAll
80     static void init() {
81         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
82     }
83
84     @BeforeEach
85     void startMocks() throws ExecutionException, InterruptedException, IOException, ApiException {
86         doReturn(true).when(kserveClient).deployInferenceService(any(), any());
87         doReturn(true).when(automationCompositionElementHandler).checkInferenceServiceStatus(any(), any(), anyInt(),
88                 anyInt());
89     }
90
91     @Test
92     void test_automationCompositionElementStateChange() throws PfModelException {
93         var automationCompositionId = commonTestData.getAutomationCompositionId();
94         var element = commonTestData.getAutomationCompositionElement();
95         var automationCompositionElementId = element.getId();
96
97         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
98         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
99                 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
100
101         assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(automationCompositionId,
102                 automationCompositionElementId));
103
104     }
105
106     @Test
107     void test_AutomationCompositionElementUpdate() throws IOException, ApiException {
108         var element = commonTestData.getAutomationCompositionElement();
109
110         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
111         assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(),
112                 element, nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
113         assertThat(automationCompositionElementHandler.getConfigRequestMap()).hasSize(1).containsKey(element.getId());
114
115         doThrow(new ApiException("Error installing the inference service")).when(kserveClient)
116                 .deployInferenceService(any(), any());
117
118         var elementId2 = UUID.randomUUID();
119         element.setId(elementId2);
120         assertThrows(KserveException.class,
121                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
122                         nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
123
124         assertThat(automationCompositionElementHandler.getConfigRequestMap().containsKey(elementId2)).isFalse();
125     }
126
127     @Test
128     void test_checkInferenceServiceStatus() throws ExecutionException, InterruptedException {
129         doReturn(result).when(executor).submit(any(Runnable.class), any());
130         doReturn("Done").when(result).get();
131         doReturn(true).when(result).isDone();
132         assertDoesNotThrow(() -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris",
133                 "kserve-test", 1, 1));
134     }
135
136     @Test
137     void testUpdate() throws PfModelException {
138         var automationCompositionId = commonTestData.getAutomationCompositionId();
139         var element = commonTestData.getAutomationCompositionElement();
140         assertDoesNotThrow(
141                 () -> automationCompositionElementHandler.update(automationCompositionId, element, Map.of()));
142     }
143
144     @Test
145     void testLock() throws PfModelException {
146         assertDoesNotThrow(() -> automationCompositionElementHandler.lock(UUID.randomUUID(), UUID.randomUUID()));
147     }
148
149     @Test
150     void testUnlock() throws PfModelException {
151         assertDoesNotThrow(() -> automationCompositionElementHandler.unlock(UUID.randomUUID(), UUID.randomUUID()));
152     }
153
154     @Test
155     void testDelete() throws PfModelException {
156         assertDoesNotThrow(() -> automationCompositionElementHandler.delete(UUID.randomUUID(), UUID.randomUUID()));
157     }
158
159     @Test
160     void testPrime() throws PfModelException {
161         assertDoesNotThrow(() -> automationCompositionElementHandler.prime(UUID.randomUUID(), List.of()));
162     }
163
164     @Test
165     void testDeprime() throws PfModelException {
166         assertDoesNotThrow(() -> automationCompositionElementHandler.deprime(UUID.randomUUID()));
167     }
168 }