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.deploymentartifact;
23 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
24 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactStatus;
25 import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.DeploymentArtifactNotFound;
26 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
27 import org.onap.dcaegen2.platform.mod.model.restapi.DeploymentArtifactPatchRequest;
28 import org.onap.dcaegen2.platform.mod.objectmothers.DeploymentArtifactObjectMother;
29 import org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother;
30 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceService;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.jupiter.MockitoExtension;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Optional;
41 import static org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother.*;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
44 import static org.mockito.ArgumentMatchers.any;
45 import static org.mockito.Mockito.*;
47 @ExtendWith(MockitoExtension.class)
48 class DeploymentArtifactServiceImplTest {
50 private DeploymentArtifactServiceImpl deploymentArtifactService;
53 private MsInstanceService msInstanceService;
56 private DeploymentArtifactGeneratorStrategy deploymentArtifactGeneratorStrategy;
59 private DeploymentArtifactGateway repository;
62 private ArtifactFileNameCreator fileNameCreator;
65 private DeploymentArtifactStatusChangeHandler deploymentArtifactStatusChangeHandler;
67 private MsInstance msInstance;
69 DeploymentArtifact deploymentArtifact;
73 //Initiated the deployment artifact core with mocks
74 deploymentArtifactService = new DeploymentArtifactServiceImpl();
75 deploymentArtifactService.setDeploymentArtifactGeneratorStrategy(deploymentArtifactGeneratorStrategy);
76 deploymentArtifactService.setDeploymentArtifactGateway(repository);
77 deploymentArtifactService.setMsInstanceService(msInstanceService);
78 deploymentArtifactService.setFileNameCreator(fileNameCreator);
79 deploymentArtifactService.setStatusChangeHandler(deploymentArtifactStatusChangeHandler);
82 private void setupMockBehaviours() {
84 deploymentArtifact = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
85 msInstance = MsInstanceObjectMother.createMsInstance();
87 when(deploymentArtifactGeneratorStrategy.generateForRelease(msInstance.getActiveSpec(), msInstance.getRelease()))
88 .thenReturn(DeploymentArtifactObjectMother.createBlueprintResponse());
89 when(repository.save(any())).thenReturn(deploymentArtifact);
90 when(fileNameCreator.createFileName(any(MsInstance.class), any(Integer.class))).thenReturn(BASE_MS_TAG + "_" +
91 msInstance.getActiveSpec().getType().toString().toLowerCase() + "_" + msInstance.getRelease() + "_1.yaml");
95 void test_getAllDeploymentArtifactInstance() throws Exception{
96 when(repository.findAll()).thenReturn(Arrays.asList(deploymentArtifact));
97 List<DeploymentArtifact> deployments = deploymentArtifactService.getAllDeploymentArtifacts();
98 assertThat(deployments.size()).isEqualTo(1);
102 void test_GenerateBlueprint_shouldReturnCorrectBlueprint() throws Exception{
104 setupMockBehaviours();
105 when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
108 DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
111 verify(msInstanceService, atLeastOnce()).getMsInstanceById(MS_INSTANCE_ID);
112 verify(repository, times(1)).save(any());
113 assertThat(resultDAO.getContent()).contains("tosca_definitions_version");
114 assertThat(resultDAO.getId()).isNotEmpty();
115 assertThat(resultDAO.getVersion()).isEqualTo(1);
116 assertThat(resultDAO.getStatus()).isEqualTo(DeploymentArtifactStatus.IN_DEV);
117 assertThat(resultDAO.getMsInstanceInfo().getId()).isEqualTo(MS_INSTANCE_ID);
118 assertThat(resultDAO.getMsInstanceInfo().getName()).isEqualTo(msInstance.getName());
119 assertThat(resultDAO.getMsInstanceInfo().getRelease()).isEqualTo(msInstance.getRelease());
120 assertThat(resultDAO.getSpecificationInfo().get("id")).isNotNull();
121 assertThat(resultDAO.getMetadata().get("createdBy")).isEqualTo(USER);
126 void test_deploymentVersionIsInitatedWith1() throws Exception{
127 setupMockBehaviours();
128 when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
129 DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
130 assertThat(resultDAO.getVersion()).isEqualTo(1);
134 void test_deploymentVersionIncrementsForEachAddForAnInstance() throws Exception{
135 setupMockBehaviours();
136 MsInstance msInstanceWithRef = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
137 when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstanceWithRef);
139 DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
141 assertThat(resultDAO.getVersion()).isEqualTo(2);
145 void test_deploymentArtifactRefAddedToMsInstanceForFirstTime() throws Exception{
146 setupMockBehaviours();
147 when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
149 DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
150 assertThat(msInstance.getDeploymentArtifactsInfo().getMostRecentVersion()).isEqualTo(1);
152 List<String> deploymentArtifactList = msInstance.getDeploymentArtifactsInfo().getDeploymentArtifacts();
153 assertThat(deploymentArtifactList.size()).isEqualTo(1);
154 assertThat(deploymentArtifactList.get(0)).isEqualTo(resultDAO.getId());
158 void test_deploymentArtifactRefAddedToMsInstanceForSecondTime() throws Exception{
159 setupMockBehaviours();
160 MsInstance msInstanceWithRef = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
161 when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstanceWithRef);
163 DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
164 assertThat(msInstanceWithRef.getDeploymentArtifactsInfo().getMostRecentVersion()).isEqualTo(2);
166 List<String> deploymentArtifactList = msInstanceWithRef.getDeploymentArtifactsInfo().getDeploymentArtifacts();
167 assertThat(deploymentArtifactList.size()).isEqualTo(2);
168 assertThat(deploymentArtifactList.get(1)).isEqualTo(resultDAO.getId());
172 void test_ifMsInstanceIsPersistedAfterDeploymentArtifactCreation() throws Exception{
173 setupMockBehaviours();
174 when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
175 deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
176 verify(msInstanceService, times(1)).updateMsInstance(msInstance);
180 void test_blueprintFileNameValidation() throws Exception{
181 setupMockBehaviours();
182 when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
183 DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
184 System.out.println(resultDAO.getFileName());
185 assertThat(resultDAO.getFileName().contains(BASE_MS_TAG)).isTrue();
189 void test_updateStatusForDeploymentArtifact() throws Exception{
191 deploymentArtifact = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
193 DeploymentArtifactPatchRequest dtoWithStatus = new DeploymentArtifactPatchRequest();
194 dtoWithStatus.setStatus(DeploymentArtifactStatus.NOT_NEEDED);
196 when(repository.findById("id-123")).thenReturn(Optional.of(deploymentArtifact));
198 //Mocking void method from DeploymentArtifactStatusChangeHandler
199 doAnswer(invocation -> {
200 deploymentArtifact.setStatus(DeploymentArtifactStatus.NOT_NEEDED);
202 }).when(deploymentArtifactStatusChangeHandler).handleStatusChange(dtoWithStatus.getStatus(), deploymentArtifact);
205 deploymentArtifactService.updateDeploymentArtifact("id-123", dtoWithStatus, "user1");
208 assertThat(deploymentArtifact.getStatus()).isEqualTo(DeploymentArtifactStatus.NOT_NEEDED);
209 assertThat(deploymentArtifact.getMetadata().get("updatedBy")).isEqualTo("user1");
210 assertThat(deploymentArtifact.getMetadata().get("updatedOn")).isNotNull();
212 verify(deploymentArtifactStatusChangeHandler, times(1)).handleStatusChange(dtoWithStatus.getStatus(),
214 verify(msInstanceService, times(1)).
215 updateStatusBasedOnDeploymentArtifactsStatuses(deploymentArtifact.getMsInstanceInfo().getId());
216 verify(repository, times(1)).save(deploymentArtifact);
220 void test_findDeploymentArtifactById() throws Exception{
222 deploymentArtifact = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
223 when(repository.findById("id-123")).thenReturn(Optional.of(deploymentArtifact));
225 DeploymentArtifact result = deploymentArtifactService.findDeploymentArtifactById("id-123");
227 assertThat(result).isEqualTo(deploymentArtifact);
231 void test_findByIdWithInvalidId() throws Exception{
232 when(repository.findById("invalid-id")).thenReturn(Optional.empty());
233 assertThatExceptionOfType(DeploymentArtifactNotFound.class).isThrownBy(
234 () -> deploymentArtifactService.findDeploymentArtifactById("invalid-id"));
238 void test_deleteDeploymentArtifact() throws Exception{
239 DeploymentArtifact deploymentArtifact =
240 DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
241 String id = deploymentArtifact.getId();
243 when(repository.findById(id)).thenReturn(Optional.of(deploymentArtifact));
245 deploymentArtifactService.deleteDeploymentArtifact(id);
246 verify(msInstanceService, times(1))
247 .removeDeploymentArtifactFromMsInstance(deploymentArtifact);
248 verify(repository, times(1)).deleteById(id);