611f41f1e3e9f983b4d9b43e5715fa7937b22a4d
[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.portalsdk.rnotebookintegration.controller;
39
40 import static org.junit.Assert.assertEquals;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.hibernate.SessionFactory;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.mockito.MockitoAnnotations;
54 import org.onap.portalapp.framework.MockitoTestSuite;
55 import org.onap.portalsdk.core.domain.User;
56 import org.onap.portalsdk.core.restful.domain.EcompUser;
57 import org.onap.portalsdk.core.service.DataAccessService;
58 import org.onap.portalsdk.core.service.DataAccessServiceImpl;
59 import org.onap.portalsdk.core.util.SystemProperties;
60 import org.onap.portalsdk.core.web.support.UserUtils;
61 import org.onap.portalsdk.rnotebookintegration.exception.RNotebookIntegrationException;
62 import org.onap.portalsdk.rnotebookintegration.service.RNoteBookIntegrationService;
63 import org.powermock.api.mockito.PowerMockito;
64 import org.powermock.core.classloader.annotations.PrepareForTest;
65 import org.powermock.modules.junit4.PowerMockRunner;
66 import org.springframework.mock.web.MockHttpServletResponse;
67
68 @RunWith(PowerMockRunner.class)
69 @PrepareForTest({UserUtils.class, SystemProperties.class})
70 public class RNoteBookFEControllerTest {
71
72         @InjectMocks
73         RNoteBookFEController rNoteBookFEController = new RNoteBookFEController();
74
75         @Mock
76         private RNoteBookIntegrationService rNoteBookIntegrationService;
77
78         @Mock
79         private SessionFactory sessionFactory;
80         @InjectMocks
81         private DataAccessServiceImpl dataAccessServiceImpl;
82         
83         @Mock
84         private DataAccessService dataAccessService;
85
86         @Before
87         public void setup() {
88                 MockitoAnnotations.initMocks(this);
89         }
90
91         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
92
93         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
94         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
95
96         NullPointerException nullPointerException = new NullPointerException();
97
98         User user = new User();
99
100         @Mock
101         UserUtils userUtils = new UserUtils();
102
103         @Test
104         public void getrNoteBookIntegrationServiceTest() {
105                 RNoteBookIntegrationService actualRNoteBookIntegrationService = rNoteBookIntegrationService;
106                 RNoteBookIntegrationService expectedRNoteBookIntegrationService = rNoteBookFEController
107                                 .getrNoteBookIntegrationService();
108                 rNoteBookFEController.setrNoteBookIntegrationService(null);
109                 assertEquals(actualRNoteBookIntegrationService, expectedRNoteBookIntegrationService);
110         }
111
112         @Test
113         public void saveRNotebookCredentialsExceptionTest() {
114
115                 String notebookId = "123";
116                 HttpServletRequest request = PowerMockito.mock(HttpServletRequest.class);
117                 String value = "  { \"key\" : \"value\" }" ;
118                 
119                 Mockito.when(request.getParameter("qparams")).thenReturn(value);
120                 
121                 HttpServletResponse response = new MockHttpServletResponse();
122
123                 PowerMockito.mockStatic(UserUtils.class);
124                 User user = new User();
125                 Mockito.when(UserUtils.getUserSession(request)).thenReturn(user);
126                 Mockito.when(dataAccessService.getDomainObject(User.class, user.getId(), null)).thenReturn(user);
127                 EcompUser ecUser = new EcompUser();
128                 Mockito.when(UserUtils.convertToEcompUser(user)).thenReturn(ecUser);
129                 
130                 PowerMockito.mockStatic(SystemProperties.class);
131                 Mockito.when(SystemProperties.containsProperty(Mockito.anyString())).thenReturn(false);
132
133                 rNoteBookFEController.saveRNotebookCredentials(notebookId, request, response);
134                 Assert.assertTrue(true);
135         }
136         
137         @Test
138         public void saveRNotebookCredentialsTest() {
139
140                 String notebookId = "123";
141                 HttpServletRequest request = PowerMockito.mock(HttpServletRequest.class);
142                 String value = "  { \"key\" : \"value\" }" ;
143                 
144                 Mockito.when(request.getParameter("qparams")).thenReturn(value);
145                 
146                 HttpServletResponse response = new MockHttpServletResponse();
147
148                 PowerMockito.mockStatic(UserUtils.class);
149                 User user = new User();
150                 Mockito.when(UserUtils.getUserSession(request)).thenReturn(user);
151                 Mockito.when(dataAccessService.getDomainObject(User.class, user.getId(), null)).thenReturn(user);
152                 EcompUser ecUser = new EcompUser();
153                 Mockito.when(UserUtils.convertToEcompUser(user)).thenReturn(ecUser);
154                 
155                 PowerMockito.mockStatic(SystemProperties.class);
156                 Mockito.when(SystemProperties.containsProperty(Mockito.anyString())).thenReturn(true);
157
158                 rNoteBookFEController.saveRNotebookCredentials(notebookId, request, response);
159                 Assert.assertTrue(true);
160         }
161         
162         @Test
163         public void saveRNotebookCredentialsCustExceptionTest() {
164
165                 String notebookId = "123";
166                 HttpServletRequest request = PowerMockito.mock(HttpServletRequest.class);
167                 String value = "  { \"key\" : \"value\" }" ;
168                 
169                 Mockito.when(request.getParameter("qparams")).thenReturn(value);
170                 
171                 HttpServletResponse response = new MockHttpServletResponse();
172
173                 PowerMockito.mockStatic(UserUtils.class);
174                 User user = new User();
175                 Mockito.when(UserUtils.getUserSession(request)).thenThrow(RNotebookIntegrationException.class);
176                 Mockito.when(dataAccessService.getDomainObject(User.class, user.getId(), null)).thenReturn(user);
177                 EcompUser ecUser = new EcompUser();
178                 Mockito.when(UserUtils.convertToEcompUser(user)).thenReturn(ecUser);
179                 
180                 PowerMockito.mockStatic(SystemProperties.class);
181                 Mockito.when(SystemProperties.containsProperty(Mockito.anyString())).thenReturn(true);
182
183                 rNoteBookFEController.saveRNotebookCredentials(notebookId, request, response);
184                 Assert.assertTrue(true);
185         }
186
187 }