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.basemicroservice.BaseMsLocation;
25 import org.onap.dcaegen2.platform.mod.model.basemicroservice.BaseMsType;
26 import org.onap.dcaegen2.platform.mod.model.exceptions.ResourceConflictException;
27 import org.onap.dcaegen2.platform.mod.model.exceptions.basemicroservice.BaseMicroserviceNotFoundException;
28 import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceCreateRequest;
29 import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceUpdateRequest;
30 import org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother;
31 import org.onap.dcaegen2.platform.mod.web.service.basemicroservice.BaseMicroserviceGateway;
32 import org.onap.dcaegen2.platform.mod.web.service.basemicroservice.MsServiceImpl;
33 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceService;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.Mock;
38 import org.mockito.Spy;
39 import org.mockito.junit.jupiter.MockitoExtension;
43 import static org.onap.dcaegen2.platform.mod.model.exceptions.ErrorMessages.MICROSERVICE_NAME_CONFLICT_MESSAGE;
44 import static org.onap.dcaegen2.platform.mod.model.exceptions.ErrorMessages.MICROSERVICE_TAG_CONFLICT_MESSAGE;
45 import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.createMockMsObject;
46 import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.createMockMsRequest;
47 import static org.assertj.core.api.Assertions.*;
48 import static org.mockito.Mockito.*;
50 @ExtendWith(MockitoExtension.class)
51 class MsServiceImplTest {
54 private BaseMicroserviceGateway repository;
57 private MsInstanceService msInstanceService;
60 private MsServiceImpl baseMsService = new MsServiceImpl();
63 void setup() throws Exception{
64 baseMsService.setRepository(repository);
65 baseMsService.setMsInstanceService(msInstanceService);
68 /**GET MICROSERVICE TESTS*/
72 BaseMicroservice ms1 = new BaseMicroservice();
73 ms1.setName("HelloWorld1");
74 BaseMicroservice ms2 = new BaseMicroservice();
75 ms2.setName("HelloWorld2");
77 when(repository.findAll()).thenReturn(Arrays.asList(ms1, ms2));
80 List<BaseMicroservice> microservices = baseMsService.getAllMicroservices();
83 assertThat(microservices).hasSizeGreaterThan(0);
87 void test_getMicroserviceById() throws Exception{
88 BaseMicroservice expectedMicroservice = BaseMsObjectMother.createMockMsObject();
89 String baseMsId = BaseMsObjectMother.BASE_MS_ID;
91 when(repository.findById(baseMsId)).thenReturn(Optional.of(expectedMicroservice));
93 BaseMicroservice resultMicroservice = baseMsService.getMicroserviceById(baseMsId);
95 assertThat(resultMicroservice).isEqualTo(expectedMicroservice);
96 verify(repository, times(1)).findById(baseMsId);
100 void test_ifMicroserviceNotFoundRaiseException() throws Exception{
101 BaseMicroservice expectedMicroservice = BaseMsObjectMother.createMockMsObject();
102 String baseMsId = BaseMsObjectMother.BASE_MS_ID;
104 when(repository.findById(baseMsId)).thenReturn(Optional.empty());
106 assertThatExceptionOfType(BaseMicroserviceNotFoundException.class).isThrownBy(
107 () -> baseMsService.getMicroserviceById(baseMsId)
111 /**CREATE MICROSERVICE TESTS*/
113 void createMicroservice() {
115 MicroserviceCreateRequest microserviceRequest = createMockMsRequest();
116 BaseMicroservice expected = createMockMsObject();
118 when(repository.save(any())).thenReturn(expected);
121 BaseMicroservice actual = baseMsService.createMicroservice(microserviceRequest);
124 assertThat(actual.getMetadata().getCreatedBy()).isEqualTo(microserviceRequest.getUser());
125 assertThat(actual.getMetadata().getUpdatedBy()).isEqualTo(microserviceRequest.getUser());
129 void AddingMsWithDuplicateName_shouldThrowException() throws Exception{
131 MicroserviceCreateRequest microserviceRequest = createMockMsRequest();
132 BaseMicroservice existedMicroservice = createMockMsObject();
134 when(repository.findByName(any())).thenReturn(Optional.of(existedMicroservice));
137 assertThatThrownBy(() -> baseMsService.createMicroservice((microserviceRequest)))
138 .isInstanceOf(ResourceConflictException.class)
139 .hasMessage(MICROSERVICE_NAME_CONFLICT_MESSAGE);
142 void AddingMsWithDuplicateTag_shouldThrowException() throws Exception{
144 MicroserviceCreateRequest microserviceRequest = createMockMsRequest();
145 BaseMicroservice existedMicroservice = createMockMsObject();
147 when(repository.findByTag(any())).thenReturn(Optional.of(existedMicroservice));
150 assertThatThrownBy(() -> baseMsService.createMicroservice((microserviceRequest)))
151 .isInstanceOf(ResourceConflictException.class)
152 .hasMessage(MICROSERVICE_TAG_CONFLICT_MESSAGE);
155 /**UPDATE MICROSERVICE TESTS*/
157 void test_updateMicroservice() throws Exception{
158 MicroserviceUpdateRequest updateRequest = createUpdateMsRequest();
160 BaseMicroservice msToBeUpdated = BaseMsObjectMother.createMockMsObject();
161 Date updateTimeBefore = new Date(msToBeUpdated.getMetadata().getUpdatedOn().getTime());
163 String baseMsId = BaseMsObjectMother.BASE_MS_ID;
165 when(repository.findById(baseMsId)).thenReturn(Optional.of(msToBeUpdated));
167 baseMsService.updateMicroservice(baseMsId, updateRequest);
170 assertUpdatedMsFileds(updateRequest, msToBeUpdated, updateTimeBefore);
171 verify(baseMsService, times(1)).getMicroserviceById(baseMsId);
172 verify(msInstanceService, times(1)).updateMicroserviceReference(msToBeUpdated);
173 verify(repository, times(1)).save(msToBeUpdated);
177 void test_msTagChangeShouldNotBeAllowed() throws Exception{
178 MicroserviceCreateRequest updateRequest = new MicroserviceCreateRequest();
179 updateRequest.setTag("updateTag");
180 String baseMsId = BaseMsObjectMother.BASE_MS_ID;
182 assertThatExceptionOfType(OperationNotAllowedException.class).isThrownBy(
183 () -> baseMsService.updateMicroservice(baseMsId, updateRequest)
187 private void assertUpdatedMsFileds(MicroserviceUpdateRequest updateRequest, BaseMicroservice msToBeUpdated,
188 Date updateTimeBefore) {
189 assertThat(msToBeUpdated.getName()).isEqualTo(updateRequest.getName());
190 assertThat(msToBeUpdated.getLocation()).isEqualTo(updateRequest.getLocation());
191 assertThat(msToBeUpdated.getServiceName()).isEqualTo(updateRequest.getServiceName());
192 assertThat(msToBeUpdated.getNamespace()).isEqualTo(updateRequest.getNamespace());
193 assertThat(msToBeUpdated.getType()).isEqualTo(updateRequest.getType());
195 assertThat(msToBeUpdated.getMetadata().getUpdatedBy()).isEqualTo(updateRequest.getUser());
196 assertThat(msToBeUpdated.getMetadata().getUpdatedOn()).isNotEqualTo(updateTimeBefore);
198 assertThat(msToBeUpdated.getMetadata().getNotes()).isEqualTo(updateRequest.getMetadata().get("notes"));
199 assertThat(msToBeUpdated.getMetadata().getLabels()).isEqualTo(updateRequest.getMetadata().get("labels"));
202 private MicroserviceUpdateRequest createUpdateMsRequest() {
203 MicroserviceUpdateRequest updateRequest = new MicroserviceUpdateRequest();
204 updateRequest.setName("updatedName");
205 updateRequest.setLocation(BaseMsLocation.EDGE);
206 updateRequest.setServiceName("updatedServiceName");
207 updateRequest.setNamespace("updatedNameSpace");
208 updateRequest.setType(BaseMsType.ANALYTIC);
209 updateRequest.setUser("updater");
211 Map<String, Object> metadata = new HashMap();
212 metadata.put("notes", "updatedNote");
213 metadata.put("labels", Arrays.asList("updatedLabel1", "updatedLabel2"));
214 updateRequest.setMetadata(metadata);
215 return updateRequest;