Merge "adding catalog-service for mod2"
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / service / MsInstanceServiceImplTest.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;
22
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;
43
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.Optional;
47
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.*;
53
54 @ExtendWith(MockitoExtension.class)
55 class MsInstanceServiceImplTest {
56
57     @Spy
58     private MsInstanceServiceImpl service = new MsInstanceServiceImpl();
59
60     @Mock
61     private MsInstanceGateway msInstanceRepository;
62
63     @Mock
64     private MsService msService;
65
66     @Mock
67     private SpecificationService specificationService;
68
69     @Mock
70     private DeploymentArtifactService deploymentArtifactService;
71
72     @Mock
73     private MsInstanceStatusChangeHandler msInstanceStatusChangeHandler;
74
75
76     @BeforeEach
77     void setUp() {
78         service.setMsService(msService);
79         service.setSpecificationService(specificationService);
80         service.setDeploymentArtifactService(deploymentArtifactService);
81         service.setMsInstanceRepository(msInstanceRepository);
82         service.setMsInstanceStatusChangeHandler(msInstanceStatusChangeHandler);
83     }
84
85     @Test
86     void getAll() {
87         MsInstance instance_1 = MsInstance.builder().id("123").build();
88         MsInstance instance_2 = MsInstance.builder().id("345").build();
89
90         when(msInstanceRepository.findAll()).thenReturn(Arrays.asList(instance_1, instance_2));
91
92         List<MsInstance> instances = service.getAll();
93
94         assertThat(instances.size()).isEqualTo(2);
95         verify(msInstanceRepository, times(1)).findAll();
96     }
97
98     @Test
99     void test_getMsInstanceById() throws Exception{
100         MsInstance expected = MsInstanceObjectMother.createMsInstance();
101
102         when(msInstanceRepository.findById(MS_INSTANCE_ID)).thenReturn(Optional.of(expected));
103
104         MsInstance original = service.getMsInstanceById(MS_INSTANCE_ID);
105
106         assertThat(original.getId()).isEqualTo(expected.getId());
107     }
108
109     @Test
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));
114     }
115
116     //TODO require cleaning and more assertions
117     @Test
118     void createMicroserviceInstance() {
119
120         BaseMicroservice microservice = BaseMsObjectMother.createMockMsObject();
121         MsInstanceRequest request = getMsInstanceMockRequest();
122         MsInstance msInstanceMockDao = createMsInstance();
123
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);
128
129         MsInstance msInstance = service.createMicroserviceInstance(BaseMsObjectMother.BASE_MS_NAME,request);
130
131         assertThat(msInstance.getId()).isEqualTo(msInstance.getId());
132         assertThat(msInstance.getName()).isEqualTo(msInstance.getName());
133         assertThat(msInstance.getMsInfo().keySet()).isEqualTo(msInstanceMockDao.getMsInfo().keySet());
134
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);
139
140     }
141
142     @Test
143     void test_updateMsInstance() {
144
145     }
146
147     @Test
148     void updateStatusBasedOnDeploymentArtifactsStatuses() {
149         MsInstance msInstance = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
150         when(msInstanceRepository.findById(msInstance.getId())).thenReturn(Optional.of(msInstance));
151
152         service.updateStatusBasedOnDeploymentArtifactsStatuses(msInstance.getId());
153
154         verify(msInstanceStatusChangeHandler, times(1)).updateStatusBasedOnDeploymentArtifactsStatuses(msInstance);
155         verify(service, times(1)).updateMsInstance(msInstance);
156
157     }
158
159     @Test
160     void test_removeDeploymentArtifactFromMsInstance() {
161         MsInstance msInstance = MsInstanceObjectMother.getMsInstanceWithExistingDeploymentArtifactRef();
162         DeploymentArtifact deploymentArtifact = createDeploymentArtifact(msInstance);
163
164         when(msInstanceRepository.findById(msInstance.getId())).thenReturn(Optional.of(msInstance));
165         //when(msInstanceStatusChangeHandler.updateStatusBasedOnDeploymentArtifactsStatuses(any())).thenReturn(msInstance);
166
167         service.removeDeploymentArtifactFromMsInstance(deploymentArtifact);
168
169         assertThat(msInstance.getDeploymentArtifactsInfo().getDeploymentArtifacts().contains(deploymentArtifact.getId())).isFalse();
170         verify(msInstanceStatusChangeHandler, times(1)).updateStatusBasedOnDeploymentArtifactsStatuses(msInstance);
171         verify(service, times(1)).updateMsInstance(msInstance);
172
173     }
174
175     @Test
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");
184
185         when(msInstanceRepository.findById("instance-1")).thenReturn(Optional.of(msInstance_1));
186         when(msInstanceRepository.findById("instance-2")).thenReturn(Optional.of(msInstance_2));
187
188         service.updateMicroserviceReference(microservice);
189
190         assertThat(msInstance_1.getName()).isEqualTo(microservice.getName());
191         assertThat(msInstance_2.getName()).isEqualTo(microservice.getName());
192
193         assertThat(msInstance_1.getMsInfo().get("name")).isEqualTo(microservice.getName());
194         assertThat(msInstance_2.getMsInfo().get("name")).isEqualTo(microservice.getName());
195
196         verify(service, times(2)).getMsInstanceById(anyString());
197         verify(msInstanceRepository, times(2)).save(any(MsInstance.class));
198     }
199
200     private DeploymentArtifact createDeploymentArtifact(MsInstance msInstance) {
201         DeploymentArtifact deploymentArtifact = new DeploymentArtifact();
202         deploymentArtifact.setId(msInstance.getDeploymentArtifactsInfo().getDeploymentArtifacts().get(0));
203
204         MsInstanceInfo msInstanceInfo = new MsInstanceInfo();
205         msInstanceInfo.setId(msInstance.getId());
206         deploymentArtifact.setMsInstanceInfo(msInstanceInfo);
207         return deploymentArtifact;
208     }
209 }