5c54d40d16aef14911ecd19bf6a5220d66f324dc
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 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.controller.core;
39
40 import java.io.PrintWriter;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import javax.servlet.http.HttpSession;
45
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;
61
62 @RunWith(PowerMockRunner.class)
63 @PrepareForTest({SystemProperties.class, CipherUtil.class, FavoritesClient.class})
64 public class FavoritesControllerTest {
65
66         @InjectMocks
67         private FavoritesController favoritesController;
68         
69         @Mock
70         private AppService appService;
71         
72         @Test
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);
80                 
81                 favoritesController.getFavorites(request, response);
82                 Assert.assertTrue(true);
83         }
84         
85         @Test
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();
93                 user.setId(123L);
94                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user");
95                 Mockito.when(session.getAttribute("user")).thenReturn(user);
96                 
97                 App app = new App();
98                 app.setName("App");
99                 app.setUsername("User");
100                 app.setAppPassword("Password");
101                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
102                 
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());
106                 
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);
114         }
115         
116         @Test
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();
124                 user.setId(123L);
125                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user");
126                 Mockito.when(session.getAttribute("user")).thenReturn(user);
127                 
128                 App app = new App();
129                 app.setName("App");
130                 app.setUsername("User");
131                 app.setAppPassword("Password");
132                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
133                 
134                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(app.getAppPassword());
135                 
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);
143         }
144         
145         @Test
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();
153                 user.setId(123L);
154                 Mockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user");
155                 Mockito.when(session.getAttribute("user")).thenReturn(user);
156                 
157                 App app = new App();
158                 app.setName("App");
159                 app.setUsername("User");
160                 app.setAppPassword("Password");
161                 Mockito.when(appService.getDefaultApp()).thenReturn(null);
162                 
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());
166                 
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);
174         }
175 }