Distributing Blueprint to DCAE Dashboard Issue-ID: DCAEGEN2-2385>
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / PolicyModelDistributionControllerTest.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.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;
36
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;
50
51 @ExtendWith(SpringExtension.class)
52 @WebMvcTest(PolicyModelDistributionController.class)
53 public class PolicyModelDistributionControllerTest {
54
55     @Autowired
56     MockMvc mockMvc;
57
58     @MockBean
59     private PolicyModelDistributionService mockPolicyModelDistributionService;
60
61     @BeforeEach
62     void setup() {
63     }
64
65     @Test
66     void test_getPolicyModelById() throws Exception {
67         PolicyModel policyModel = getPolicyModelResponse();
68         ResponseEntity mockResponseEntity = ResponseEntity.status(200).body(policyModel.getContent());
69
70        when(mockPolicyModelDistributionService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
71
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());
75
76         verify(mockPolicyModelDistributionService, times(1)).getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
77     }
78
79     @Test
80     void test_getPolicyModelById_BadRequest() throws Exception {
81         PolicyModel policyModel = getPolicyModelResponse();
82         ResponseEntity mockResponseEntity = ResponseEntity.status(400).body(policyModel.getContent());
83
84         when(mockPolicyModelDistributionService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
85
86         mockMvc.perform(get("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
87                 .contentType(MediaType.APPLICATION_JSON))
88                 .andExpect(status().is4xxClientError());
89
90         verify(mockPolicyModelDistributionService, times(0)).getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
91     }
92
93     @Test
94     void test_distributePolicyModelById_shouldReturn201AndResponseBody() throws Exception {
95         PolicyModel policyModel = getPolicyModelResponse();
96         ResponseEntity mockResponseEntity = ResponseEntity.status(200).body(policyModel.getContent());
97
98         when(mockPolicyModelDistributionService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
99
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);
104     }
105
106     @Test
107     void test_distributePolicyModelById_BadRequest() throws Exception {
108         PolicyModel policyModel = getPolicyModelResponse();
109         ResponseEntity mockResponseEntity = ResponseEntity.status(400).body(policyModel.getContent());
110
111         when(mockPolicyModelDistributionService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
112
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);
117     }
118
119
120 }