Add API to retrieve UI configuration
[sdc.git] / catalog-fe / src / test / java / org / openecomp / sdc / fe / servlets / ConfigServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.fe.servlets;
22
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;
35
36 import javax.servlet.ServletContext;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpSession;
39 import javax.ws.rs.core.Response;
40
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;
47
48 public class ConfigServletTest {
49
50     private ConfigServlet configServlet;
51
52     @Mock
53     private HttpServletRequest httpServletRequest;
54     @Mock
55     private HttpSession httpSession;
56     @Mock
57     private ServletContext mockedContext;
58     @Mock
59     private PluginStatusBL pluginStatusBL;
60     @Mock
61     private ConfigurationManager configManager;
62
63     @Before
64     public void setUp() {
65         initMocks(this);
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();
71     }
72
73     @Test
74     public void validateWorkspaceConfiguration() {
75
76         prepareMocks();
77
78         Response response = configServlet.getUIWorkspaceConfiguration(httpServletRequest);
79
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);
84     }
85     @Test
86     public void validateGetPluginsConfigurationReturnsCorrectConfiguration() {
87
88         final String expectedEntity = "testPluginsList";
89         prepareMocks();
90         when(pluginStatusBL.getPluginsList()).thenReturn(expectedEntity);
91
92         Response response = configServlet.getPluginsConfiguration(httpServletRequest);
93
94         assertEquals(response.getEntity().toString(),expectedEntity);
95         assertEquals(response.getStatus(), HttpStatus.SC_OK);
96     }
97     @Test
98     public void validateGetPluginsConfigurationResponsesWithServerErrorIfExceptionIsThrown() {
99
100         prepareMocks();
101         when(pluginStatusBL.getPluginsList()).thenThrow(new RuntimeException());
102
103         Response response = configServlet.getPluginsConfiguration(httpServletRequest);
104
105         assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
106     }
107     @Test
108     public void validateGetPluginOnlineStateReturnsCorrectState() {
109
110         final String testPluginName = "testPlugin";
111         final String pluginAvailability = "forTesting";
112         prepareMocks();
113         when(pluginStatusBL.getPluginAvailability(eq(testPluginName))).thenReturn(pluginAvailability);
114
115         Response response = configServlet.getPluginOnlineState(testPluginName,httpServletRequest);
116
117         assertEquals(response.getEntity().toString(),pluginAvailability);
118         assertEquals(response.getStatus(), HttpStatus.SC_OK);
119     }
120     @Test
121     public void validateGetPluginOnlineStateResponsesWithServerErrorIfExceptionIsThrown() {
122
123         final String testPluginName = "testPlugin";
124         prepareMocks();
125         when(pluginStatusBL.getPluginAvailability(any(String.class))).thenThrow(new RuntimeException());
126
127         Response response = configServlet.getPluginOnlineState(testPluginName, httpServletRequest);
128
129         assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
130     }
131     @Test
132     public void validateGetPluginOnlineStateResponsesWithNotFoundIfThereIsNoPlugin() {
133
134         final String testPluginName = "testPlugin";
135         prepareMocks();
136         when(pluginStatusBL.getPluginAvailability(any(String.class))).thenReturn(null);
137
138         Response response = configServlet.getPluginOnlineState(testPluginName, httpServletRequest);
139
140         assertEquals(response.getStatus(), HttpStatus.SC_NOT_FOUND);
141         assertTrue(response.getEntity().toString().contains(testPluginName));
142     }
143
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);
149     }
150
151 }