2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. 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.openecomp.sdc.fe.servlets;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.apache.http.HttpStatus;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.openecomp.sdc.common.api.ConfigurationSource;
30 import org.openecomp.sdc.common.api.Constants;
31 import org.openecomp.sdc.common.impl.ExternalConfiguration;
32 import org.openecomp.sdc.common.impl.FSConfigurationSource;
33 import org.openecomp.sdc.fe.config.ConfigurationManager;
34 import org.openecomp.sdc.fe.impl.PluginStatusBL;
36 import javax.servlet.ServletContext;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpSession;
39 import javax.ws.rs.core.Response;
41 import static org.junit.Assert.assertEquals;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.Mockito.when;
44 import static org.mockito.MockitoAnnotations.openMocks;
46 public class ConfigServletTest {
48 private ConfigServlet configServlet;
51 private HttpServletRequest httpServletRequest;
53 private HttpSession httpSession;
55 private ServletContext mockedContext;
57 private PluginStatusBL pluginStatusBL;
59 private ConfigurationManager configManager;
64 String appConfigDir = "src/test/resources/config/catalog-fe";
65 ConfigurationSource configurationSource =
66 new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
67 configManager = new ConfigurationManager(configurationSource);
68 configServlet = new ConfigServlet();
72 public void validateWorkspaceConfiguration() {
76 Response response = configServlet.getUIWorkspaceConfiguration(httpServletRequest);
78 Gson gson = new GsonBuilder().setPrettyPrinting().create();
79 String expected = gson.toJson(configManager.getWorkspaceConfiguration());
80 assertEquals(expected, response.getEntity().toString());
81 assertEquals(response.getStatus(), HttpStatus.SC_OK);
84 public void validateGetPluginsConfigurationReturnsCorrectConfiguration() {
86 final String expectedEntity = "testPluginsList";
88 when(pluginStatusBL.getPluginsList()).thenReturn(expectedEntity);
90 Response response = configServlet.getPluginsConfiguration(httpServletRequest);
92 assertEquals(response.getEntity().toString(),expectedEntity);
93 assertEquals(response.getStatus(), HttpStatus.SC_OK);
96 public void validateGetPluginsConfigurationResponsesWithServerErrorIfExceptionIsThrown() {
99 when(pluginStatusBL.getPluginsList()).thenThrow(new RuntimeException());
101 Response response = configServlet.getPluginsConfiguration(httpServletRequest);
103 assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
107 public void validateGetPluginOnlineStateResponsesWithServerErrorIfExceptionIsThrown() {
109 final String testPluginName = "testPlugin";
111 when(pluginStatusBL.getPluginAvailability(any(String.class))).thenThrow(new RuntimeException());
113 Response response = configServlet.getPluginOnlineState(testPluginName, httpServletRequest);
115 assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
119 private void prepareMocks() {
120 when(httpServletRequest.getSession()).thenReturn(httpSession);
121 when(httpSession.getServletContext()).thenReturn(mockedContext);
122 when(mockedContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configManager);
123 when(mockedContext.getAttribute(Constants.PLUGIN_BL_COMPONENT)).thenReturn(pluginStatusBL);