2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.service.sessionmgt;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
43 import java.net.HttpURLConnection;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.InjectMocks;
50 import org.mockito.Matchers;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.mockito.MockitoAnnotations;
54 import org.onap.portalapp.portal.domain.EPApp;
55 import org.onap.portalapp.portal.service.AppsCacheService;
56 import org.onap.portalapp.portal.service.AppsCacheServiceImple;
57 import org.onap.portalapp.portal.transport.OnboardingApp;
58 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
59 import org.powermock.api.mockito.PowerMockito;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest({URL.class, HttpURLConnection.class,CipherUtil.class})
65 public class SessionCommunicationTest {
69 MockitoAnnotations.initMocks(this);
73 SessionCommunication sessionCommunication = new SessionCommunication();
76 AppsCacheService appsCacheService = new AppsCacheServiceImple();
79 public void sendGetConnectionRefusedTest() throws Exception {
80 OnboardingApp app = new OnboardingApp();
81 app.setRestrictedApp(false);
82 app.setUebKey("test");
83 app.setUebSecret("test");
84 app.setUebTopicName("test");
85 app.isCentralAuth = true;
89 app.restUrl ="http://localhost:1234";
90 app.username = "test";
91 app.appPassword = "xyz";
92 URL u = PowerMockito.mock(URL.class);
93 HttpURLConnection huc = PowerMockito.mock(HttpURLConnection.class);
94 String url = "http://localhost:1234/sessionTimeOuts";
95 PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(u);
96 PowerMockito.whenNew(HttpURLConnection.class).withAnyArguments().thenReturn(huc);
97 PowerMockito.when(huc.getResponseCode()).thenReturn(200);
98 String actual = sessionCommunication.sendGet(app);
99 assertEquals("", actual);
103 public void sendGetConnectionRefusedTest1() throws Exception {
104 OnboardingApp app = new OnboardingApp();
105 app.setRestrictedApp(false);
106 app.setUebKey("test");
107 app.setUebSecret("test");
108 app.setUebTopicName("test");
109 app.isCentralAuth = true;
110 app.isEnabled = true;
113 app.restUrl ="http://localhost:1234";
114 app.username = "test";
115 app.appPassword = "";
116 EPApp epApp = new EPApp();
117 epApp.setUsername("test");
118 epApp.setAppPassword("xyz1234");
119 PowerMockito.mockStatic(CipherUtil.class);
120 PowerMockito.when(CipherUtil.decryptPKC(Matchers.anyString(),Matchers.anyString())).thenReturn("test");
121 Mockito.when(appsCacheService.getApp(1L)).thenReturn(epApp);
122 URL u = PowerMockito.mock(URL.class);
123 HttpURLConnection huc = PowerMockito.mock(HttpURLConnection.class);
124 String url = "http://localhost:1234/sessionTimeOuts";
125 PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(u);
126 PowerMockito.whenNew(HttpURLConnection.class).withAnyArguments().thenReturn(huc);
127 PowerMockito.when(huc.getResponseCode()).thenReturn(200);
128 String actual = sessionCommunication.sendGet(app);
129 assertEquals("", actual);
133 public void pingSessionConnectionRefusedTest() throws Exception {
134 OnboardingApp app = new OnboardingApp();
135 app.setRestrictedApp(false);
136 app.setUebKey("test");
137 app.setUebSecret("test");
138 app.setUebTopicName("test");
139 app.isCentralAuth = true;
140 app.isEnabled = true;
143 app.restUrl ="http://localhost:1234";
144 app.username = "test";
145 app.appPassword = "xyz";
146 URL u = PowerMockito.mock(URL.class);
147 HttpURLConnection huc = PowerMockito.mock(HttpURLConnection.class);
148 String url = "http://localhost:1234/sessionTimeOuts";
149 PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(u);
150 PowerMockito.whenNew(HttpURLConnection.class).withAnyArguments().thenReturn(huc);
151 PowerMockito.when(huc.getResponseCode()).thenReturn(200);
152 Boolean actual = sessionCommunication.pingSession(app, "test");
158 public void timeoutSessionConnectionRefusedTest() throws Exception {
159 OnboardingApp app = new OnboardingApp();
160 app.setRestrictedApp(false);
161 app.setUebKey("test");
162 app.setUebSecret("test");
163 app.setUebTopicName("test");
164 app.isCentralAuth = true;
165 app.isEnabled = true;
168 app.restUrl ="http://localhost:1234";
169 app.username = "test";
170 app.appPassword = "xyz";
171 URL u = PowerMockito.mock(URL.class);
172 HttpURLConnection huc = PowerMockito.mock(HttpURLConnection.class);
173 String url = "http://localhost:1234/sessionTimeOuts";
174 PowerMockito.whenNew(URL.class).withArguments(url).thenReturn(u);
175 PowerMockito.whenNew(HttpURLConnection.class).withAnyArguments().thenReturn(huc);
176 PowerMockito.when(huc.getResponseCode()).thenReturn(200);
177 Boolean actual = sessionCommunication.timeoutSession(app, "test");
182 public void clear() {
184 sessionCommunication.clear(true);