338b31c832060d6385246c9bb74ea97187748657
[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.core.service;
39
40 import java.io.IOException;
41 import java.util.List;
42
43 import org.junit.Assert;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.InjectMocks;
47 import org.mockito.Matchers;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.onap.portalsdk.core.domain.Role;
51 import org.onap.portalsdk.core.domain.RoleFunction;
52 import org.powermock.modules.junit4.PowerMockRunner;
53
54 @RunWith(PowerMockRunner.class)
55 public class RoleServiceCentralizedAccessTest {
56
57         @InjectMocks
58         private RoleServiceCentralizedAccess roleServiceCntrlAccess;
59         
60         @Mock
61         private RestApiRequestBuilder restApiRequestBuilder;
62
63         @Test
64         public void getRoleFunctionsTest() throws Exception {
65                 String loginId ="1234";
66                 String response ="[    {        \"code\" : \"abc\",        \"name\" : \"xyz\"    },    {        \"code\" : \"pqr\",        \"name\" : \"str\"    } ]";
67                 Mockito.when(restApiRequestBuilder.getViaREST(Matchers.anyString(), Matchers.anyBoolean(), Matchers.anyString())).thenReturn(response);
68                 List<RoleFunction> roleFunctions = roleServiceCntrlAccess.getRoleFunctions(loginId);
69                 Assert.assertTrue(roleFunctions.size() > 0);
70         }
71         
72         @Test
73         public void getAvailableChildRolesWithEmptyRoleIdTest() throws Exception {
74                 String loginId = "123";
75                 Long roleId = null;
76                 String response ="[    {        \"active\" : true,        \"name\" : \"xyz\"    } ]";
77                 Mockito.when(restApiRequestBuilder.getViaREST("/v1/roles", true, loginId)).thenReturn(response);
78                 List<Role> roles = roleServiceCntrlAccess.getAvailableChildRoles(loginId, roleId);
79                 Assert.assertNotNull(roles);
80         }
81         
82         @Test
83         public void getAvailableChildRolesWithZeroRoleIdTest() throws Exception {
84                 String loginId = "123";
85                 Long roleId = 0L;
86                 String response ="[    {        \"active\" : true,        \"name\" : \"xyz\"    } ]";
87                 Mockito.when(restApiRequestBuilder.getViaREST("/v1/roles", true, loginId)).thenReturn(response);
88                 List<Role> roles = roleServiceCntrlAccess.getAvailableChildRoles(loginId, roleId);
89                 Assert.assertNotNull(roles);
90         }
91         
92         @Test
93         public void getAvailableChildRolesTest() throws Exception {
94                 String loginId = "123";
95                 Long roleId = 123L;
96                 String response ="[    {        \"active\" : false,        \"name\" : \"xyz\"    } ]";
97                 Mockito.when(restApiRequestBuilder.getViaREST("/v1/roles", true, loginId)).thenReturn(response);
98                 String roleResponse =" {        \"active\" : true,        \"name\" : \"xyz\", \"roleFunctions\" : [    {        \"code\" : \"abc\",        \"name\" : \"RF1\" , \"type\" : \"abc\", \"action\" : \"abc\"   },    {        \"code\" : \"pqr\",        \"name\" : \"RF2\"  , \"type\" : \"abc\", \"action\" : \"abc\"  } ]    ,  \"parentRoles\": [   {\"active\" : false,        \"name\" : \"XYZ-ABC\"}, {\"active\" : true,        \"name\" : \"ABC\"}  ]    } ";
99                 Mockito.when(restApiRequestBuilder.getViaREST("/v1/role/" + roleId, true, loginId)).thenReturn(roleResponse);
100                 roleServiceCntrlAccess.getAvailableChildRoles(loginId, roleId);
101                 Assert.assertTrue(true);
102         }
103         
104         @Test
105         public void saveRoleTest() throws Exception {
106                 Role role = new Role();
107                 role.setName("Role");
108                 roleServiceCntrlAccess.saveRole("123", role);
109                 Assert.assertTrue(true);
110         }
111         
112         @Test
113         public void deleteRoleTest() throws Exception {
114                 Role role = new Role();
115                 role.setName("Role");
116                 role.setId(123l);
117                 roleServiceCntrlAccess.deleteRole("123", role);
118                 Assert.assertTrue(true);
119         }
120         
121         @Test
122         public void getActiveRolesTest() throws Exception {
123                 String requestedLoginId ="1234";
124                 String response ="[    {        \"active\" : true,        \"name\" : \"role1\"    }, {        \"active\" : false,        \"name\" : \"role2\"    } ]";
125                 Mockito.when(restApiRequestBuilder.getViaREST("/v1/activeRoles", true, requestedLoginId)).thenReturn(response);
126                 List<Role> roles = roleServiceCntrlAccess.getActiveRoles(requestedLoginId);
127                 Assert.assertNotNull(roles);
128         }
129         
130         @Test
131         public void getRoleFunctionTest() throws IOException {
132                 String requestedLoginId = "xyz";
133                 String code ="abc";
134                 
135                 String responseString = " {        \"code\" : \"abc\",        \"name\" : \"xyz\"   , \"type\" : \"abc\", \"action\" : \"abc\" }";
136 //              Mockito.when(restApiRequestBuilder.getViaREST("v1/function/" + code, true, requestedLoginId)).thenReturn(responseString);
137                 Mockito.when(restApiRequestBuilder.getViaREST(Matchers.anyString(), Matchers.anyBoolean(), Matchers.anyString())).thenReturn(responseString);
138                 RoleFunction roleFunction = roleServiceCntrlAccess.getRoleFunction(requestedLoginId, code);
139                 Assert.assertNotNull(roleFunction);
140         }
141         
142         @Test
143         public void saveRoleFunctionTest() throws IOException {
144                 String requestedLoginId ="123";
145                 RoleFunction domainRoleFunction = new RoleFunction();
146                 domainRoleFunction.setId(1234L);
147                 roleServiceCntrlAccess.saveRoleFunction(requestedLoginId, domainRoleFunction);
148                 Assert.assertTrue(true);
149         }
150
151         @Test
152         public void deleteRoleFunctionTest() throws IOException {
153                 String requestedLoginId ="123";
154                 RoleFunction domainRoleFunction = new RoleFunction();
155                 domainRoleFunction.setId(1234L);
156                 roleServiceCntrlAccess.deleteRoleFunction(requestedLoginId, domainRoleFunction);
157                 Assert.assertTrue(true);
158         }
159
160         @Test
161         public void deleteDependcyRoleRecord() throws IOException {
162                 String requestedLoginId = "123";
163                 Long id = 123L;
164                 roleServiceCntrlAccess.deleteDependcyRoleRecord(requestedLoginId, id);
165                 Assert.assertTrue(true);
166         }
167 }