1d19b1a1701dc3ee86914328b114da34a1c0eda9
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.element.rest;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.when;
29 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
30 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.nio.charset.StandardCharsets;
36 import javax.ws.rs.core.Response;
37 import org.apache.commons.io.FileUtils;
38 import org.json.JSONObject;
39 import org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.onap.policy.clamp.acm.element.main.parameters.AcElement;
44 import org.onap.policy.clamp.acm.element.main.rest.AcElementController;
45 import org.onap.policy.clamp.acm.element.service.ConfigService;
46 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
47 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
48 import org.onap.policy.common.utils.coder.Coder;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.boot.context.properties.EnableConfigurationProperties;
53 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
54 import org.springframework.boot.test.mock.mockito.MockBean;
55 import org.springframework.http.MediaType;
56 import org.springframework.test.context.junit.jupiter.SpringExtension;
57 import org.springframework.test.web.servlet.MockMvc;
58 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
59 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
60 import org.springframework.web.context.WebApplicationContext;
61
62
63 @ExtendWith(SpringExtension.class)
64 @WebMvcTest(value = AcElementController.class)
65 @EnableConfigurationProperties(value = AcElement.class)
66 class AcElementControllerTest {
67
68     private static final Coder CODER = new StandardCoder();
69     private static final String ELEMENT_CONFIG_YAML = "src/test/resources/config.json";
70     private static final String RETRIEVE_CONFIG = "/config";
71     private static final String ACTIVATE_CONFIG = "/activate";
72     private static final String DEACTIVATE_CONFIG = "/deactivate";
73     private static ElementConfig config;
74
75
76     @Autowired
77     private MockMvc mockMvc;
78
79     @MockBean
80     private ConfigService configService;
81
82     @Autowired
83     private WebApplicationContext context;
84
85     /**
86      * Read input element config json.
87      * @throws CoderException in case of error.
88      */
89     @BeforeAll
90     static void setupParams() throws CoderException {
91         config = CODER.decode(new File(ELEMENT_CONFIG_YAML), ElementConfig.class);
92     }
93
94     /**
95      * Mock service layer in Controller.
96      */
97     @BeforeEach
98     void mockServiceClass() {
99         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
100         when(configService.getElementConfig()).thenReturn(config);
101     }
102
103     /**
104      * Test endpoint for retrieving element config.
105      * @throws Exception in case of error.
106      */
107     @Test
108     void retrieveElementConfig() throws Exception {
109         var requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CONFIG)
110                 .accept(MediaType.APPLICATION_JSON_VALUE);
111
112         mockMvc.perform(requestBuilder).andExpect(status().isOk())
113                 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
114                 .andExpect(jsonPath("$.elementType", is("STARTER")));
115     }
116
117     /**
118      * Test endpoint for activating element config.
119      * @throws Exception in case of error.
120      */
121     @Test
122     void activateConfig() throws Exception {
123         //Mocking successful activation of element config
124         doNothing().when(configService).activateElement(config);
125
126         var requestBuilder = MockMvcRequestBuilders.post(ACTIVATE_CONFIG)
127                 .accept(MediaType.APPLICATION_JSON_VALUE)
128                 .content(getElementConfigJson())
129                 .contentType(MediaType.APPLICATION_JSON_VALUE);
130
131         mockMvc.perform(requestBuilder).andExpect(status().isCreated());
132
133         doThrow(new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "service manager already running"))
134                 .when(configService).activateElement(any());
135
136         //Activate Invalid config, expects HTTP status CONFLICT
137         requestBuilder = MockMvcRequestBuilders.post(ACTIVATE_CONFIG).accept(MediaType.APPLICATION_JSON_VALUE)
138                 .content(getInvalidJson())
139                 .contentType(MediaType.APPLICATION_JSON_VALUE);
140
141         mockMvc.perform(requestBuilder).andExpect(status().isConflict())
142                 .andExpect(result -> assertEquals("service manager already running",
143                         result.getResolvedException().getMessage()));
144     }
145
146     /**
147      * Test endpoint for deactivating element config.
148      * @throws Exception in case of error.
149      */
150     @Test
151     void deActivateConfig() throws Exception {
152
153         //Mocking successful deactivation of element config
154         doNothing().when(configService).deleteConfig();
155
156         var requestBuilder = MockMvcRequestBuilders.delete(DEACTIVATE_CONFIG);
157
158         mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
159     }
160
161     private String getInvalidJson() {
162         return new JSONObject().toString();
163     }
164
165     private String getElementConfigJson() throws IOException {
166         return FileUtils.readFileToString(new File(ELEMENT_CONFIG_YAML), StandardCharsets.UTF_8);
167     }
168
169 }