Replace ecomp references
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / PortalAdminControllerTest.java
1
2 /*-
3  * ============LICENSE_START==========================================
4  * ONAP Portal
5  * ===================================================================
6  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7  * ===================================================================
8  *
9  * Unless otherwise specified, all software contained herein is licensed
10  * under the Apache License, Version 2.0 (the "License");
11  * you may not use this software except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *             http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * Unless otherwise specified, all documentation contained herein is licensed
23  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
24  * you may not use this documentation except in compliance with the License.
25  * You may obtain a copy of the License at
26  *
27  *             https://creativecommons.org/licenses/by/4.0/
28  *
29  * Unless required by applicable law or agreed to in writing, documentation
30  * distributed under the License is distributed on an "AS IS" BASIS,
31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32  * See the License for the specific language governing permissions and
33  * limitations under the License.
34  *
35  * ============LICENSE_END============================================
36  *
37  * 
38  */package org.onap.portalapp.portal.controller;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.onap.portalapp.portal.controller.PortalAdminController;
56 import org.onap.portalapp.portal.core.MockEPUser;
57 import org.onap.portalapp.portal.domain.EPRole;
58 import org.onap.portalapp.portal.domain.EPUser;
59 import org.onap.portalapp.portal.exceptions.NoHealthyServiceException;
60 import org.onap.portalapp.portal.framework.MockitoTestSuite;
61 import org.onap.portalapp.portal.service.AdminRolesService;
62 import org.onap.portalapp.portal.service.AdminRolesServiceImpl;
63 import org.onap.portalapp.portal.service.PortalAdminService;
64 import org.onap.portalapp.portal.service.PortalAdminServiceImpl;
65 import org.onap.portalapp.portal.transport.FieldsValidator;
66 import org.onap.portalapp.portal.transport.PortalAdmin;
67 import org.onap.portalapp.portal.utils.EcompPortalUtils;
68 import org.onap.portalapp.util.EPUserUtils;
69 import org.onap.portalsdk.core.service.AuditService;
70 import org.onap.portalsdk.core.service.AuditServiceImpl;
71
72 public class PortalAdminControllerTest extends MockitoTestSuite{
73
74         @InjectMocks
75         PortalAdminController portalAdminController = new PortalAdminController();
76
77         @Mock
78         AdminRolesService adminRolesService = new AdminRolesServiceImpl();
79         
80         @Mock
81         PortalAdminService portalAdminService = new PortalAdminServiceImpl();
82
83         @Mock
84         AuditService auditService = new AuditServiceImpl();
85
86          
87         @Mock
88         EcompPortalUtils ecompPortalUtils = new EcompPortalUtils();
89
90         @Before
91         public void setup() {
92                 MockitoAnnotations.initMocks(this);
93         }
94
95         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
96
97         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
98         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
99         NullPointerException nullPointerException = new NullPointerException();
100
101         @Mock
102         EPUserUtils ePUserUtils = new EPUserUtils();
103
104         MockEPUser mockUser = new MockEPUser();
105         
106         
107         @Test
108         public void getPortalAdminsTest()
109         {
110                 EPUser user = mockUser.mockEPUser();
111                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
112                 List<PortalAdmin> expectedPortalAdminsList = new ArrayList<PortalAdmin>();
113                 PortalAdmin portalAdmin= new PortalAdmin();
114                 
115                 portalAdmin.setUserId((long) 1);
116                 portalAdmin.setLoginId("guestT");
117                 portalAdmin.setFirstName("Test_FirstName");
118                 portalAdmin.setLastName("Test_LastName");
119                 
120                 expectedPortalAdminsList.add(portalAdmin);
121                 
122                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true);
123                 
124          Mockito.when(portalAdminService.getPortalAdmins()).thenReturn(expectedPortalAdminsList);
125          List<PortalAdmin> actualPortalAdminsList =  portalAdminController.getPortalAdmins(mockedRequest, mockedResponse);
126          assertEquals(actualPortalAdminsList,expectedPortalAdminsList);
127
128         }
129 //      @Test
130 //      public void getPortalAdminsIfUserIsNullTest()
131 //      {
132 //              EPUser user = null;
133 //              Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
134 //
135 //         assertNull(portalAdminController.getPortalAdmins(mockedRequest, mockedResponse));
136 //
137 //      }
138         
139         @Test
140         public void getPortalAdminsIfUserIsSuperAdminTest()
141         {
142                 EPUser user = mockUser.mockEPUser();
143                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
144                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
145                  assertNull(portalAdminController.getPortalAdmins(mockedRequest, mockedResponse));
146
147         }
148         
149         
150         
151         @Test
152         public void createPortalAdminTest()
153         {
154                 EPUser user = mockUser.mockEPUser();
155                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
156         
157                 FieldsValidator expectedFieldValidator = new FieldsValidator();
158                 expectedFieldValidator.setHttpStatusCode((long) 200);
159                 expectedFieldValidator.setFields(null);
160                 expectedFieldValidator.setErrorCode(null);
161                 FieldsValidator actualFieldValidator = new FieldsValidator();
162                 String sbcid = "Test";
163                 
164                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(true);
165                 Mockito.when(portalAdminService.createPortalAdmin(sbcid)).thenReturn(expectedFieldValidator);
166                 actualFieldValidator = portalAdminController.createPortalAdmin(mockedRequest, sbcid, mockedResponse);
167         assertEquals(actualFieldValidator,expectedFieldValidator);
168
169         }
170                 
171 //      @Test
172 //      public void createPortalAdminIfUserIsNullTest()
173 //      {
174 //              //EPUser user = null;
175 //              Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(null);
176 //              String sbcid = "null";
177 //              assertNull(portalAdminController.createPortalAdmin(mockedRequest, sbcid, mockedResponse));
178 //
179 //      }
180         
181         @Test
182         public void createPortalAdminIfUserIsSuperAdminTest()
183         {
184                 EPUser user = mockUser.mockEPUser();
185                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
186                 Mockito.when(adminRolesService.isSuperAdmin(user)).thenReturn(false);
187                 String sbcid = "Test";
188                 assertNull(portalAdminController.createPortalAdmin(mockedRequest, sbcid, mockedResponse));
189
190         }
191                         
192 }