Merge "Portal Spring Boot Development"
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / service / sessionmgt / SessionCommunicationTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.service.sessionmgt;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
42
43 import java.net.HttpURLConnection;
44 import java.net.URL;
45
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;
62
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest({URL.class, HttpURLConnection.class,CipherUtil.class})
65 public class SessionCommunicationTest {
66         
67         @Before
68         public void setup() {
69                 MockitoAnnotations.initMocks(this);
70         }
71         
72         @InjectMocks
73         SessionCommunication sessionCommunication = new SessionCommunication();
74         
75         @Mock
76         AppsCacheService appsCacheService = new AppsCacheServiceImple();
77         
78         @Test
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;
86                 app.isEnabled = true;
87                 app.isOpen =false;
88                 app.name = "test";
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);
100         }
101         
102         @Test
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;
111                 app.isOpen =false;
112                 app.name = "test";
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);
130         }
131         
132         @Test
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;
141                 app.isOpen =false;
142                 app.name = "test";
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");
153                 assertTrue(actual);
154         }
155         
156         
157         @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;
166                 app.isOpen =false;
167                 app.name = "test";
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");
178                 assertTrue(actual);
179         }
180         
181         @Test
182         public void clear() {
183                 
184                 sessionCommunication.clear(true);
185                 
186         }
187 }