2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 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.controller.core;
40 import java.io.PrintWriter;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import javax.servlet.http.HttpSession;
46 import org.junit.Assert;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.InjectMocks;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.onap.portalsdk.core.domain.App;
53 import org.onap.portalsdk.core.domain.User;
54 import org.onap.portalsdk.core.onboarding.rest.FavoritesClient;
55 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
56 import org.onap.portalsdk.core.service.AppService;
57 import org.onap.portalsdk.core.util.SystemProperties;
58 import org.powermock.api.mockito.PowerMockito;
59 import org.powermock.core.classloader.annotations.PrepareForTest;
60 import org.powermock.modules.junit4.PowerMockRunner;
62 @RunWith(PowerMockRunner.class)
63 @PrepareForTest({SystemProperties.class, CipherUtil.class, FavoritesClient.class})
64 public class FavoritesControllerTest {
67 private FavoritesController favoritesController;
70 private AppService appService;
73 public void getFavoritesExceptionTest() {
74 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
75 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
76 HttpSession session = Mockito.mock(HttpSession.class);
77 Mockito.when(request.getSession()).thenReturn(session);
78 PowerMockito.mockStatic(SystemProperties.class);
79 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn(null);
81 favoritesController.getFavorites(request, response);
82 Assert.assertTrue(true);
86 public void getFavoritesTest() throws Exception {
87 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
88 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
89 HttpSession session = Mockito.mock(HttpSession.class);
90 Mockito.when(request.getSession()).thenReturn(session);
91 PowerMockito.mockStatic(SystemProperties.class);
92 User user = new User();
94 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user");
95 Mockito.when(session.getAttribute("user")).thenReturn(user);
99 app.setUsername("User");
100 app.setAppPassword("Password");
101 Mockito.when(appService.getDefaultApp()).thenReturn(app);
103 PowerMockito.mockStatic(CipherUtil.class);
104 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(app.getAppPassword());
105 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), app.getAppPassword())).thenReturn(app.getAppPassword());
107 PowerMockito.mockStatic(FavoritesClient.class);
108 Mockito.when(FavoritesClient.getFavorites(Mockito.anyString(), Mockito.anyString(),
109 Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn("Response");
110 PrintWriter writer = Mockito.mock(PrintWriter.class);
111 Mockito.when(response.getWriter()).thenReturn(writer);
112 favoritesController.getFavorites(request, response);
113 Assert.assertTrue(true);
117 public void getFavoritesCipherExceptionTest() throws Exception {
118 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
119 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
120 HttpSession session = Mockito.mock(HttpSession.class);
121 Mockito.when(request.getSession()).thenReturn(session);
122 PowerMockito.mockStatic(SystemProperties.class);
123 User user = new User();
125 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user");
126 Mockito.when(session.getAttribute("user")).thenReturn(user);
130 app.setUsername("User");
131 app.setAppPassword("Password");
132 Mockito.when(appService.getDefaultApp()).thenReturn(app);
134 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(app.getAppPassword());
136 PowerMockito.mockStatic(FavoritesClient.class);
137 Mockito.when(FavoritesClient.getFavorites(Mockito.anyString(), Mockito.anyString(),
138 Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn("Response");
139 PrintWriter writer = Mockito.mock(PrintWriter.class);
140 Mockito.when(response.getWriter()).thenReturn(writer);
141 favoritesController.getFavorites(request, response);
142 Assert.assertTrue(true);
146 public void getFavoritesWithAppNullTest() throws Exception {
147 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
148 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
149 HttpSession session = Mockito.mock(HttpSession.class);
150 Mockito.when(request.getSession()).thenReturn(session);
151 PowerMockito.mockStatic(SystemProperties.class);
152 User user = new User();
154 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user");
155 Mockito.when(session.getAttribute("user")).thenReturn(user);
159 app.setUsername("User");
160 app.setAppPassword("Password");
161 Mockito.when(appService.getDefaultApp()).thenReturn(null);
163 PowerMockito.mockStatic(CipherUtil.class);
164 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(app.getAppPassword());
165 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword(), app.getAppPassword())).thenReturn(app.getAppPassword());
167 PowerMockito.mockStatic(FavoritesClient.class);
168 Mockito.when(FavoritesClient.getFavorites(Mockito.anyString(), Mockito.anyString(),
169 Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn("Response");
170 PrintWriter writer = Mockito.mock(PrintWriter.class);
171 Mockito.when(response.getWriter()).thenReturn(writer);
172 favoritesController.getFavorites(request, response);
173 Assert.assertTrue(true);