2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.platform.mod.web.service;
23 import org.onap.dcaegen2.platform.mod.model.basemicroservice.BaseMicroservice;
24 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
25 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.MsInstanceInfo;
26 import org.onap.dcaegen2.platform.mod.model.exceptions.msinstance.MsInstanceNotFoundException;
27 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
28 import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceRequest;
29 import org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother;
30 import org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother;
31 import org.onap.dcaegen2.platform.mod.web.service.basemicroservice.MsService;
32 import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactService;
33 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceGateway;
34 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceServiceImpl;
35 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceStatusChangeHandler;
36 import org.onap.dcaegen2.platform.mod.web.service.specification.SpecificationService;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.extension.ExtendWith;
40 import org.mockito.Mock;
41 import org.mockito.Spy;
42 import org.mockito.junit.jupiter.MockitoExtension;
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.Optional;
48 import static org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother.*;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
51 import static org.mockito.ArgumentMatchers.any;
52 import static org.mockito.Mockito.*;
54 @ExtendWith(MockitoExtension.class)
55 class MsInstanceServiceImplTest {
58 private MsInstanceServiceImpl service = new MsInstanceServiceImpl();
61 private MsInstanceGateway msInstanceRepository;
64 private MsService msService;
67 private SpecificationService specificationService;
70 private DeploymentArtifactService deploymentArtifactService;
73 private MsInstanceStatusChangeHandler msInstanceStatusChangeHandler;
78 service.setMsService(msService);
79 service.setSpecificationService(specificationService);
80 service.setDeploymentArtifactService(deploymentArtifactService);
81 service.setMsInstanceRepository(msInstanceRepository);
82 service.setMsInstanceStatusChangeHandler(msInstanceStatusChangeHandler);
87 MsInstance instance_1 = MsInstance.builder().id("123").build();
88 MsInstance instance_2 = MsInstance.builder().id("345").build();
90 when(msInstanceRepository.findAll()).thenReturn(Arrays.asList(instance_1, instance_2));
92 List<MsInstance> instances = service.getAll();
94 assertThat(instances.size()).isEqualTo(2);
95 verify(msInstanceRepository, times(1)).findAll();
99 void test_getMsInstanceById() throws Exception{
100 MsInstance expected = MsInstanceObjectMother.createMsInstance();
102 when(msInstanceRepository.findById(MS_INSTANCE_ID)).thenReturn(Optional.of(expected));
104 MsInstance original = service.getMsInstanceById(MS_INSTANCE_ID);
106 assertThat(original.getId()).isEqualTo(expected.getId());
110 void test_msIntanceNotFound_willRaiseException() throws Exception{
111 when(msInstanceRepository.findById(MS_INSTANCE_ID)).thenReturn(Optional.empty());
112 assertThatExceptionOfType(MsInstanceNotFoundException.class).isThrownBy(
113 () -> service.getMsInstanceById(MS_INSTANCE_ID));
116 //TODO require cleaning and more assertions
118 void createMicroserviceInstance() {
120 BaseMicroservice microservice = BaseMsObjectMother.createMockMsObject();
121 MsInstanceRequest request = getMsInstanceMockRequest();
122 MsInstance msInstanceMockDao = createMsInstance();
124 when(msService.getMicroserviceByName(BaseMsObjectMother.BASE_MS_NAME)).thenReturn(microservice);
125 when(msInstanceRepository.findByNameAndRelease(request.getName(), request.getRelease()))
126 .thenReturn(Optional.empty());
127 when(msInstanceRepository.save(any())).thenReturn(msInstanceMockDao);
129 MsInstance msInstance = service.createMicroserviceInstance(BaseMsObjectMother.BASE_MS_NAME,request);
131 assertThat(msInstance.getId()).isEqualTo(msInstance.getId());
132 assertThat(msInstance.getName()).isEqualTo(msInstance.getName());
133 assertThat(msInstance.getMsInfo().keySet()).isEqualTo(msInstanceMockDao.getMsInfo().keySet());
135 verify(msService, times(1)).getMicroserviceByName(BaseMsObjectMother.BASE_MS_NAME);
136 verify(msInstanceRepository, times(1)).save(any(MsInstance.class));
137 verify(msService, times(1)).
138 saveMsInstanceReferenceToMs(microservice, msInstance);
143 void test_updateMsInstance() {
148 void updateStatusBasedOnDeploymentArtifactsStatuses() {
149 MsInstance msInstance = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
150 when(msInstanceRepository.findById(msInstance.getId())).thenReturn(Optional.of(msInstance));
152 service.updateStatusBasedOnDeploymentArtifactsStatuses(msInstance.getId());
154 verify(msInstanceStatusChangeHandler, times(1)).updateStatusBasedOnDeploymentArtifactsStatuses(msInstance);
155 verify(service, times(1)).updateMsInstance(msInstance);
160 void test_removeDeploymentArtifactFromMsInstance() {
161 MsInstance msInstance = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
162 DeploymentArtifact deploymentArtifact = createDeploymentArtifact(msInstance);
164 when(msInstanceRepository.findById(msInstance.getId())).thenReturn(Optional.of(msInstance));
165 //when(msInstanceStatusChangeHandler.updateStatusBasedOnDeploymentArtifactsStatuses(any())).thenReturn(msInstance);
167 service.removeDeploymentArtifactFromMsInstance(deploymentArtifact);
169 assertThat(msInstance.getDeploymentArtifactsInfo().getDeploymentArtifacts().contains(deploymentArtifact.getId())).isFalse();
170 verify(msInstanceStatusChangeHandler, times(1)).updateStatusBasedOnDeploymentArtifactsStatuses(msInstance);
171 verify(service, times(1)).updateMsInstance(msInstance);
176 void updateMicroserviceReference() throws Exception{
177 BaseMicroservice microservice = BaseMsObjectMother.createMockMsObject();
178 MsInstance msInstance_1 = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
179 msInstance_1.setId("instance-1");
180 msInstance_1.getMsInfo().put("name", "old-ms");
181 MsInstance msInstance_2 = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
182 msInstance_2.setId("instance-2");
183 msInstance_2.getMsInfo().put("name", "old-ms");
185 when(msInstanceRepository.findById("instance-1")).thenReturn(Optional.of(msInstance_1));
186 when(msInstanceRepository.findById("instance-2")).thenReturn(Optional.of(msInstance_2));
188 service.updateMicroserviceReference(microservice);
190 assertThat(msInstance_1.getName()).isEqualTo(microservice.getName());
191 assertThat(msInstance_2.getName()).isEqualTo(microservice.getName());
193 assertThat(msInstance_1.getMsInfo().get("name")).isEqualTo(microservice.getName());
194 assertThat(msInstance_2.getMsInfo().get("name")).isEqualTo(microservice.getName());
196 verify(service, times(2)).getMsInstanceById(anyString());
197 verify(msInstanceRepository, times(2)).save(any(MsInstance.class));
200 private DeploymentArtifact createDeploymentArtifact(MsInstance msInstance) {
201 DeploymentArtifact deploymentArtifact = new DeploymentArtifact();
202 deploymentArtifact.setId(msInstance.getDeploymentArtifactsInfo().getDeploymentArtifacts().get(0));
204 MsInstanceInfo msInstanceInfo = new MsInstanceInfo();
205 msInstanceInfo.setId(msInstance.getId());
206 deploymentArtifact.setMsInstanceInfo(msInstanceInfo);
207 return deploymentArtifact;