Distributing Blueprint to DCAE Dashboard Issue-ID: DCAEGEN2-2385>
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / MicroserviceInstanceControllerTest.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;
22
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;
40
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.Map;
44
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;
53
54 @ExtendWith(SpringExtension.class)
55 @WebMvcTest(MicroserviceInstanceController.class)
56 class MicroserviceInstanceControllerTest {
57
58     @MockBean
59     MsInstanceService service;
60
61     @Autowired
62     MockMvc mockMvc;
63
64     @BeforeEach
65     void setUp() {
66     }
67
68     @Test
69     void getAll() throws Exception {
70         MsInstance instance_1 = MsInstance.builder().id("123").build();
71         MsInstance instance_2 = MsInstance.builder().id("345").build();
72
73         when(service.getAll()).thenReturn(Arrays.asList(instance_1,instance_2));
74
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();
80     }
81
82     @Test
83     void createMsInstance_shouldReturn201AndResponseBody() throws Exception {
84
85         MsInstanceRequest request = getMsInstanceMockRequest();
86         MsInstance msInstance = createMsInstance();
87
88         when(service.createMicroserviceInstance(BaseMsObjectMother.BASE_MS_NAME, request)).thenReturn(msInstance);
89
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)));
95
96         verify(service, times(1)).createMicroserviceInstance(BaseMsObjectMother.BASE_MS_NAME,request);
97     }
98
99     @Test
100     void patchMsInstance_shouldReturn204NoContent() throws Exception{
101         //given
102         String updatedVersion = "updatedVersion";
103         String updatedRelease = "updatedRelease";
104
105         MsInstance mockedMsInstance = prepareMockMsInstance(updatedVersion, updatedRelease);
106         String msInstanceId = mockedMsInstance.getId();
107
108         MsInstanceUpdateRequest updateRequest = prepareMsInstanceUpdateRequest(updatedVersion, updatedRelease);
109
110        when(service.updateMsInstance(updateRequest, msInstanceId)).thenReturn(mockedMsInstance);
111
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")));
119
120        verify(service, times(1)).updateMsInstance(updateRequest, msInstanceId);
121     }
122
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;
129     }
130
131     private Map<String, Object> prepareMetadataToBeUpdated() {
132         Map<String, Object> metadata = new HashMap<>();
133         metadata.put("scrumLead", "updatedScrumLead");
134         return metadata;
135     }
136
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;
143     }
144
145 }