ReservationBodyReader implements MessageBodyReader<Reservation> { @Override public Reservation readFrom(Class<Reservation> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { JsonReader reader = Json.createReader(entityStream); JsonObject jsonObject = reader.readObject(); DateTimeFormatter formatter = ofPattern("yyyy-MM-dd"); LocalDate checkin = parse(requireNonNull(jsonObject.getString(CHECKINDATE.fieldName())), formatter); LocalDate checkout = parse(requireNonNull(jsonObject.getString(CHECKOUTDATE.fieldName())), formatter); BigDecimal cost = null; if (null == jsonObject.getJsonNumber(COSTSINEURO.fieldName())) { cost = reservationCostCalculator.calculateRateFor(STANDARD, checkin, checkout); } else { cost = jsonObject.getJsonNumber(COSTSINEURO.fieldName()).bigDecimalValue(); } return new Reservation(requireNonNull(jsonObject.getString(GUESTID.fieldName())), jsonObject.getString(RESERVATIONNUMBER.fieldName(), ""), requireNonNull(jsonObject.getString(ASSIGNEDROOMID.fieldName())), checkin, checkout, cost); } ReservationBodyReader(); @Inject  ReservationBodyReader(ReservationCostCalculator reservationCostCalculator); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override Reservation readFrom(Class<Reservation> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream);  }
@Test public void a_json_reservation_can_be_read() throws IOException { String json = "{\"guestId\":\"9999\",\"reservationNumber\":\"abc-123\",\"checkinDate\":\"2013-01-01\",\"checkoutDate\":\"2013-02-01\",\"assignedRoomId\":\"999\",\"reservationStatus\":\"CLOSED\",\"costsInEuro\":242.23}"; Reservation reservation = new ReservationBodyReader(new ReservationCostCalculator()).readFrom(Reservation.class, null, null, null, null, new ByteArrayInputStream(json.getBytes())); assertThat(reservation).isNotNull(); assertThat(reservation.getGuest()).isEqualTo("9999"); assertThat(reservation.getReservationNumber()).isEqualTo("abc-123"); assertThat(reservation.getCostsInEuro()).isEqualTo(new BigDecimal("242.23")); assertThat(reservation.getReservationStatus()).isEqualTo(OPEN); assertThat(reservation.getCheckinDate()).isEqualTo(of(2013, 1, 1)); assertThat(reservation.getCheckoutDate()).isEqualTo(of(2013, 2, 1)); assertThat(reservation.getAssignedRoom()).isEqualTo("999"); }