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;
23 import org.onap.dcaegen2.platform.mod.model.microserviceinstance.MsInstance;
24 import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceRequest;
25 import org.onap.dcaegen2.platform.mod.model.restapi.MsInstanceUpdateRequest;
26 import org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother;
27 import org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother;
28 import org.onap.dcaegen2.platform.mod.web.controller.MicroserviceInstanceController;
29 import org.onap.dcaegen2.platform.mod.web.service.microserviceinstance.MsInstanceService;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
35 import org.springframework.boot.test.mock.mockito.MockBean;
36 import org.springframework.http.MediaType;
37 import org.springframework.test.context.junit.jupiter.SpringExtension;
38 import org.springframework.test.web.servlet.MockMvc;
39 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
41 import java.util.Arrays;
42 import java.util.HashMap;
45 import static org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother.*;
46 import static org.hamcrest.Matchers.equalTo;
47 import static org.hamcrest.Matchers.hasSize;
48 import static org.mockito.Mockito.*;
49 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
50 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
51 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
52 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
54 @ExtendWith(SpringExtension.class)
55 @WebMvcTest(MicroserviceInstanceController.class)
56 class MicroserviceInstanceControllerTest {
59 MsInstanceService service;
69 void getAll() throws Exception {
70 MsInstance instance_1 = MsInstance.builder().id("123").build();
71 MsInstance instance_2 = MsInstance.builder().id("345").build();
73 when(service.getAll()).thenReturn(Arrays.asList(instance_1,instance_2));
75 mockMvc.perform(get("/api/microservice-instance")
76 .contentType(MediaType.APPLICATION_JSON))
77 .andExpect(status().isOk())
78 .andExpect(jsonPath("$",hasSize(2)));
79 verify(service, times(1)).getAll();
83 void createMsInstance_shouldReturn201AndResponseBody() throws Exception {
85 MsInstanceRequest request = getMsInstanceMockRequest();
86 MsInstance msInstance = createMsInstance();
88 when(service.createMicroserviceInstance(BaseMsObjectMother.BASE_MS_NAME, request)).thenReturn(msInstance);
90 mockMvc.perform(MockMvcRequestBuilders.post("/api/microservice-instance/"+ BaseMsObjectMother.BASE_MS_NAME)
91 .contentType(MediaType.APPLICATION_JSON)
92 .content(BaseMsObjectMother.asJsonString(request)).accept(MediaType.APPLICATION_JSON))
93 .andExpect(status().isCreated())
94 .andExpect(jsonPath("$.name",equalTo(MS_INSTANCE_NAME)));
96 verify(service, times(1)).createMicroserviceInstance(BaseMsObjectMother.BASE_MS_NAME,request);
100 void patchMsInstance_shouldReturn204NoContent() throws Exception{
102 String updatedVersion = "updatedVersion";
103 String updatedRelease = "updatedRelease";
105 MsInstance mockedMsInstance = prepareMockMsInstance(updatedVersion, updatedRelease);
106 String msInstanceId = mockedMsInstance.getId();
108 MsInstanceUpdateRequest updateRequest = prepareMsInstanceUpdateRequest(updatedVersion, updatedRelease);
110 when(service.updateMsInstance(updateRequest, msInstanceId)).thenReturn(mockedMsInstance);
112 mockMvc.perform(patch("/api/microservice-instance/" + msInstanceId)
113 .contentType(MediaType.APPLICATION_JSON)
114 .content(BaseMsObjectMother.asJsonString(updateRequest)))
115 .andExpect(status().isOk())
116 .andExpect(jsonPath("$.release", equalTo(updatedRelease)))
117 .andExpect(jsonPath("$.version", equalTo(updatedVersion)))
118 .andExpect(jsonPath("$.metadata.scrumLead", equalTo("updatedScrumLead")));
120 verify(service, times(1)).updateMsInstance(updateRequest, msInstanceId);
123 private MsInstanceUpdateRequest prepareMsInstanceUpdateRequest(String updatedVersion, String updatedRelease) {
124 MsInstanceUpdateRequest updateRequest = new MsInstanceUpdateRequest();
125 updateRequest.setRelease(updatedRelease);
126 updateRequest.setVersion(updatedVersion);
127 updateRequest.setMetadata(prepareMetadataToBeUpdated());
128 return updateRequest;
131 private Map<String, Object> prepareMetadataToBeUpdated() {
132 Map<String, Object> metadata = new HashMap<>();
133 metadata.put("scrumLead", "updatedScrumLead");
137 private MsInstance prepareMockMsInstance(String updatedVersion, String updatedRelease) {
138 MsInstance msInstanceToBeUpdated = MsInstanceObjectMother.createMsInstance();
139 msInstanceToBeUpdated.setVersion(updatedVersion);
140 msInstanceToBeUpdated.setRelease(updatedRelease);
141 msInstanceToBeUpdated.getMetadata().put("scrumLead", "updatedScrumLead");
142 return msInstanceToBeUpdated;