Merge "Refactor, fix code formatting and add unittests"
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / service / MsServiceImplTest.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.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;
40
41 import java.util.*;
42
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.*;
49
50 @ExtendWith(MockitoExtension.class)
51 class MsServiceImplTest {
52
53     @Mock
54     private BaseMicroserviceGateway repository;
55
56     @Mock
57     private MsInstanceService msInstanceService;
58
59     @Spy
60     private MsServiceImpl baseMsService = new MsServiceImpl();
61
62     @BeforeEach
63     void setup() throws Exception{
64         baseMsService.setRepository(repository);
65         baseMsService.setMsInstanceService(msInstanceService);
66     }
67
68     /**GET MICROSERVICE TESTS*/
69     @Test
70     void getAll() {
71         //arrange
72         BaseMicroservice ms1 = new BaseMicroservice();
73         ms1.setName("HelloWorld1");
74         BaseMicroservice ms2 = new BaseMicroservice();
75         ms2.setName("HelloWorld2");
76
77         when(repository.findAll()).thenReturn(Arrays.asList(ms1, ms2));
78
79         //act
80         List<BaseMicroservice> microservices = baseMsService.getAllMicroservices();
81
82         //assert
83         assertThat(microservices).hasSizeGreaterThan(0);
84     }
85
86     @Test
87     void test_getMicroserviceById() throws Exception{
88         BaseMicroservice expectedMicroservice = BaseMsObjectMother.createMockMsObject();
89         String baseMsId = BaseMsObjectMother.BASE_MS_ID;
90
91         when(repository.findById(baseMsId)).thenReturn(Optional.of(expectedMicroservice));
92
93         BaseMicroservice resultMicroservice = baseMsService.getMicroserviceById(baseMsId);
94
95         assertThat(resultMicroservice).isEqualTo(expectedMicroservice);
96         verify(repository, times(1)).findById(baseMsId);
97     }
98
99     @Test
100     void test_ifMicroserviceNotFoundRaiseException() throws Exception{
101         BaseMicroservice expectedMicroservice = BaseMsObjectMother.createMockMsObject();
102         String baseMsId = BaseMsObjectMother.BASE_MS_ID;
103
104         when(repository.findById(baseMsId)).thenReturn(Optional.empty());
105
106         assertThatExceptionOfType(BaseMicroserviceNotFoundException.class).isThrownBy(
107                 () -> baseMsService.getMicroserviceById(baseMsId)
108         );
109     }
110
111     /**CREATE MICROSERVICE TESTS*/
112     @Test
113     void createMicroservice() {
114         //arrange
115         MicroserviceCreateRequest microserviceRequest = createMockMsRequest();
116         BaseMicroservice expected = createMockMsObject();
117
118         when(repository.save(any())).thenReturn(expected);
119
120         //act
121         BaseMicroservice actual = baseMsService.createMicroservice(microserviceRequest);
122
123         //assert
124         assertThat(actual.getMetadata().getCreatedBy()).isEqualTo(microserviceRequest.getUser());
125         assertThat(actual.getMetadata().getUpdatedBy()).isEqualTo(microserviceRequest.getUser());
126     }
127
128     @Test
129     void AddingMsWithDuplicateName_shouldThrowException() throws Exception{
130         //arrange
131         MicroserviceCreateRequest microserviceRequest = createMockMsRequest();
132         BaseMicroservice existedMicroservice = createMockMsObject();
133
134         when(repository.findByName(any())).thenReturn(Optional.of(existedMicroservice));
135
136         //act/assert
137         assertThatThrownBy(() -> baseMsService.createMicroservice((microserviceRequest)))
138                 .isInstanceOf(ResourceConflictException.class)
139                 .hasMessage(MICROSERVICE_NAME_CONFLICT_MESSAGE);
140     }
141     @Test
142     void AddingMsWithDuplicateTag_shouldThrowException() throws Exception{
143         //arrange
144         MicroserviceCreateRequest microserviceRequest = createMockMsRequest();
145         BaseMicroservice existedMicroservice = createMockMsObject();
146
147         when(repository.findByTag(any())).thenReturn(Optional.of(existedMicroservice));
148
149         //act/assert
150         assertThatThrownBy(() -> baseMsService.createMicroservice((microserviceRequest)))
151                 .isInstanceOf(ResourceConflictException.class)
152                 .hasMessage(MICROSERVICE_TAG_CONFLICT_MESSAGE);
153     }
154
155     /**UPDATE MICROSERVICE TESTS*/
156     @Test
157     void test_updateMicroservice() throws Exception{
158         MicroserviceUpdateRequest updateRequest = createUpdateMsRequest();
159
160         BaseMicroservice msToBeUpdated = BaseMsObjectMother.createMockMsObject();
161         Date updateTimeBefore = new Date(msToBeUpdated.getMetadata().getUpdatedOn().getTime());
162
163         String baseMsId = BaseMsObjectMother.BASE_MS_ID;
164
165         when(repository.findById(baseMsId)).thenReturn(Optional.of(msToBeUpdated));
166
167         baseMsService.updateMicroservice(baseMsId, updateRequest);
168
169         //assert
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);
174     }
175
176 /*    @Test
177     void test_msTagChangeShouldNotBeAllowed() throws Exception{
178         MicroserviceCreateRequest updateRequest = new MicroserviceCreateRequest();
179         updateRequest.setTag("updateTag");
180         String baseMsId = BaseMsObjectMother.BASE_MS_ID;
181
182         assertThatExceptionOfType(OperationNotAllowedException.class).isThrownBy(
183                 () -> baseMsService.updateMicroservice(baseMsId, updateRequest)
184         );
185     }*/
186
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());
194
195         assertThat(msToBeUpdated.getMetadata().getUpdatedBy()).isEqualTo(updateRequest.getUser());
196         assertThat(msToBeUpdated.getMetadata().getUpdatedOn()).isNotEqualTo(updateTimeBefore);
197
198         assertThat(msToBeUpdated.getMetadata().getNotes()).isEqualTo(updateRequest.getMetadata().get("notes"));
199         assertThat(msToBeUpdated.getMetadata().getLabels()).isEqualTo(updateRequest.getMetadata().get("labels"));
200     }
201
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");
210
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;
216     }
217 }