summaryrefslogtreecommitdiffstats
path: root/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/BaseMicroserviceControllerTest.java
blob: c89b4dc0b0c98248ac7115f414d8c2e70f30c527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * ============LICENSE_START=======================================================
 *  org.onap.dcae
 *  ================================================================================
 *  Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============LICENSE_END=========================================================
 */

package org.onap.dcaegen2.platform.mod.web;

import org.onap.dcaegen2.platform.mod.model.basemicroservice.BaseMicroservice;
import org.onap.dcaegen2.platform.mod.model.exceptions.OperationNotAllowedException;
import org.onap.dcaegen2.platform.mod.model.exceptions.ResourceConflictException;
import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceCreateRequest;
import org.onap.dcaegen2.platform.mod.model.restapi.MicroserviceUpdateRequest;
import org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother;
import org.onap.dcaegen2.platform.mod.web.controller.BaseMicroserviceController;
import org.onap.dcaegen2.platform.mod.web.service.basemicroservice.MsService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.Arrays;

import static org.onap.dcaegen2.platform.mod.model.exceptions.ErrorMessages.MICROSERVICE_NAME_CONFLICT_MESSAGE;

@ExtendWith(SpringExtension.class)
@WebMvcTest(BaseMicroserviceController.class)
class BaseMicroserviceControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private MsService mockBaseMsService;

    @BeforeEach
    void setUp() {
    }

    @Test
    void test_GetAllBaseMicroservices_returnsListOfDTOs() throws Exception {
        //arrange
        BaseMicroservice ms1 = new BaseMicroservice();
        ms1.setName("HelloWorld1");
        BaseMicroservice ms2 = new BaseMicroservice();
        ms2.setName("HelloWorld2");

        Mockito.when(mockBaseMsService.getAllMicroservices()).thenReturn(Arrays.asList(ms1, ms2));

        //act/assert
        mockMvc.perform(MockMvcRequestBuilders.get("/api/base-microservice")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2)));
    }

    @Test
    void test_addBaseMicroservice_returnsMicroservice() throws Exception {
        //arrange
        MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();

        //response
        BaseMicroservice microserviceDao = BaseMsObjectMother.createMockMsObject();

        Mockito.when(mockBaseMsService.createMicroservice(microserviceRequest)).thenReturn(microserviceDao);

        //act/assert
        mockMvc.perform(MockMvcRequestBuilders.post("/api/base-microservice")
                .contentType(MediaType.APPLICATION_JSON)
                .content(BaseMsObjectMother.asJsonString(microserviceRequest))
                .characterEncoding("utf-8"))
                .andExpect(MockMvcResultMatchers.status().isCreated())
                .andExpect((MockMvcResultMatchers.jsonPath("$.id", Matchers.equalTo(BaseMsObjectMother.BASE_MS_ID))))
                .andExpect(MockMvcResultMatchers.jsonPath("$.name", Matchers.equalTo(BaseMsObjectMother.BASE_MS_NAME)))
                .andExpect(MockMvcResultMatchers.jsonPath("$.metadata.createdBy", Matchers.equalTo(BaseMsObjectMother.USER)));
    }

    @Test
    void test_addBaseMicroserviceWithDuplicateName_shouldThrowConflictError() throws Exception{
        //arrange
        MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();
        Mockito.when(mockBaseMsService.createMicroservice(ArgumentMatchers.any())).thenThrow(new ResourceConflictException(MICROSERVICE_NAME_CONFLICT_MESSAGE));

        //act/assert
        mockMvc.perform(MockMvcRequestBuilders.post("/api/base-microservice")
                .contentType(MediaType.APPLICATION_JSON)
                .content(BaseMsObjectMother.asJsonString(microserviceRequest)))
                .andExpect(MockMvcResultMatchers.status().isConflict());
    }

    @Test
    void test_updateBaseMicroserviceEndpoint() throws Exception{
        MicroserviceUpdateRequest microserviceRequest = BaseMsObjectMother.createUpdateMsRequest();
        String requestedMsId = "id-123";

        mockMvc.perform(MockMvcRequestBuilders.patch(String.format(BaseMicroserviceController.API_BASE_MICROSERVICE + "/%s", requestedMsId))
                .contentType(MediaType.APPLICATION_JSON)
                .content(BaseMsObjectMother.asJsonString(microserviceRequest))
                .characterEncoding("utf-8"))
                .andExpect(MockMvcResultMatchers.status().isNoContent());
        Mockito.verify(mockBaseMsService, Mockito.times(1)).updateMicroservice(requestedMsId, microserviceRequest);
    }

    @Test
    void test_OperationNotAllowedExceptionThrows409() throws Exception{
        MicroserviceUpdateRequest microserviceRequest = BaseMsObjectMother.createUpdateMsRequest();
        String requestedMsId = "id-123";
        Mockito.doThrow(new OperationNotAllowedException("")).
                when(mockBaseMsService).updateMicroservice(requestedMsId, microserviceRequest);

        mockMvc.perform(MockMvcRequestBuilders.patch(String.format(BaseMicroserviceController.API_BASE_MICROSERVICE + "/%s", requestedMsId))
                .contentType(MediaType.APPLICATION_JSON)
                .content(BaseMsObjectMother.asJsonString(microserviceRequest)))
                .andExpect(MockMvcResultMatchers.status().isConflict());
    }

    @Test
    void test_validateMsRequestShouldThrowCorrectResponse() throws Exception {
        //arrange
        MicroserviceCreateRequest microserviceRequest = BaseMsObjectMother.createMockMsRequest();
        microserviceRequest.setName(" ");
        microserviceRequest.setTag("123");
        microserviceRequest.setServiceName("123");
        microserviceRequest.setUser(" ");

        //response
        BaseMicroservice microserviceDao = BaseMsObjectMother.createMockMsObject();

        Mockito.when(mockBaseMsService.createMicroservice(microserviceRequest)).thenReturn(microserviceDao);

        //act/assert
        mockMvc.perform(MockMvcRequestBuilders.post("/api/base-microservice")
                .contentType(MediaType.APPLICATION_JSON)
                .content(BaseMsObjectMother.asJsonString(microserviceRequest))
                .characterEncoding("utf-8"))
                .andExpect(MockMvcResultMatchers.status().isBadRequest())
                .andExpect(MockMvcResultMatchers.jsonPath("$.message", Matchers.equalTo("Validation failed.")))
                .andExpect(MockMvcResultMatchers.jsonPath("$.errors", Matchers.hasSize(4)))
                ;
    }
}