Merge "Refactor, fix code formatting and add unittests"
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / service / deploymentartifact / DeploymentArtifactServiceImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
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
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.dcaegen2.platform.mod.web.service.deploymentartifact;
22
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;
36
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Optional;
40
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.*;
46
47 @ExtendWith(MockitoExtension.class)
48 class DeploymentArtifactServiceImplTest {
49
50     private DeploymentArtifactServiceImpl deploymentArtifactService;
51
52     @Mock
53     private MsInstanceService msInstanceService;
54
55     @Mock
56     private DeploymentArtifactGeneratorStrategy deploymentArtifactGeneratorStrategy;
57
58     @Mock
59     private DeploymentArtifactGateway repository;
60
61     @Mock
62     private ArtifactFileNameCreator fileNameCreator;
63
64     @Mock
65     private DeploymentArtifactStatusChangeHandler deploymentArtifactStatusChangeHandler;
66
67     private MsInstance msInstance;
68
69     DeploymentArtifact deploymentArtifact;
70
71     @BeforeEach
72     void setUp() {
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);
80     }
81
82     private void setupMockBehaviours() {
83         //Mock methods
84         deploymentArtifact = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
85         msInstance = MsInstanceObjectMother.createMsInstance();
86
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");
92     }
93
94     @Test
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);
99     }
100
101     @Test
102     void test_GenerateBlueprint_shouldReturnCorrectBlueprint() throws Exception{
103
104         setupMockBehaviours();
105         when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
106
107         //act
108         DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
109
110         //assert
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);
122
123     }
124
125     @Test
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);
131     }
132
133     @Test
134     void test_deploymentVersionIncrementsForEachAddForAnInstance() throws Exception{
135         setupMockBehaviours();
136         MsInstance msInstanceWithRef = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
137         when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstanceWithRef);
138
139         DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
140
141         assertThat(resultDAO.getVersion()).isEqualTo(2);
142     }
143
144     @Test
145     void test_deploymentArtifactRefAddedToMsInstanceForFirstTime() throws Exception{
146         setupMockBehaviours();
147         when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstance);
148
149         DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
150         assertThat(msInstance.getDeploymentArtifactsInfo().getMostRecentVersion()).isEqualTo(1);
151
152         List<String> deploymentArtifactList = msInstance.getDeploymentArtifactsInfo().getDeploymentArtifacts();
153         assertThat(deploymentArtifactList.size()).isEqualTo(1);
154         assertThat(deploymentArtifactList.get(0)).isEqualTo(resultDAO.getId());
155     }
156
157     @Test
158     void test_deploymentArtifactRefAddedToMsInstanceForSecondTime() throws Exception{
159         setupMockBehaviours();
160         MsInstance msInstanceWithRef = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
161         when(msInstanceService.getMsInstanceById(MS_INSTANCE_ID)).thenReturn(msInstanceWithRef);
162
163         DeploymentArtifact resultDAO = deploymentArtifactService.generateDeploymentArtifact(MS_INSTANCE_ID, USER);
164         assertThat(msInstanceWithRef.getDeploymentArtifactsInfo().getMostRecentVersion()).isEqualTo(2);
165
166         List<String> deploymentArtifactList = msInstanceWithRef.getDeploymentArtifactsInfo().getDeploymentArtifacts();
167         assertThat(deploymentArtifactList.size()).isEqualTo(2);
168         assertThat(deploymentArtifactList.get(1)).isEqualTo(resultDAO.getId());
169     }
170
171     @Test
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);
177     }
178
179     @Test
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();
186     }
187
188     @Test
189     void test_updateStatusForDeploymentArtifact() throws Exception{
190         //arrange
191         deploymentArtifact = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
192
193         DeploymentArtifactPatchRequest dtoWithStatus = new DeploymentArtifactPatchRequest();
194         dtoWithStatus.setStatus(DeploymentArtifactStatus.NOT_NEEDED);
195
196         when(repository.findById("id-123")).thenReturn(Optional.of(deploymentArtifact));
197
198             //Mocking void method from DeploymentArtifactStatusChangeHandler
199         doAnswer(invocation -> {
200             deploymentArtifact.setStatus(DeploymentArtifactStatus.NOT_NEEDED);
201             return null;
202         }).when(deploymentArtifactStatusChangeHandler).handleStatusChange(dtoWithStatus.getStatus(), deploymentArtifact);
203
204         //act
205         deploymentArtifactService.updateDeploymentArtifact("id-123", dtoWithStatus, "user1");
206
207         //assert
208         assertThat(deploymentArtifact.getStatus()).isEqualTo(DeploymentArtifactStatus.NOT_NEEDED);
209         assertThat(deploymentArtifact.getMetadata().get("updatedBy")).isEqualTo("user1");
210         assertThat(deploymentArtifact.getMetadata().get("updatedOn")).isNotNull();
211
212         verify(deploymentArtifactStatusChangeHandler, times(1)).handleStatusChange(dtoWithStatus.getStatus(),
213                 deploymentArtifact);
214         verify(msInstanceService, times(1)).
215                 updateStatusBasedOnDeploymentArtifactsStatuses(deploymentArtifact.getMsInstanceInfo().getId());
216         verify(repository, times(1)).save(deploymentArtifact);
217     }
218
219     @Test
220     void test_findDeploymentArtifactById() throws Exception{
221         //arrange
222         deploymentArtifact = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
223         when(repository.findById("id-123")).thenReturn(Optional.of(deploymentArtifact));
224
225         DeploymentArtifact result = deploymentArtifactService.findDeploymentArtifactById("id-123");
226
227         assertThat(result).isEqualTo(deploymentArtifact);
228     }
229
230     @Test
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"));
235     }
236
237     @Test
238     void test_deleteDeploymentArtifact() throws Exception{
239         DeploymentArtifact deploymentArtifact =
240                 DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
241         String id = deploymentArtifact.getId();
242
243         when(repository.findById(id)).thenReturn(Optional.of(deploymentArtifact));
244
245         deploymentArtifactService.deleteDeploymentArtifact(id);
246         verify(msInstanceService, times(1))
247                 .removeDeploymentArtifactFromMsInstance(deploymentArtifact);
248         verify(repository, times(1)).deleteById(id);
249     }
250 }