@Test public void test_configureLdapGroup_successfully_metaUpdate_failure() { String token = "4EpPYDSfgN2D4Gf7UmNO3nuL"; String jsonStr = "{\"groupname\":\"admin\",\"policies\": \"admin,default\"}"; Response responseNotFound = getMockResponse(HttpStatus.NOT_FOUND, false, ""); Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response response = getMockResponse(HttpStatus.MULTI_STATUS, true, "Meta data update failed"); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.MULTI_STATUS).body("{\"messages\":[\"LDAP group configured\",\"Meta data update failed\"]}"); LDAPGroup ldapGroup = new LDAPGroup("admin", "admin,default"); when(JSONUtil.getJSON(ldapGroup)).thenReturn(jsonStr); when(ControllerUtil.updateMetaDataOnConfigChanges(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(response); when(reqProcessor.process("/auth/ldap/groups","{\"groupname\":\"admin\"}",token)).thenReturn(responseNotFound); when(reqProcessor.process("/auth/ldap/groups/configure",jsonStr,token)).thenReturn(responseNoContent); ResponseEntity<String> responseEntity = ldapAuthService.configureLdapGroup(token, ldapGroup); assertEquals(HttpStatus.MULTI_STATUS, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
public ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup){ String jsonStr = JSONUtil.getJSON(ldapGroup); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP Group"). put(LogMessage.MESSAGE, String.format ("Trying to create LDAP group [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); ObjectMapper objMapper = new ObjectMapper(); String currentPolicies = ""; String latestPolicies = ""; String groupName = "" ; try { JsonNode root = objMapper.readTree(jsonStr); groupName = root.get("groupname").asText(); latestPolicies = root.get("policies").asText(); } catch (IOException e) { log.error(e); } Response grpResponse = reqProcessor.process("/auth/ldap/groups","{\"groupname\":\""+groupName+"\"}",token); String responseJson=""; if(HttpStatus.OK.equals(grpResponse.getHttpstatus())){ responseJson = grpResponse.getResponse(); try { currentPolicies = ControllerUtil.getPoliciesAsStringFromJson(objMapper, responseJson); } catch (IOException e) { log.error(e); } } Response response = reqProcessor.process("/auth/ldap/groups/configure",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { response = ControllerUtil.updateMetaDataOnConfigChanges(groupName, "groups", currentPolicies, latestPolicies, token); if(!HttpStatus.OK.equals(response.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"messages\":[\"LDAP group configured\",\""+response.getResponse()+"\"]}"); } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"LDAP Group configured\"]}"); }
LDAPAuthService { public ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup){ String jsonStr = JSONUtil.getJSON(ldapGroup); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP Group"). put(LogMessage.MESSAGE, String.format ("Trying to create LDAP group [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); ObjectMapper objMapper = new ObjectMapper(); String currentPolicies = ""; String latestPolicies = ""; String groupName = "" ; try { JsonNode root = objMapper.readTree(jsonStr); groupName = root.get("groupname").asText(); latestPolicies = root.get("policies").asText(); } catch (IOException e) { log.error(e); } Response grpResponse = reqProcessor.process("/auth/ldap/groups","{\"groupname\":\""+groupName+"\"}",token); String responseJson=""; if(HttpStatus.OK.equals(grpResponse.getHttpstatus())){ responseJson = grpResponse.getResponse(); try { currentPolicies = ControllerUtil.getPoliciesAsStringFromJson(objMapper, responseJson); } catch (IOException e) { log.error(e); } } Response response = reqProcessor.process("/auth/ldap/groups/configure",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { response = ControllerUtil.updateMetaDataOnConfigChanges(groupName, "groups", currentPolicies, latestPolicies, token); if(!HttpStatus.OK.equals(response.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"messages\":[\"LDAP group configured\",\""+response.getResponse()+"\"]}"); } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"LDAP Group configured\"]}"); } }
LDAPAuthService { public ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup){ String jsonStr = JSONUtil.getJSON(ldapGroup); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP Group"). put(LogMessage.MESSAGE, String.format ("Trying to create LDAP group [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); ObjectMapper objMapper = new ObjectMapper(); String currentPolicies = ""; String latestPolicies = ""; String groupName = "" ; try { JsonNode root = objMapper.readTree(jsonStr); groupName = root.get("groupname").asText(); latestPolicies = root.get("policies").asText(); } catch (IOException e) { log.error(e); } Response grpResponse = reqProcessor.process("/auth/ldap/groups","{\"groupname\":\""+groupName+"\"}",token); String responseJson=""; if(HttpStatus.OK.equals(grpResponse.getHttpstatus())){ responseJson = grpResponse.getResponse(); try { currentPolicies = ControllerUtil.getPoliciesAsStringFromJson(objMapper, responseJson); } catch (IOException e) { log.error(e); } } Response response = reqProcessor.process("/auth/ldap/groups/configure",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { response = ControllerUtil.updateMetaDataOnConfigChanges(groupName, "groups", currentPolicies, latestPolicies, token); if(!HttpStatus.OK.equals(response.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"messages\":[\"LDAP group configured\",\""+response.getResponse()+"\"]}"); } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"LDAP Group configured\"]}"); }  }
LDAPAuthService { public ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup){ String jsonStr = JSONUtil.getJSON(ldapGroup); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP Group"). put(LogMessage.MESSAGE, String.format ("Trying to create LDAP group [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); ObjectMapper objMapper = new ObjectMapper(); String currentPolicies = ""; String latestPolicies = ""; String groupName = "" ; try { JsonNode root = objMapper.readTree(jsonStr); groupName = root.get("groupname").asText(); latestPolicies = root.get("policies").asText(); } catch (IOException e) { log.error(e); } Response grpResponse = reqProcessor.process("/auth/ldap/groups","{\"groupname\":\""+groupName+"\"}",token); String responseJson=""; if(HttpStatus.OK.equals(grpResponse.getHttpstatus())){ responseJson = grpResponse.getResponse(); try { currentPolicies = ControllerUtil.getPoliciesAsStringFromJson(objMapper, responseJson); } catch (IOException e) { log.error(e); } } Response response = reqProcessor.process("/auth/ldap/groups/configure",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { response = ControllerUtil.updateMetaDataOnConfigChanges(groupName, "groups", currentPolicies, latestPolicies, token); if(!HttpStatus.OK.equals(response.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"messages\":[\"LDAP group configured\",\""+response.getResponse()+"\"]}"); } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"LDAP Group configured\"]}"); }  ResponseEntity<String> authenticateLdap(UserLogin userLogin); ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup); ResponseEntity<String> listLdapGroups(String token); ResponseEntity<String> fetchLdapGroup(String token, String groupname); ResponseEntity<String> deleteLdapGroup(String token, String groupname); ResponseEntity<String> configureLdapUser(String token, LDAPUser ldapUser); ResponseEntity<String> listLdapUsers(String token); ResponseEntity<String> fetchLdapUser(String token, String username); ResponseEntity<String> deleteLdapUser(String token, String username); }
LDAPAuthService { public ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup){ String jsonStr = JSONUtil.getJSON(ldapGroup); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP Group"). put(LogMessage.MESSAGE, String.format ("Trying to create LDAP group [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); ObjectMapper objMapper = new ObjectMapper(); String currentPolicies = ""; String latestPolicies = ""; String groupName = "" ; try { JsonNode root = objMapper.readTree(jsonStr); groupName = root.get("groupname").asText(); latestPolicies = root.get("policies").asText(); } catch (IOException e) { log.error(e); } Response grpResponse = reqProcessor.process("/auth/ldap/groups","{\"groupname\":\""+groupName+"\"}",token); String responseJson=""; if(HttpStatus.OK.equals(grpResponse.getHttpstatus())){ responseJson = grpResponse.getResponse(); try { currentPolicies = ControllerUtil.getPoliciesAsStringFromJson(objMapper, responseJson); } catch (IOException e) { log.error(e); } } Response response = reqProcessor.process("/auth/ldap/groups/configure",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)) { response = ControllerUtil.updateMetaDataOnConfigChanges(groupName, "groups", currentPolicies, latestPolicies, token); if(!HttpStatus.OK.equals(response.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"messages\":[\"LDAP group configured\",\""+response.getResponse()+"\"]}"); } }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create LDAP group"). put(LogMessage.MESSAGE, "Creation of LDAP group completed"). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"LDAP Group configured\"]}"); }  ResponseEntity<String> authenticateLdap(UserLogin userLogin); ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup); ResponseEntity<String> listLdapGroups(String token); ResponseEntity<String> fetchLdapGroup(String token, String groupname); ResponseEntity<String> deleteLdapGroup(String token, String groupname); ResponseEntity<String> configureLdapUser(String token, LDAPUser ldapUser); ResponseEntity<String> listLdapUsers(String token); ResponseEntity<String> fetchLdapUser(String token, String username); ResponseEntity<String> deleteLdapUser(String token, String username);  }