Catalog alignment
[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.mockito.ArgumentMatchers.any;
43 import static org.mockito.Mockito.when;
44 import static org.mockito.MockitoAnnotations.initMocks;
45
46 public class ConfigServletTest {
47
48     private ConfigServlet configServlet;
49
50     @Mock
51     private HttpServletRequest httpServletRequest;
52     @Mock
53     private HttpSession httpSession;
54     @Mock
55     private ServletContext mockedContext;
56     @Mock
57     private PluginStatusBL pluginStatusBL;
58     @Mock
59     private ConfigurationManager configManager;
60
61     @Before
62     public void setUp() {
63         initMocks(this);
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();
69     }
70
71     @Test
72     public void validateWorkspaceConfiguration() {
73
74         prepareMocks();
75
76         Response response = configServlet.getUIWorkspaceConfiguration(httpServletRequest);
77
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);
82     }
83     @Test
84     public void validateGetPluginsConfigurationReturnsCorrectConfiguration() {
85
86         final String expectedEntity = "testPluginsList";
87         prepareMocks();
88         when(pluginStatusBL.getPluginsList()).thenReturn(expectedEntity);
89
90         Response response = configServlet.getPluginsConfiguration(httpServletRequest);
91
92         assertEquals(response.getEntity().toString(),expectedEntity);
93         assertEquals(response.getStatus(), HttpStatus.SC_OK);
94     }
95     @Test
96     public void validateGetPluginsConfigurationResponsesWithServerErrorIfExceptionIsThrown() {
97
98         prepareMocks();
99         when(pluginStatusBL.getPluginsList()).thenThrow(new RuntimeException());
100
101         Response response = configServlet.getPluginsConfiguration(httpServletRequest);
102
103         assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
104     }
105
106     @Test
107     public void validateGetPluginOnlineStateResponsesWithServerErrorIfExceptionIsThrown() {
108
109         final String testPluginName = "testPlugin";
110         prepareMocks();
111         when(pluginStatusBL.getPluginAvailability(any(String.class))).thenThrow(new RuntimeException());
112
113         Response response = configServlet.getPluginOnlineState(testPluginName, httpServletRequest);
114
115         assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
116     }
117
118
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);
124     }
125
126 }