d1d3f74108ccd6ab72fed830350cc8030259e09f
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller.core;
39
40
41 import java.io.IOException;
42 import java.io.PrintWriter;
43 import java.io.StringWriter;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Matchers;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.framework.MockitoTestSuite;
57 import org.onap.portalsdk.core.domain.App;
58 import org.onap.portalsdk.core.domain.User;
59 import org.onap.portalsdk.core.onboarding.ueb.UebException;
60 import org.onap.portalsdk.core.onboarding.ueb.UebManager;
61 import org.onap.portalsdk.core.onboarding.ueb.UebMsg;
62 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
63 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
64 import org.onap.portalsdk.core.service.AppService;
65 import org.onap.portalsdk.core.web.support.UserUtils;
66 import org.powermock.api.mockito.PowerMockito;
67 import org.powermock.core.classloader.annotations.PrepareForTest;
68 import org.powermock.modules.junit4.PowerMockRunner;
69
70 import com.fasterxml.jackson.databind.ObjectMapper;
71
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({UebManager.class, PortalApiProperties.class, PortalApiConstants.class})
74 public class FuncMenuControllerTest {
75
76         @InjectMocks
77         FuncMenuController funcMenuController = new FuncMenuController();
78         
79         @Mock
80         AppService appService;
81         
82         @Mock
83         UebManager uebManager;
84
85         @Before
86         public void setup() {
87                 MockitoAnnotations.initMocks(this);
88         }
89         
90         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
91         
92         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
93         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
94         
95         NullPointerException nullPointerException = new NullPointerException();
96         
97         User user = new User();
98         
99         @Mock
100         UserUtils userUtils = new UserUtils();
101         
102         @Mock
103         ObjectMapper mapper = new ObjectMapper();
104         
105         @Test
106         public void functionalMenuUserExistsTest() throws IOException, UebException{
107                 User user = new User();
108                 user.setOrgUserId("test12");
109                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
110                 StringWriter sw = new StringWriter();
111                 PrintWriter writer = new PrintWriter(sw);
112                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
113                 PowerMockito.mockStatic(UebManager.class);
114                 
115             Mockito.when(UebManager.getInstance()).thenReturn(uebManager);
116             Mockito.when(uebManager.requestReply(Matchers.anyObject())).thenReturn(new UebMsg());
117                 funcMenuController.functionalMenu(mockedRequest, mockedResponse);
118         }
119         
120         @Test
121         public void functionalMenuUserNotExistsTest() throws IOException, UebException{
122                 StringWriter sw = new StringWriter();
123                 PrintWriter writer = new PrintWriter(sw);
124                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
125                 
126                 funcMenuController.functionalMenu(mockedRequest, mockedResponse);
127         }
128         
129         @Test
130         public void functionalMenuViaRestTest() throws IOException, UebException{
131                 User user = new User();
132                 user.setOrgUserId("test12");
133                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
134                 StringWriter sw = new StringWriter();
135                 PrintWriter writer = new PrintWriter(sw);
136                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
137                 PowerMockito.mockStatic(UebManager.class);
138                 PowerMockito.mockStatic(PortalApiProperties.class);
139                 PowerMockito.mockStatic(PortalApiConstants.class);
140                 Mockito.when(PortalApiProperties
141                                                 .getProperty(PortalApiConstants.USE_REST_FOR_FUNCTIONAL_MENU)).thenReturn("test");
142                 Mockito.when(UebManager.getInstance()).thenReturn(uebManager);
143                 funcMenuController.functionalMenu(mockedRequest, mockedResponse);
144         }
145         
146         @Test
147         public void functionalMenuViaRestAppNullTest() throws IOException, UebException{
148                 App app = new App();
149                 User user = new User();
150                 user.setOrgUserId("test12");
151                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
152                 StringWriter sw = new StringWriter();
153                 PrintWriter writer = new PrintWriter(sw);
154                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);
155                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
156                 PowerMockito.mockStatic(UebManager.class);
157                 PowerMockito.mockStatic(PortalApiProperties.class);
158                 PowerMockito.mockStatic(PortalApiConstants.class);
159                 Mockito.when(PortalApiProperties
160                                                 .getProperty(PortalApiConstants.USE_REST_FOR_FUNCTIONAL_MENU)).thenReturn("test");
161                 Mockito.when(UebManager.getInstance()).thenReturn(uebManager);
162                 funcMenuController.functionalMenu(mockedRequest, mockedResponse);
163         }
164         
165         @Test
166         public void functionalMenuExceptionTest() throws IOException{
167                 User user = new User();
168                 user.setOrgUserId("test12");
169                 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
170                 StringWriter sw = new StringWriter();
171                 PrintWriter writer = new PrintWriter(sw);
172                 Mockito.when(mockedResponse.getWriter()).thenReturn(writer);            
173                 funcMenuController.functionalMenu(mockedRequest, mockedResponse);
174         }
175         
176 }