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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.element.rest;
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;
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;
63 @ExtendWith(SpringExtension.class)
64 @WebMvcTest(value = AcElementController.class)
65 @EnableConfigurationProperties(value = AcElement.class)
66 class AcElementControllerTest {
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;
77 private MockMvc mockMvc;
80 private ConfigService configService;
83 private WebApplicationContext context;
86 * Read input element config json.
87 * @throws CoderException in case of error.
90 static void setupParams() throws CoderException {
91 config = CODER.decode(new File(ELEMENT_CONFIG_YAML), ElementConfig.class);
95 * Mock service layer in Controller.
98 void mockServiceClass() {
99 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
100 when(configService.getElementConfig()).thenReturn(config);
104 * Test endpoint for retrieving element config.
105 * @throws Exception in case of error.
108 void retrieveElementConfig() throws Exception {
109 var requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CONFIG)
110 .accept(MediaType.APPLICATION_JSON_VALUE);
112 mockMvc.perform(requestBuilder).andExpect(status().isOk())
113 .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
114 .andExpect(jsonPath("$.elementType", is("STARTER")));
118 * Test endpoint for activating element config.
119 * @throws Exception in case of error.
122 void activateConfig() throws Exception {
123 //Mocking successful activation of element config
124 doNothing().when(configService).activateElement(config);
126 var requestBuilder = MockMvcRequestBuilders.post(ACTIVATE_CONFIG)
127 .accept(MediaType.APPLICATION_JSON_VALUE)
128 .content(getElementConfigJson())
129 .contentType(MediaType.APPLICATION_JSON_VALUE);
131 mockMvc.perform(requestBuilder).andExpect(status().isCreated());
133 doThrow(new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "service manager already running"))
134 .when(configService).activateElement(any());
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);
141 mockMvc.perform(requestBuilder).andExpect(status().isConflict())
142 .andExpect(result -> assertEquals("service manager already running",
143 result.getResolvedException().getMessage()));
147 * Test endpoint for deactivating element config.
148 * @throws Exception in case of error.
151 void deActivateConfig() throws Exception {
153 //Mocking successful deactivation of element config
154 doNothing().when(configService).deleteConfig();
156 var requestBuilder = MockMvcRequestBuilders.delete(DEACTIVATE_CONFIG);
158 mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
161 private String getInvalidJson() {
162 return new JSONObject().toString();
165 private String getElementConfigJson() throws IOException {
166 return FileUtils.readFileToString(new File(ELEMENT_CONFIG_YAML), StandardCharsets.UTF_8);