Update policy committers in policy/api
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / TestNodeTemplateController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2022-2024 Nordix Foundation. 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest;
24
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.when;
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
32
33 import jakarta.ws.rs.core.Response;
34 import java.util.List;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.policy.api.main.service.ToscaServiceTemplateService;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
46 import org.springframework.boot.test.mock.mockito.MockBean;
47 import org.springframework.http.MediaType;
48 import org.springframework.test.annotation.DirtiesContext;
49 import org.springframework.test.context.ActiveProfiles;
50 import org.springframework.test.web.servlet.MockMvc;
51 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
52 import org.springframework.web.context.WebApplicationContext;
53
54 @WebMvcTest(controllers = NodeTemplateController.class)
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
56 @ActiveProfiles({"default", "test-mvc"})
57 class TestNodeTemplateController {
58
59     @Autowired
60     private MockMvc mvc;
61
62     @MockBean
63     private ToscaServiceTemplateService toscaServiceTemplateService;
64
65     AutoCloseable autoCloseable;
66
67     private static final PfModelException PF_MODEL_EXCEPTION =
68         new PfModelException(Response.Status.BAD_REQUEST, "Error");
69
70     @BeforeEach
71     void setUp(@Autowired WebApplicationContext context) {
72         autoCloseable = MockitoAnnotations.openMocks(this);
73         mvc = MockMvcBuilders.webAppContextSetup(context).build();
74     }
75
76     @AfterEach
77     void tearDown() throws Exception {
78         autoCloseable.close();
79     }
80
81     @Test
82     void createToscaNodeTemplates() throws Exception {
83         var body = ResourceUtils.getResourceAsString("nodetemplates/nodetemplates.metadatasets.input.tosca.json");
84         var createRequest = post("/nodetemplates").contentType(MediaType.APPLICATION_JSON).content(body);
85         when(toscaServiceTemplateService.createToscaNodeTemplates(any(ToscaServiceTemplate.class)))
86             .thenReturn(new ToscaServiceTemplate());
87         mvc.perform(createRequest).andExpect(status().isCreated());
88     }
89
90     @Test
91     void createToscaNodeTemplates_Exception() throws Exception {
92         var body = ResourceUtils.getResourceAsString("nodetemplates/nodetemplates.metadatasets.input.tosca.json");
93         var createRequest = post("/nodetemplates").contentType(MediaType.APPLICATION_JSON).content(body);
94         when(toscaServiceTemplateService.createToscaNodeTemplates(any(ToscaServiceTemplate.class)))
95             .thenThrow(PF_MODEL_EXCEPTION);
96         mvc.perform(createRequest).andExpect(status().isBadRequest());
97     }
98
99     @Test
100     void updateToscaNodeTemplates() throws Exception {
101         var body = ResourceUtils.getResourceAsString("nodetemplates/nodetemplates.metadatasets.input.tosca.json");
102         var updateRequest = put("/nodetemplates").contentType(MediaType.APPLICATION_JSON).content(body);
103         when(toscaServiceTemplateService.updateToscaNodeTemplates(any(ToscaServiceTemplate.class)))
104             .thenReturn(new ToscaServiceTemplate());
105         mvc.perform(updateRequest).andExpect(status().isOk());
106     }
107
108     @Test
109     void updateToscaNodeTemplates_Exception() throws Exception {
110         var body = ResourceUtils.getResourceAsString("nodetemplates/nodetemplates.metadatasets.input.tosca.json");
111         var updateRequest = put("/nodetemplates").contentType(MediaType.APPLICATION_JSON).content(body);
112         when(toscaServiceTemplateService.updateToscaNodeTemplates(any(ToscaServiceTemplate.class)))
113             .thenThrow(PF_MODEL_EXCEPTION);
114         mvc.perform(updateRequest).andExpect(status().isBadRequest());
115     }
116
117     @Test
118     void deleteToscaNodeTemplates() throws Exception {
119         var deleteRequest = delete("/nodetemplates/nodeName/versions/nodeVersion")
120             .accept(MediaType.APPLICATION_JSON);
121         when(toscaServiceTemplateService.deleteToscaNodeTemplate("nodeName", "nodeVersion"))
122             .thenReturn(new ToscaServiceTemplate());
123         mvc.perform(deleteRequest).andExpect(status().isOk());
124     }
125
126     @Test
127     void deleteToscaNodeTemplates_Exception() throws Exception {
128         var deleteRequest = delete("/nodetemplates/nodeName/versions/nodeVersion")
129             .accept(MediaType.APPLICATION_JSON);
130         when(toscaServiceTemplateService.deleteToscaNodeTemplate("nodeName", "nodeVersion"))
131             .thenThrow(PF_MODEL_EXCEPTION);
132         mvc.perform(deleteRequest).andExpect(status().isBadRequest());
133     }
134
135     @Test
136     void getSpecificVersionOfNodeTemplate() throws Exception {
137         var getRequest = get("/nodetemplates/nodeName/versions/nodeVersion")
138             .accept(MediaType.APPLICATION_JSON);
139         when(toscaServiceTemplateService.fetchToscaNodeTemplates("nodeName", "nodeVersion"))
140             .thenReturn(List.of(new ToscaNodeTemplate()));
141         mvc.perform(getRequest).andExpect(status().isOk());
142     }
143
144     @Test
145     void getSpecificVersionOfNodeTemplate_Exception() throws Exception {
146         var getRequest = get("/nodetemplates/nodeName/versions/nodeVersion")
147             .accept(MediaType.APPLICATION_JSON);
148         when(toscaServiceTemplateService.fetchToscaNodeTemplates("nodeName", "nodeVersion"))
149             .thenThrow(PF_MODEL_EXCEPTION);
150         mvc.perform(getRequest).andExpect(status().isBadRequest());
151     }
152
153     @Test
154     void getAllNodeTemplates() throws Exception {
155         var getRequest = get("/nodetemplates")
156             .accept(MediaType.APPLICATION_JSON);
157         when(toscaServiceTemplateService.fetchToscaNodeTemplates(null, null))
158             .thenReturn(List.of(new ToscaNodeTemplate()));
159         mvc.perform(getRequest).andExpect(status().isOk());
160     }
161
162     @Test
163     void getAllNodeTemplates_Exception() throws Exception {
164         var getRequest = get("/nodetemplates")
165             .accept(MediaType.APPLICATION_JSON);
166         when(toscaServiceTemplateService.fetchToscaNodeTemplates(null, null))
167             .thenThrow(PF_MODEL_EXCEPTION);
168         mvc.perform(getRequest).andExpect(status().isBadRequest());
169     }
170 }