MdsRestController { @RequestMapping(value = "/{moduleName}/{namespace}/{entityName}", method = RequestMethod.GET) @ResponseBody public Object get(@PathVariable String moduleName, @PathVariable String namespace, @PathVariable String entityName, @RequestParam Map<String, String> requestParams) { return doGet(entityName, moduleName, namespace, requestParams, null); }  @RequestMapping(value = "/{moduleName}/{namespace}/{entityName}", method = RequestMethod.GET) @ResponseBody Object get(@PathVariable String moduleName, @PathVariable String namespace,
                    @PathVariable String entityName, @RequestParam Map<String, String> requestParams); @RequestMapping(value = "/{moduleName}/{entityName}", method = RequestMethod.GET) @ResponseBody Object get(@PathVariable String moduleName, @PathVariable String entityName,
                    @RequestParam Map<String, String> requestParams); @RequestMapping(value = "/lookup/{entityName}/{lookupName}", method = RequestMethod.GET) @ResponseBody Object lookupGet(@PathVariable String entityName, @PathVariable String lookupName,
                      @RequestParam Map<String, String> requestParams); @RequestMapping(value = "/lookup/{moduleName}/{entityName}/{lookupName}", method = RequestMethod.GET) @ResponseBody Object lookupGet(@PathVariable String moduleName, @PathVariable String entityName,
                      @PathVariable String lookupName, @RequestParam Map<String, String> requestParams); @RequestMapping(value = "/lookup/{moduleName}/{namespace}/{entityName}/{lookupName}", method = RequestMethod.GET) @ResponseBody Object lookupGet(@PathVariable String moduleName, @PathVariable String namespace,
                      @PathVariable String entityName, @PathVariable String lookupName,
                      @RequestParam Map<String, String> requestParams); @RequestMapping(value = "/{entityName}", method = RequestMethod.GET) @ResponseBody Object get(@PathVariable String entityName, @RequestParam Map<String, String> requestParams); @RequestMapping(value = "/{moduleName}/{namespace}/{entityName}", method = RequestMethod.POST) @ResponseBody Object post(@PathVariable String moduleName, @PathVariable String namespace,
                    @PathVariable String entityName, HttpServletRequest request); @RequestMapping(value = "/{moduleName}/{entityName}", method = RequestMethod.POST) @ResponseBody Object post(@PathVariable String moduleName, @PathVariable String entityName,
                     HttpServletRequest request); @RequestMapping(value = "/{entityName}", method = RequestMethod.POST) @ResponseBody Object post(@PathVariable String entityName, HttpServletRequest request); @RequestMapping(value = "/{moduleName}/{namespace}/{entityName}", method = RequestMethod.PUT) @ResponseBody Object put(@PathVariable String moduleName, @PathVariable String namespace,
                     @PathVariable String entityName, HttpServletRequest request); @RequestMapping(value = "/{moduleName}/{entityName}", method = RequestMethod.PUT) @ResponseBody Object put(@PathVariable String moduleName, @PathVariable String entityName,
                     HttpServletRequest request); @RequestMapping(value = "/{entityName}", method = RequestMethod.PUT) @ResponseBody Object put(@PathVariable String entityName, HttpServletRequest request); @RequestMapping(value = "/{moduleName}/{namespace}/{entityName}/{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.OK) void delete(@PathVariable String moduleName, @PathVariable String namespace,
                    @PathVariable String entityName, @PathVariable Long id); @RequestMapping(value = "/{moduleName}/{entityName}/{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.OK) void delete(@PathVariable String moduleName, @PathVariable String entityName,
                    @PathVariable Long id); @RequestMapping(value = "/{entityName}/{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.OK) void delete(@PathVariable String entityName, @PathVariable Long id); @ExceptionHandler({RestNotSupportedException.class, RestLookupNotFoundException.class, RestEntityNotFoundException.class, RestNoLookupResultException.class}) @ResponseStatus(HttpStatus.NOT_FOUND) void handleRestNotSupportedException(Exception e); @ExceptionHandler({RestOperationNotSupportedException.class, RestLookupExecutionForbiddenException.class}) @ResponseStatus(HttpStatus.FORBIDDEN) void handleRestOperationNotSupportedException(Exception e); @ExceptionHandler({RestBadBodyFormatException.class, InvalidParameterException.class, JDOUserException.class, IllegalArgumentException.class, NucleusUserException.class, org.springframework.orm.jdo.JdoUsageException.class}) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody String handleBadBodyException(RuntimeException e); @ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody String handleConstraintViolationException(ConstraintViolationException e); @ExceptionHandler({JdoOptimisticLockingFailureException.class, NucleusOptimisticException.class}) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody String handleOptimisticTransactionException(ConstraintViolationException e);  }
@Test public void shouldReturn404WhenResultNotFoundForSingleValueLookup() throws Exception { when(restFacadeRetriever.getRestFacade(ENTITY_NAME, MODULE_NAME, NAMESPACE)) .thenReturn(restFacade); when(restFacade.executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean())) .thenThrow(new RestNoLookupResultException("No result found!")); String url = buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?lookup=" + LOOKUP_NAME + "&" + LOOKUP_PAGINATION_STR; mockMvc.perform( get(url) ).andExpect(status().isNotFound()); }
@Test public void shouldReturn400WhenWrongParams() throws Exception { String url1 = buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?order=foo"; String url2 = buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?page=foo"; String url3 = buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?pageSize=foo"; String url4 = buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?id=foo"; mockMvc.perform( get(url1) ).andExpect(status().isBadRequest()); mockMvc.perform( get(url2) ).andExpect(status().isBadRequest()); mockMvc.perform( get(url3) ).andExpect(status().isBadRequest()); mockMvc.perform( get(url4) ).andExpect(status().isBadRequest()); }
@Test public void shouldReturn404ForNotExistingLookups() throws Exception { when(restFacadeRetriever.getRestFacade(ENTITY_NAME, MODULE_NAME, NAMESPACE)) .thenReturn(restFacade); when(restFacade.executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean())) .thenThrow(new RestLookupNotFoundException(LOOKUP_NAME)); mockMvc.perform( get(buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?lookup=" + LOOKUP_NAME) ).andExpect(status().isNotFound()); verify(restFacade).executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean()); }
@Test public void shouldReturn403ForForbiddenLookups() throws Exception { when(restFacadeRetriever.getRestFacade(ENTITY_NAME, MODULE_NAME, NAMESPACE)) .thenReturn(restFacade); when(restFacade.executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean())) .thenThrow(new RestLookupExecutionForbiddenException(LOOKUP_NAME)); mockMvc.perform( get(buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?lookup=" + LOOKUP_NAME) ).andExpect(status().isForbidden()); verify(restFacade).executeLookup(eq(LOOKUP_NAME), any(Map.class), any(QueryParams.class), anyBoolean()); }
@Test public void shouldReturn404ForNonexistantId() throws Exception { when(restFacadeRetriever.getRestFacade(ENTITY_NAME, MODULE_NAME, NAMESPACE)) .thenReturn(restFacade); when(restFacade.get(1l, true)).thenThrow(new RestEntityNotFoundException("id", "1l")); mockMvc.perform( get(buildUrl(ENTITY_NAME, MODULE_NAME, NAMESPACE) + "?id=" + 1l) ).andExpect(status().isNotFound()); verify(restFacade).get(1l, true); }