0743e042930e245d583b945c3dc0dc1af9d7efd1
[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.clamp.models.acm.concepts.AutomationCompositionElement;
47 import org.onap.policy.models.base.PfModelException;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.springframework.test.context.junit.jupiter.SpringExtension;
51
52 @ExtendWith(SpringExtension.class)
53 class AcElementHandlerTest {
54
55     private final KserveClient kserveClient = mock(KserveClient.class);
56
57     @InjectMocks
58     @Spy
59     private AutomationCompositionElementHandler automationCompositionElementHandler =
60             new AutomationCompositionElementHandler(kserveClient);
61
62     @Mock
63     private ParticipantIntermediaryApi participantIntermediaryApi;
64
65     @Mock
66     private ExecutorService executor;
67     @Mock
68     private Future<String> result;
69
70     private final CommonTestData commonTestData = new CommonTestData();
71
72     private static ToscaServiceTemplate serviceTemplate;
73     private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
74             "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
75
76     @BeforeAll
77     static void init() {
78         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
79     }
80
81     @BeforeEach
82     void startMocks() throws KserveException, ExecutionException, InterruptedException, IOException, ApiException {
83         doReturn(true).when(kserveClient).deployInferenceService(any(), any());
84         doReturn(true).when(automationCompositionElementHandler)
85                 .checkInferenceServiceStatus(any(), any(), anyInt(), anyInt());
86     }
87
88     @Test
89     void test_automationCompositionElementStateChange() throws PfModelException {
90         var automationCompositionId = commonTestData.getAutomationCompositionId();
91         var element = commonTestData.getAutomationCompositionElement();
92         var automationCompositionElementId = element.getId();
93
94
95         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
96         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
97                 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
98
99         assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(automationCompositionId,
100                 automationCompositionElementId));
101
102     }
103
104     @Test
105     void test_AutomationCompositionElementUpdate() {
106         var element = commonTestData.getAutomationCompositionElement();
107
108         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
109         assertDoesNotThrow(
110                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
111                         nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
112     }
113
114     @Test
115     void test_checkInferenceServiceStatus() throws ExecutionException, InterruptedException {
116         doReturn(result).when(executor).submit(any(Runnable.class), any());
117         doReturn("Done").when(result).get();
118         doReturn(true).when(result).isDone();
119         ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier();
120         AutomationCompositionElement element = new AutomationCompositionElement();
121         assertDoesNotThrow(
122                 () -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris", "kserve-test", 1,
123                         1));
124     }
125 }