MenuResource { @POST public Response createMenu(@Auth AccessToken token, @Valid Menu menu){ if(token.getUserId().equals(menu.getUserId())){ if(UserType.values()[userDao.getUserById(token.getUserId()).getUserType()]==UserType.FOOD_SERVER || UserType.values()[userDao.getUserById(token.getUserId()).getUserType()]==UserType.ADMIN) { Long id = menuDao.createMenu(menu); menu.setId(id); return Response.ok(menu).build(); } else { return Response.notModified().build(); } } else { return Response.notModified().build(); } } MenuResource(MenuDAO menuDao, MealDAO mealDao, UserDAO userDao); @GET @Path("/{id}") Menu menuById(@PathParam("id") Long id); @GET @Path("/{id}/meals") List<Meal> mealsByMenu(@PathParam("id") Long id); @POST Response createMenu(@Auth AccessToken token, @Valid Menu menu);  }
@Test public void testCreateMenu() throws Exception { Response res = rule.getJerseyTest() .target("/menu") .request(MediaType.APPLICATION_JSON_TYPE) .header("Authorization", "Bearer test") .post(Entity.json(menu)); @SuppressWarnings("rawtypes") LinkedHashMap read = mapper.readValue(res.readEntity(String.class), LinkedHashMap.class); assertThat(res.getStatusInfo().getStatusCode()).isEqualTo(200); assertThat(read.get("id")).isEqualTo(1); verify(menuDao).createMenu(any()); }
@Test public void unauthorizedCreateMenu() throws Exception { Response res = rule.getJerseyTest() .target("/menu") .request(MediaType.APPLICATION_JSON_TYPE) .header("Authorization", "Bearer noauth") .post(Entity.json(menu)); assertThat(res.getStatusInfo().getStatusCode()).isEqualTo(304); verify(menuDao, never()).createMenu(any()); }