63ad3ef145de2f97cb75e9fd05333a7a3c28f454
[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.UUID;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Future;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Spy;
45 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
46 import org.onap.policy.clamp.acm.participant.kserve.exception.KserveException;
47 import org.onap.policy.clamp.acm.participant.kserve.k8s.KserveClient;
48 import org.onap.policy.clamp.acm.participant.kserve.utils.CommonTestData;
49 import org.onap.policy.clamp.acm.participant.kserve.utils.ToscaUtils;
50 import org.onap.policy.models.base.PfModelException;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 import org.springframework.test.context.junit.jupiter.SpringExtension;
53
54 @ExtendWith(SpringExtension.class)
55 class AcElementHandlerTest {
56
57     private final KserveClient kserveClient = mock(KserveClient.class);
58
59     @InjectMocks
60     @Spy
61     private AutomationCompositionElementHandler automationCompositionElementHandler =
62             new AutomationCompositionElementHandler(kserveClient);
63
64     @Mock
65     private ParticipantIntermediaryApi participantIntermediaryApi;
66
67     @Mock
68     private ExecutorService executor;
69     @Mock
70     private Future<String> result;
71
72     private final CommonTestData commonTestData = new CommonTestData();
73
74     private static ToscaServiceTemplate serviceTemplate;
75     private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
76             "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
77
78     @BeforeAll
79     static void init() {
80         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
81     }
82
83     @BeforeEach
84     void startMocks() throws ExecutionException, InterruptedException, IOException, ApiException {
85         doReturn(true).when(kserveClient).deployInferenceService(any(), any());
86         doReturn(true).when(automationCompositionElementHandler)
87                 .checkInferenceServiceStatus(any(), any(), anyInt(), anyInt());
88     }
89
90     @Test
91     void test_automationCompositionElementStateChange() throws PfModelException {
92         var automationCompositionId = commonTestData.getAutomationCompositionId();
93         var element = commonTestData.getAutomationCompositionElement();
94         var automationCompositionElementId = element.getId();
95
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(
112                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
113                         nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
114         assertThat(automationCompositionElementHandler.getConfigRequestMap()).hasSize(1)
115                 .containsKey(element.getId());
116
117         doThrow(new ApiException("Error installing the inference service")).when(kserveClient)
118                 .deployInferenceService(any(), any());
119
120         var elementId2 = UUID.randomUUID();
121         element.setId(elementId2);
122         assertThrows(KserveException.class,
123                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
124                         nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
125
126         assertThat(automationCompositionElementHandler.getConfigRequestMap().containsKey(elementId2)).isFalse();
127     }
128
129     @Test
130     void test_checkInferenceServiceStatus() throws ExecutionException, InterruptedException {
131         doReturn(result).when(executor).submit(any(Runnable.class), any());
132         doReturn("Done").when(result).get();
133         doReturn(true).when(result).isDone();
134         assertDoesNotThrow(
135                 () -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris", "kserve-test", 1,
136                         1));
137     }
138 }