add junit coverage
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / aai / tasks / AAICommonTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Modifications Copyright (c) 2021 Nokia
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.aai.tasks;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 import java.util.Optional;
27 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.so.bpmn.common.BuildingBlockExecution;
35 import org.onap.so.bpmn.common.DelegateExecutionImpl;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
37 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
38 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
39 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
40 import org.onap.so.client.exception.BBObjectNotFoundException;
41 import org.onap.so.client.exception.ExceptionBuilder;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class AAICommonTasksTest {
45
46     private static final String SERVICE_TYPE = "testService";
47
48     @Mock
49     private ExtractPojosForBB extractPojosForBBMock;
50     @Mock
51     private ExceptionBuilder exceptionBuilder;
52     @InjectMocks
53     private AAICommonTasks testedObject;
54
55     private ServiceInstance serviceInstance;
56     private BuildingBlockExecution buildingBlockExecution;
57
58     @Before
59     public void setup() {
60         serviceInstance = new ServiceInstance();
61         buildingBlockExecution = new DelegateExecutionImpl(new DelegateExecutionFake());
62     }
63
64     @Test
65     public void getServiceType_success() throws Exception {
66         // given
67         ModelInfoServiceInstance modelInfoServiceInstance = new ModelInfoServiceInstance();
68         modelInfoServiceInstance.setServiceType(SERVICE_TYPE);
69         serviceInstance.setModelInfoServiceInstance(modelInfoServiceInstance);
70         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.SERVICE_INSTANCE_ID))
71                 .thenReturn(serviceInstance);
72         // when
73         Optional<String> resultOpt = testedObject.getServiceType(buildingBlockExecution);
74         // then
75         assertThat(resultOpt).isNotEmpty();
76         String result = resultOpt.get();
77         assertThat(result).isEqualTo(SERVICE_TYPE);
78     }
79
80     @Test
81     public void getServiceType_emptyWhenServiceInstanceModelIsNull() throws Exception {
82         // given
83         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.SERVICE_INSTANCE_ID))
84                 .thenReturn(serviceInstance);
85         // when
86         Optional<String> result = testedObject.getServiceType(buildingBlockExecution);
87         // then
88         assertThat(result).isEmpty();
89     }
90
91     @Test
92     public void getServiceType_exceptionHandling() throws Exception {
93         // given
94         BBObjectNotFoundException exception = new BBObjectNotFoundException();
95         when(extractPojosForBBMock.extractByKey(buildingBlockExecution, ResourceKey.SERVICE_INSTANCE_ID))
96                 .thenThrow(exception);
97         // when
98         testedObject.getServiceType(buildingBlockExecution);
99         // then
100         verify(exceptionBuilder).buildAndThrowWorkflowException(buildingBlockExecution, 7000, exception);
101     }
102 }