+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void saveRoleNewTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ JSONObject roleJson = new JSONObject();
+ roleJson.put("name", "test");
+ JSONObject roleJson2 = new JSONObject();
+ List<JSONObject> childRolesJson = new ArrayList<>();
+ List<JSONObject> roleFunctions = new ArrayList<>();
+ roleJson2.put("role", roleJson);
+ roleJson2.put("childRoles", childRolesJson);
+ roleJson2.put("roleFunctions", roleFunctions);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode actualObj = mapper.readTree(roleJson2.toString());
+ Mockito.when(mockedRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(
+ new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8))));
+ Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
+ Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
+ Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
+ Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role());
+ ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
+ Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any()))
+ .thenReturn(externalRequestFieldsValidator);
+ Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("role", new CentralV2Role(null, "test"));
+ expected.put("status", "Success");
+ assertEquals(expected, actual);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void saveRoleUpdateTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ JSONObject roleJson = new JSONObject();
+ roleJson.put("id", 1);
+ roleJson.put("name", "test");
+ roleJson.put("active", true);
+ JSONObject roleJson2 = new JSONObject();
+ List<JSONObject> childRolesJson = new ArrayList<>();
+ List<JSONObject> roleFunctions = new ArrayList<>();
+ JSONObject roleFunction = new JSONObject();
+ roleFunction.put("code", "test");
+ roleFunction.put("name", "test");
+ roleFunction.put("type", "test");
+ roleFunction.put("action", "test");
+ roleFunctions.add(roleFunction);
+ roleJson.put("roleFunctions", roleFunctions);
+ roleJson2.put("role", roleJson);
+ roleJson2.put("childRoles", childRolesJson);
+ roleJson2.put("roleFunctions", roleFunctions);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode actualObj = mapper.readTree(roleJson2.toString());
+ Mockito.when(mockedRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(
+ new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8))));
+ Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
+ Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
+ Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
+ Mockito.when(externalAccessRolesService.ConvertCentralRoleToRole(Matchers.anyString())).thenReturn(new Role());
+ ExternalRequestFieldsValidator externalRequestFieldsValidator = new ExternalRequestFieldsValidator(true, "");
+ Mockito.when(externalAccessRolesService.saveRoleForApplication(Matchers.any(), Matchers.any()))
+ .thenReturn(externalRequestFieldsValidator);
+ CentralV2Role cenV2Role = new CentralV2Role(1l, "test1");
+ cenV2Role.setActive(true);
+ Mockito.when(externalAccessRolesService.getRoleInfo(Matchers.anyLong(), Matchers.any())).thenReturn(cenV2Role);
+ Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("status", "Success");
+ assertEquals(expected.get("status"), actual.get("status"));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void saveRoleInvalidRoleExceptionTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(true);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ JSONObject roleJson = new JSONObject();
+ roleJson.put("id", 1);
+ roleJson.put("name", "test%%");
+ roleJson.put("active", true);
+ JSONObject roleJson2 = new JSONObject();
+ List<JSONObject> childRolesJson = new ArrayList<>();
+ List<JSONObject> roleFunctions = new ArrayList<>();
+ JSONObject roleFunction = new JSONObject();
+ roleFunction.put("code", "test");
+ roleFunction.put("name", "test");
+ roleFunction.put("type", "test");
+ roleFunction.put("action", "test");
+ roleFunctions.add(roleFunction);
+ roleJson.put("roleFunctions", roleFunctions);
+ roleJson2.put("role", roleJson);
+ roleJson2.put("childRoles", childRolesJson);
+ roleJson2.put("roleFunctions", roleFunctions);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode actualObj = mapper.readTree(roleJson2.toString());
+ Mockito.when(mockedRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(
+ new ByteArrayInputStream(actualObj.toString().getBytes(StandardCharsets.UTF_8))));
+ Mockito.when(mockedRequest.getReader()).thenReturn(new BufferedReader(new StringReader(actualObj.toString())));
+ Mockito.when(mockedRequest.getContentType()).thenReturn("application/json");
+ Mockito.when(mockedRequest.getCharacterEncoding()).thenReturn("UTF-8");
+ Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("error", "Invalid role name found for 'test%%'. Any one of the following characters '%,(),=,:,comma, and double quotes' are not allowed");
+ assertEquals(expected.get("error"), actual.get("error"));
+ }
+
+ @Test
+ public void saveRoleUnauthorizedUserExceptionTest() throws Exception {
+ PowerMockito.mockStatic(EPUserUtils.class);
+ PowerMockito.mockStatic(EcompPortalUtils.class);
+ Mockito.when(EcompPortalUtils.checkIfRemoteCentralAccessAllowed()).thenReturn(true);
+ List<EPUser> epuserList = new ArrayList<>();
+ List<EPApp> appList = new ArrayList<>();
+ appList.add(CentralApp());
+ EPUser user = mockUser.mockEPUser();
+ epuserList.add(user);
+ Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
+ Mockito.when(appService.getApp((long) 1)).thenReturn(CentralApp());
+ Mockito.when(adminRolesService.isSuperAdmin(Matchers.anyObject())).thenReturn(false);
+ Mockito.when(externalAccessRolesService.getApp(Matchers.anyString())).thenReturn(appList);
+ ResponseEntity<String> response = new ResponseEntity<>(HttpStatus.OK);
+ Mockito.when(externalAccessRolesService.getNameSpaceIfExists(Matchers.anyObject())).thenReturn(response);
+ Map<String, Object> actual = roleManageController.saveRole(mockedRequest, mockedResponse, CentralApp().getId());
+ final Map<String, Object> expected = new HashMap<>();
+ expected.put("error", " Unauthorized user");
+ assertEquals(expected.get("error"), actual.get("error"));
+ }