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.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;
52 @ExtendWith(SpringExtension.class)
53 class AcElementHandlerTest {
55 private final KserveClient kserveClient = mock(KserveClient.class);
59 private AutomationCompositionElementHandler automationCompositionElementHandler =
60 new AutomationCompositionElementHandler(kserveClient);
63 private ParticipantIntermediaryApi participantIntermediaryApi;
66 private ExecutorService executor;
68 private Future<String> result;
70 private final CommonTestData commonTestData = new CommonTestData();
72 private static ToscaServiceTemplate serviceTemplate;
73 private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
74 "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
78 serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
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());
89 void test_automationCompositionElementStateChange() throws PfModelException {
90 var automationCompositionId = commonTestData.getAutomationCompositionId();
91 var element = commonTestData.getAutomationCompositionElement();
92 var automationCompositionElementId = element.getId();
95 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
96 automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
97 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
99 assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(automationCompositionId,
100 automationCompositionElementId));
105 void test_AutomationCompositionElementUpdate() {
106 var element = commonTestData.getAutomationCompositionElement();
108 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
110 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
111 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
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();
122 () -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris", "kserve-test", 1,