Catalog alignment
[sdc.git] / catalog-fe / src / test / java / org / openecomp / sdc / fe / servlets / ConfigMgrServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. 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 package org.openecomp.sdc.fe.servlets;
21
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.openecomp.sdc.common.api.Constants;
30 import org.openecomp.sdc.common.rest.api.RestConfigurationInfo;
31 import org.openecomp.sdc.fe.config.Configuration;
32 import org.openecomp.sdc.fe.config.ConfigurationManager;
33
34 import javax.servlet.ServletContext;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpSession;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class ConfigMgrServletTest {
40
41     private static final String VERSION_1 = "VERSION 1";
42     private static final String PROTOCOL = "PROTO";
43     @Mock
44     private HttpServletRequest request;
45     @Mock
46     private HttpSession session;
47     @Mock
48     private ServletContext context;
49     @Mock
50     private ConfigurationManager configManager;
51
52     @Before
53     public void setUp() {
54         Mockito.when(request.getSession()).thenReturn(session);
55         Mockito.when(session.getServletContext()).thenReturn(context);
56         Mockito.when(context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configManager);
57     }
58
59     @Test
60     public void shouldGetConfigForRestType() {
61         String wantedResult = "{\n"
62             + "  \"connectionPoolSize\": 5\n"
63             + "}";
64         RestConfigurationInfo restConfiguration = new RestConfigurationInfo();
65         restConfiguration.setConnectionPoolSize(5);
66         Mockito.when(configManager.getRestClientConfiguration()).thenReturn(restConfiguration);
67
68         ConfigMgrServlet configMgrServlet = new ConfigMgrServlet();
69         String config = configMgrServlet.getConfig(request, "rest");
70         Assert.assertEquals(wantedResult, config);
71     }
72
73     @Test
74     public void shouldGetConfigForConfigurationType() {
75         checkForConfigurationTypeOrEmpty("configuration");
76     }
77
78     @Test
79     public void shouldGetConfigForNullType() {
80         checkForConfigurationTypeOrEmpty(null);
81     }
82
83     private void checkForConfigurationTypeOrEmpty(String type) {
84         String wantedResult = "{\n"
85             + "  \"beProtocol\": \"PROTO\",\n"
86             + "  \"version\": \"VERSION 1\",\n"
87             + "  \"threadpoolSize\": 0,\n"
88             + "  \"requestTimeout\": 0\n"
89             + "}";
90
91         Configuration configuration = new Configuration();
92         configuration.setVersion(VERSION_1);
93         configuration.setBeProtocol(PROTOCOL);
94         Mockito.when(configManager.getConfiguration()).thenReturn(configuration);
95
96         ConfigMgrServlet configMgrServlet = new ConfigMgrServlet();
97         String config = configMgrServlet.getConfig(request, type);
98         Assert.assertEquals(wantedResult, config);
99     }
100
101 }