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.hamcrest.Matchers;
24 import org.junit.Ignore;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel;
29 import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest;
30 import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest;
31 import org.onap.dcaegen2.platform.mod.web.controller.PolicyModelController;
32 import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelService;
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;
40 import java.util.Arrays;
42 import static org.hamcrest.Matchers.notNullValue;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.when;
46 import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.USER;
47 import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.asJsonString;
48 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.POLICY_MODEL_ID;
49 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelCreateRequest;
50 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelResponse;
51 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelUpdateRequest;
52 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
53 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
54 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
55 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
56 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
58 @ExtendWith(SpringExtension.class)
59 @WebMvcTest(PolicyModelController.class)
60 public class PolicyModelControllerTest {
66 private PolicyModelService mockPolicyModelService;
73 void test_getAllPolicyModels() throws Exception {
74 PolicyModel policyModel = getPolicyModelResponse();
76 when(mockPolicyModelService.getAll()).thenReturn(Arrays.asList(policyModel));
78 mockMvc.perform(get("/api/policy-model")
79 .contentType(MediaType.APPLICATION_JSON))
80 .andExpect(status().isOk())
81 .andExpect(jsonPath("$", Matchers.hasSize(1)));
85 void test_getPolicyModelById() throws Exception {
86 PolicyModel policyModel = getPolicyModelResponse();
88 when(mockPolicyModelService.getPolicyModelById(POLICY_MODEL_ID)).thenReturn(policyModel);
90 mockMvc.perform(get("/api/policy-model/" + POLICY_MODEL_ID)
91 .contentType(MediaType.APPLICATION_JSON))
92 .andExpect(status().isOk())
93 .andExpect(jsonPath("$.id", notNullValue()));
97 void test_patchPolicyModel_shouldReturn204AndResponseBody() throws Exception{
98 PolicyModelUpdateRequest policyModelpdateRequest = getPolicyModelUpdateRequest();
99 PolicyModel policyModel = getPolicyModelResponse();
101 when(mockPolicyModelService.updatePolicyModel(policyModelpdateRequest,POLICY_MODEL_ID, USER)).thenReturn(policyModel);
103 mockMvc.perform(patch("/api/policy-model/" + POLICY_MODEL_ID).param("user", USER)
104 .contentType(MediaType.APPLICATION_JSON)
105 .content(asJsonString(policyModelpdateRequest)).accept(MediaType.APPLICATION_JSON))
106 .andExpect(status().isBadRequest());
107 verify(mockPolicyModelService, times(0)).updatePolicyModel(policyModelpdateRequest,POLICY_MODEL_ID, USER);