AccessService { public ResponseEntity<String> createPolicy(String token, AccessPolicy accessPolicy){ String jsonStr = JSONUtil.getJSON(accessPolicy); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create Policy"). put(LogMessage.MESSAGE, String.format("Trying to create policy [%s]", jsonStr)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response response = reqProcessor.process("/access/create",jsonStr,token); if(response.getHttpstatus().equals(HttpStatus.OK)) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create Policy"). put(LogMessage.MESSAGE, "Policy creation completed successfully"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Access Policy created \"]}"); } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create Policy"). put(LogMessage.MESSAGE, "Policy creation failed"). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); }  ResponseEntity<String> createPolicy(String token, AccessPolicy accessPolicy); ResponseEntity<String> updatePolicy( String token,  AccessPolicy accessPolicy); ResponseEntity<String> listAllPolices(String token); ResponseEntity<String> getPolicyInfo(String token, String accessid); ResponseEntity<String> deletePolicyInfo(String token, String accessid);  }
@Test public void test_createAccessPolicy_successfully() { Response response =getMockResponse(HttpStatus.OK, true, ""); String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; HashMap<String, String> access = new HashMap<>(); access.put("users/*", "read"); access.put("apps/*", "read"); access.put("shared/*", "read"); AccessPolicy accessPolicy = new AccessPolicy("my-test-policy", access); String jsonStr = "{\"access\": { \"users/*\": \"read\", \"apps/*\": \"read\",\"shared/*\": \"read\" }, \"accessid\": \"my-test-policy\"}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Access Policy created \"]}"); when(reqProcessor.process("/access/create",jsonStr,token)).thenReturn(response); when(JSONUtil.getJSON(accessPolicy)).thenReturn(jsonStr); ResponseEntity<String> responseEntity = accessService.createPolicy(token, accessPolicy); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createAccessPolicy_failure_400() { String responseBody = "{\"errors\":[\"Requried Parameter Missing : accessid\"]}"; Response response =getMockResponse(HttpStatus.BAD_REQUEST, true, responseBody); String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; HashMap<String, String> access = new HashMap<>(); access.put("users/*", "read"); access.put("apps/*", "read"); access.put("shared/*", "read"); AccessPolicy accessPolicy = new AccessPolicy(); accessPolicy.setAccess(access); String jsonStr = "{\"access\": { \"users/*\": \"read\", \"apps/*\": \"read\",\"shared/*\": \"read\" }}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseBody); when(reqProcessor.process("/access/create",jsonStr,token)).thenReturn(response); when(JSONUtil.getJSON(accessPolicy)).thenReturn(jsonStr); ResponseEntity<String> responseEntity = accessService.createPolicy(token, accessPolicy); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }