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.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel;
27 import org.onap.dcaegen2.platform.mod.web.controller.PolicyModelDistributionController;
28 import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelDistributionService;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
31 import org.springframework.boot.test.mock.mockito.MockBean;
32 import org.springframework.http.MediaType;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.test.context.junit.jupiter.SpringExtension;
35 import org.springframework.test.web.servlet.MockMvc;
37 import static org.hamcrest.Matchers.notNullValue;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41 import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.USER;
42 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_ENV;
43 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_MODEL_ID;
44 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_MODEL_NAME;
45 import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelResponse;
46 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
47 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
48 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
49 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
51 @ExtendWith(SpringExtension.class)
52 @WebMvcTest(PolicyModelDistributionController.class)
53 public class PolicyModelDistributionControllerTest {
59 private PolicyModelDistributionService mockPolicyModelDistributionService;
66 void test_getPolicyModelById() throws Exception {
67 PolicyModel policyModel = getPolicyModelResponse();
68 ResponseEntity mockResponseEntity = ResponseEntity.status(200).body(policyModel.getContent());
70 when(mockPolicyModelDistributionService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
72 mockMvc.perform(get("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
73 .param("env",PM_DISTRIBUTION_ENV).contentType(MediaType.APPLICATION_JSON))
74 .andExpect(status().isOk());
76 verify(mockPolicyModelDistributionService, times(1)).getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
80 void test_getPolicyModelById_BadRequest() throws Exception {
81 PolicyModel policyModel = getPolicyModelResponse();
82 ResponseEntity mockResponseEntity = ResponseEntity.status(400).body(policyModel.getContent());
84 when(mockPolicyModelDistributionService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
86 mockMvc.perform(get("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
87 .contentType(MediaType.APPLICATION_JSON))
88 .andExpect(status().is4xxClientError());
90 verify(mockPolicyModelDistributionService, times(0)).getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
94 void test_distributePolicyModelById_shouldReturn201AndResponseBody() throws Exception {
95 PolicyModel policyModel = getPolicyModelResponse();
96 ResponseEntity mockResponseEntity = ResponseEntity.status(200).body(policyModel.getContent());
98 when(mockPolicyModelDistributionService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
100 mockMvc.perform(post("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
101 .param("env",PM_DISTRIBUTION_ENV).contentType(MediaType.APPLICATION_JSON))
102 .andExpect(status().isOk());
103 verify(mockPolicyModelDistributionService, times(1)).distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
107 void test_distributePolicyModelById_BadRequest() throws Exception {
108 PolicyModel policyModel = getPolicyModelResponse();
109 ResponseEntity mockResponseEntity = ResponseEntity.status(400).body(policyModel.getContent());
111 when(mockPolicyModelDistributionService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
113 mockMvc.perform(post("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
114 .contentType(MediaType.APPLICATION_JSON))
115 .andExpect(status().is4xxClientError());
116 verify(mockPolicyModelDistributionService, times(0)).distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);