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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.kserve.handler;
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;
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;
50 @ExtendWith(SpringExtension.class)
51 class AcElementHandlerTest {
53 private final KserveClient kserveClient = mock(KserveClient.class);
57 private AutomationCompositionElementHandler automationCompositionElementHandler =
58 new AutomationCompositionElementHandler(kserveClient);
61 private ParticipantIntermediaryApi participantIntermediaryApi;
64 private ExecutorService executor;
66 private Future<String> result;
68 private final CommonTestData commonTestData = new CommonTestData();
70 private static ToscaServiceTemplate serviceTemplate;
71 private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
72 "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
76 serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
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());
87 void test_automationCompositionElementStateChange() throws PfModelException {
88 var automationCompositionId = commonTestData.getAutomationCompositionId();
89 var element = commonTestData.getAutomationCompositionElement();
90 var automationCompositionElementId = element.getId();
93 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
94 automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
95 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
97 assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(automationCompositionId,
98 automationCompositionElementId));
103 void test_AutomationCompositionElementUpdate() {
104 var element = commonTestData.getAutomationCompositionElement();
106 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
108 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
109 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
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();
118 () -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris", "kserve-test", 1,