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.junit.Assert.assertTrue;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.ArgumentMatchers.eq;
45 import static org.mockito.Mockito.when;
46 import static org.mockito.MockitoAnnotations.initMocks;
48 public class ConfigServletTest {
50 private ConfigServlet configServlet;
53 private HttpServletRequest httpServletRequest;
55 private HttpSession httpSession;
57 private ServletContext mockedContext;
59 private PluginStatusBL pluginStatusBL;
61 private ConfigurationManager configManager;
66 String appConfigDir = "src/test/resources/config/catalog-fe";
67 ConfigurationSource configurationSource =
68 new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
69 configManager = new ConfigurationManager(configurationSource);
70 configServlet = new ConfigServlet();
74 public void validateWorkspaceConfiguration() {
78 Response response = configServlet.getUIWorkspaceConfiguration(httpServletRequest);
80 Gson gson = new GsonBuilder().setPrettyPrinting().create();
81 String expected = gson.toJson(configManager.getWorkspaceConfiguration());
82 assertEquals(expected, response.getEntity().toString());
83 assertEquals(response.getStatus(), HttpStatus.SC_OK);
86 public void validateGetPluginsConfigurationReturnsCorrectConfiguration() {
88 final String expectedEntity = "testPluginsList";
90 when(pluginStatusBL.getPluginsList()).thenReturn(expectedEntity);
92 Response response = configServlet.getPluginsConfiguration(httpServletRequest);
94 assertEquals(response.getEntity().toString(),expectedEntity);
95 assertEquals(response.getStatus(), HttpStatus.SC_OK);
98 public void validateGetPluginsConfigurationResponsesWithServerErrorIfExceptionIsThrown() {
101 when(pluginStatusBL.getPluginsList()).thenThrow(new RuntimeException());
103 Response response = configServlet.getPluginsConfiguration(httpServletRequest);
105 assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
108 public void validateGetPluginOnlineStateReturnsCorrectState() {
110 final String testPluginName = "testPlugin";
111 final String pluginAvailability = "forTesting";
113 when(pluginStatusBL.getPluginAvailability(eq(testPluginName))).thenReturn(pluginAvailability);
115 Response response = configServlet.getPluginOnlineState(testPluginName,httpServletRequest);
117 assertEquals(response.getEntity().toString(),pluginAvailability);
118 assertEquals(response.getStatus(), HttpStatus.SC_OK);
121 public void validateGetPluginOnlineStateResponsesWithServerErrorIfExceptionIsThrown() {
123 final String testPluginName = "testPlugin";
125 when(pluginStatusBL.getPluginAvailability(any(String.class))).thenThrow(new RuntimeException());
127 Response response = configServlet.getPluginOnlineState(testPluginName, httpServletRequest);
129 assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
132 public void validateGetPluginOnlineStateResponsesWithNotFoundIfThereIsNoPlugin() {
134 final String testPluginName = "testPlugin";
136 when(pluginStatusBL.getPluginAvailability(any(String.class))).thenReturn(null);
138 Response response = configServlet.getPluginOnlineState(testPluginName, httpServletRequest);
140 assertEquals(response.getStatus(), HttpStatus.SC_NOT_FOUND);
141 assertTrue(response.getEntity().toString().contains(testPluginName));
144 private void prepareMocks() {
145 when(httpServletRequest.getSession()).thenReturn(httpSession);
146 when(httpSession.getServletContext()).thenReturn(mockedContext);
147 when(mockedContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configManager);
148 when(mockedContext.getAttribute(Constants.PLUGIN_BL_COMPONENT)).thenReturn(pluginStatusBL);