d8a896ef8a9612b4c7062caba0020bc92b91613f
[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.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28
29 import io.kubernetes.client.openapi.ApiException;
30 import java.io.IOException;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Future;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.Spy;
41 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
42 import org.onap.policy.clamp.acm.participant.kserve.exception.KserveException;
43 import org.onap.policy.clamp.acm.participant.kserve.k8s.KserveClient;
44 import org.onap.policy.clamp.acm.participant.kserve.utils.CommonTestData;
45 import org.onap.policy.clamp.acm.participant.kserve.utils.ToscaUtils;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.springframework.test.context.junit.jupiter.SpringExtension;
49
50 @ExtendWith(SpringExtension.class)
51 class AcElementHandlerTest {
52
53     private final KserveClient kserveClient = mock(KserveClient.class);
54
55     @InjectMocks
56     @Spy
57     private AutomationCompositionElementHandler automationCompositionElementHandler =
58             new AutomationCompositionElementHandler(kserveClient);
59
60     @Mock
61     private ParticipantIntermediaryApi participantIntermediaryApi;
62
63     @Mock
64     private ExecutorService executor;
65     @Mock
66     private Future<String> result;
67
68     private final CommonTestData commonTestData = new CommonTestData();
69
70     private static ToscaServiceTemplate serviceTemplate;
71     private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
72             "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
73
74     @BeforeAll
75     static void init() {
76         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
77     }
78
79     @BeforeEach
80     void startMocks() throws KserveException, ExecutionException, InterruptedException, IOException, ApiException {
81         doReturn(true).when(kserveClient).deployInferenceService(any(), any());
82         doReturn(true).when(automationCompositionElementHandler)
83                 .checkInferenceServiceStatus(any(), any(), anyInt(), anyInt());
84     }
85
86     @Test
87     void test_automationCompositionElementStateChange() throws PfModelException {
88         var automationCompositionId = commonTestData.getAutomationCompositionId();
89         var element = commonTestData.getAutomationCompositionElement();
90         var automationCompositionElementId = element.getId();
91
92
93         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
94         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
95                 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
96
97         assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(automationCompositionId,
98                 automationCompositionElementId));
99
100     }
101
102     @Test
103     void test_AutomationCompositionElementUpdate() {
104         var element = commonTestData.getAutomationCompositionElement();
105
106         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
107         assertDoesNotThrow(
108                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
109                         nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
110     }
111
112     @Test
113     void test_checkInferenceServiceStatus() throws ExecutionException, InterruptedException {
114         doReturn(result).when(executor).submit(any(Runnable.class), any());
115         doReturn("Done").when(result).get();
116         doReturn(true).when(result).isDone();
117         assertDoesNotThrow(
118                 () -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris", "kserve-test", 1,
119                         1));
120     }
121 }