@Test(expected = IllegalArgumentException.class) public void shouldNotAllowNullConstructorDuration() throws Exception { new Conference("", "", null); }
@Override public ConferenceRepresentation from(UriInfo uriInfo, Conference source) { ConferenceRepresentation rep = new ConferenceRepresentation(source.getId(), uriInfo); rep.setName(source.getName()); rep.setTagLine(source.getTagLine()); if(source.getDuration() != null) { rep.setStart(source.getDuration().getStart()); rep.setEnd(source.getDuration().getEnd()); } return rep; }
@SuppressWarnings({ "unchecked", "rawtypes" }) @GET @Path("/{c_id}/session") @Produces({ BASE_JSON_MEDIA_TYPE, BASE_XML_MEDIA_TYPE }) public Response getSessions(@PathParam("c_id") String conferenceId) { Conference conference = getRepository().get(conferenceId); if (conference == null) { return Response.status(Status.BAD_REQUEST).build(); // TODO: Need Business Exception type to explain why? } Collection<SessionRepresentation> sessions = sessionConverter.from(getUriInfo(), (Collection)conference.getSessions()); return Response.ok(new GenericEntity<Collection<SessionRepresentation>>(sessions){}) .type(matchMediaType( SessionResource.SESSION_XML_MEDIA_TYPE, SessionResource.SESSION_JSON_MEDIA_TYPE)) .build(); } }
@Override public Conference update(UriInfo uriInfo, ConferenceRepresentation representation, Conference target) { target.setName(representation.getName()); target.setTagLine(representation.getTagLine()); target.setDuration(new Duration(representation.getStart(), representation.getEnd())); return target; } }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowNullSetterTagLine() throws Exception { Conference conf = new Conference("", "", new Duration(new Date(), new Date())); conf.setTagLine(null); }
@Override protected ResponseSpecification responseValidation(ResponseSpecification spec, Conference conference) { return spec. root("conference"). body("name", equalTo(conference.getName())). body("tagLine", equalTo(conference.getTagLine())). body("start", equalToXmlDate(conference.getDuration().getStart())). body("end", equalToXmlDate(conference.getDuration().getEnd())); }
@Test(expected = UnsupportedOperationException.class) public void shouldNotAllowToAddSessionToSessions() throws Exception { Conference conf = new Conference("", "", new Duration(new Date(), new Date())); Session sess = new Session("", "", new Duration(new Date(), new Date())); conf.getSessions().add(sess); } }
@Test @UsingDataSet({ "conference.yml", "session.yml" }) @ShouldMatchDataSet({ "conference.yml", "session_empty.yml" }) public void shouldBeAbleToRemoveConferenceWithSession() { Conference conference = repository.get("CA"); Session session = conference.getSessions().toArray(new Session[0])[0]; conference.removeSession(session); repository.store(conference); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowNullSetterDuration() throws Exception { Conference conf = new Conference("", "", new Duration(new Date(), new Date())); conf.setDuration(null); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowNullSetterName() throws Exception { Conference conf = new Conference("", "", new Duration(new Date(), new Date())); conf.setName(null); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowToAddNullSession() throws Exception { Conference conf = new Conference("", "", new Duration(new Date(), new Date())); conf.addSession(null); }
@PreRemove public void removeConferenceRef() { if(conference != null) { conference.removeSession(this); } } }
@Override protected void validateUpdatedDomainObject(Conference domain) { Assert.assertEquals(UPDATED_NAME, domain.getName()); }
@POST @Path("/{c_id}/session") @Consumes({ BASE_JSON_MEDIA_TYPE, BASE_XML_MEDIA_TYPE }) public Response createSession(@PathParam("c_id") String conferenceId, SessionRepresentation sessionRepresentation) { Conference conference = getRepository().get(conferenceId); if (conference == null) { return Response.status(Status.BAD_REQUEST).build(); // TODO: Need Business Exception type to explain why? } Session session = sessionConverter.to(getUriInfo(), sessionRepresentation); conference.addSession(session); getRepository().store(conference); return Response.created( UriBuilder.fromResource( SessionResource.class).segment("{id}") .build(session.getId())) .build(); }
@Override protected Conference updateDomainObject(Conference domain) { return domain.setName(UPDATED_NAME); }
@Override public void remove(Session entity) { Conference conf = conferenceRepository.getConferenceBySessionId(entity.getId()); if(conf != null) { conf.removeSession(entity); } sessions.remove(entity); }
@Test @ShouldMatchDataSet(value = { "conference.yml", "session.yml" }, excludeColumns = { "*id" }) public void shouldBeAbleToCreateConferenceWithSession() { Conference conference = createConference(); conference.addSession(createSession()); repository.store(conference); }
@Test @UsingDataSet("conference.yml") @ShouldMatchDataSet(value = { "conference_updated.yml" }) public void shouldBeAbleToChangeConference() { Conference conference = repository.get("CA"); conference.setName("UPDATED"); repository.store(conference); }
@Override protected Conference createInstance() { return new Conference("", "", new Duration(new Date(), new Date())); }
Session getSessionById(String id) { for(Conference conf : getStored()) { for(Session session : conf.getSessions()) { if(session.getId().equals(id)) { return session; } } } return null; }