@ExceptionHandler public View handleException(Exception t, HttpServletRequest request) throws CodeStoreException { CodeStoreException e = new CodeStoreException("Unexpected error", t, HttpStatus.INTERNAL_SERVER_ERROR); if (t instanceof CodeStoreException) { e = (CodeStoreException) t; } // User can supply trace=true or just trace (unspecified) to get stack // traces boolean trace = request.getParameter("trace") != null && !request.getParameter("trace").equals("false"); return new ConvertingExceptionView(new ResponseEntity<ExceptionReport>(new ExceptionReport(e, trace), e.getStatus()), messageConverters); } }
@Test public void testGenerateCodeWithExpiresAtInThePast() { String data = "{}"; Timestamp expiresAt = new Timestamp(currentTime.get() - 60000); ExpiringCode expiringCode = new ExpiringCode(null, expiresAt, data, null); try { codeStoreEndpoints.generateCode(expiringCode); fail("expiresAt is in the past, should throw CodeStoreException."); } catch (CodeStoreException e) { assertEquals(e.getStatus(), HttpStatus.BAD_REQUEST); } }
@RequestMapping(value = "/Codes/{code}", method = RequestMethod.GET) @ResponseBody public ExpiringCode retrieveCode(@PathVariable String code) { ExpiringCode result = null; try { result = expiringCodeStore.retrieveCode(code, IdentityZoneHolder.get().getId()); } catch (NullPointerException e) { throw new CodeStoreException("code is required.", HttpStatus.BAD_REQUEST); } if (result == null) { throw new CodeStoreException("Code not found: " + code, HttpStatus.NOT_FOUND); } return result; }
@Test public void testGenerateCodeWithNullData() { Timestamp expiresAt = new Timestamp(currentTime.get() + 60000); ExpiringCode expiringCode = new ExpiringCode(null, expiresAt, null, null); try { codeStoreEndpoints.generateCode(expiringCode); fail("code is null, should throw CodeStoreException."); } catch (CodeStoreException e) { assertEquals(e.getStatus(), HttpStatus.BAD_REQUEST); } }
@RequestMapping(value = { "/Codes" }, method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) @ResponseBody public ExpiringCode generateCode(@RequestBody ExpiringCode expiringCode) { try { return expiringCodeStore.generateCode(expiringCode.getData(), expiringCode.getExpiresAt(), null, IdentityZoneHolder.get().getId()); } catch (NullPointerException e) { throw new CodeStoreException("data and expiresAt are required.", HttpStatus.BAD_REQUEST); } catch (IllegalArgumentException e) { throw new CodeStoreException("expiresAt must be in the future.", HttpStatus.BAD_REQUEST); } catch (DataIntegrityViolationException e) { throw new CodeStoreException("Duplicate code generated.", HttpStatus.INTERNAL_SERVER_ERROR); } }
@Test public void testGenerateCodeWithDuplicateCode() { RandomValueStringGenerator generator = mock(RandomValueStringGenerator.class); when(generator.generate()).thenReturn("duplicate"); expiringCodeStore.setGenerator(generator); String data = "{}"; Timestamp expiresAt = new Timestamp(currentTime.get() + 60000); ExpiringCode expiringCode = new ExpiringCode(null, expiresAt, data, null); try { codeStoreEndpoints.generateCode(expiringCode); codeStoreEndpoints.generateCode(expiringCode); fail("duplicate code generated, should throw CodeStoreException."); } catch (CodeStoreException e) { assertEquals(e.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR); } }
@Test public void testRetrieveCodeWithCodeNotFound() { try { codeStoreEndpoints.retrieveCode("unknown"); fail("Non-existent code, should throw CodeStoreException."); } catch (CodeStoreException e) { assertEquals(e.getStatus(), HttpStatus.NOT_FOUND); } }
@Test public void testRetrieveCodeWithNullCode() { try { codeStoreEndpoints.retrieveCode(null); fail("code is null, should throw CodeStoreException."); } catch (CodeStoreException e) { assertEquals(e.getStatus(), HttpStatus.BAD_REQUEST); } }
@Test public void testRetrieveCodeWithExpiredCode() { String data = "{}"; int expiresIn = 1000; Timestamp expiresAt = new Timestamp(currentTime.get() + expiresIn); ExpiringCode expiringCode = new ExpiringCode(null, expiresAt, data, null); ExpiringCode generatedCode = codeStoreEndpoints.generateCode(expiringCode); currentTime.addAndGet(expiresIn + 1); try { codeStoreEndpoints.retrieveCode(generatedCode.getCode()); fail("code is expired, should throw CodeStoreException."); } catch (CodeStoreException e) { assertEquals(e.getStatus(), HttpStatus.NOT_FOUND); } } }
@Test public void testGenerateCodeWithNullExpiresAt() { String data = "{}"; ExpiringCode expiringCode = new ExpiringCode(null, null, data, null); try { codeStoreEndpoints.generateCode(expiringCode); fail("expiresAt is null, should throw CodeStoreException."); } catch (CodeStoreException e) { assertEquals(e.getStatus(), HttpStatus.BAD_REQUEST); } }
@Test public void testRetrieveCode() { String data = "{}"; Timestamp expiresAt = new Timestamp(currentTime.get() + 60000); ExpiringCode expiringCode = new ExpiringCode(null, expiresAt, data, null); ExpiringCode generatedCode = codeStoreEndpoints.generateCode(expiringCode); ExpiringCode retrievedCode = codeStoreEndpoints.retrieveCode(generatedCode.getCode()); assertEquals(generatedCode, retrievedCode); try { codeStoreEndpoints.retrieveCode(generatedCode.getCode()); fail("One-use code already retrieved, should throw CodeStoreException."); } catch (CodeStoreException e) { assertEquals(e.getStatus(), HttpStatus.NOT_FOUND); } }