@Test public void testRepresentNullPerson() throws ResourceException, IOException { final Variant variant = context.mock(Variant.class); setupCommonInitExpectations("coolStreamId", "person"); context.checking(new Expectations() { { allowing(transManager).getTransaction(with(any(TransactionDefinition.class))); will(returnValue(ts)); oneOf(getPersonMVByAccountId).execute("coolStreamId"); will(returnValue(null)); oneOf(transManager).commit(ts); } }); sut.init(restContext, request, response); Representation actual = sut.represent(variant); assertEquals("MediaType should be text/html", MediaType.TEXT_HTML, actual.getMediaType()); assertTrue(actual.getText().contains("Invalid Person")); context.assertIsSatisfied(); }
@Override public Representation represent(final Variant variant) throws ResourceException { DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); transDef.setReadOnly(true); TransactionStatus currentStatus = transManager.getTransaction(transDef); boolean isValid; String typeString = WordUtils.capitalizeFully(type.toString()); try { switch (type) { case PERSON: PersonModelView pmv = getPersonMVByAccountId.execute(uniqueKey); isValid = pmv != null; break; case GROUP: List<DomainGroupModelView> groups = groupByShortNameDAO.execute(Collections.singletonList(uniqueKey)); isValid = groups.size() == 1; break; default: typeString = "Type"; throw new RuntimeException("only accepts person and group types."); } transManager.commit(currentStatus); } catch (Exception e) { log.warn("Error validating id", e); transManager.rollback(currentStatus); isValid = false; } String styleColor = isValid ? "green" : "red"; String responseString = isValid ? "Valid " : "Invalid "; String styledResponse = "<div style=\"color:" + styleColor + "; font-family:Arial; font-size:smaller\">" + responseString + typeString + "</div>"; Representation rep = new StringRepresentation(styledResponse, MediaType.TEXT_HTML); rep.setExpirationDate(new Date(0L)); return rep; }
StreamIdValidationResource extends SmpResource { @Override public Representation represent(final Variant variant) throws ResourceException { DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); transDef.setReadOnly(true); TransactionStatus currentStatus = transManager.getTransaction(transDef); boolean isValid; String typeString = WordUtils.capitalizeFully(type.toString()); try { switch (type) { case PERSON: PersonModelView pmv = getPersonMVByAccountId.execute(uniqueKey); isValid = pmv != null; break; case GROUP: List<DomainGroupModelView> groups = groupByShortNameDAO.execute(Collections.singletonList(uniqueKey)); isValid = groups.size() == 1; break; default: typeString = "Type"; throw new RuntimeException("only accepts person and group types."); } transManager.commit(currentStatus); } catch (Exception e) { log.warn("Error validating id", e); transManager.rollback(currentStatus); isValid = false; } String styleColor = isValid ? "green" : "red"; String responseString = isValid ? "Valid " : "Invalid "; String styledResponse = "<div style=\"color:" + styleColor + "; font-family:Arial; font-size:smaller\">" + responseString + typeString + "</div>"; Representation rep = new StringRepresentation(styledResponse, MediaType.TEXT_HTML); rep.setExpirationDate(new Date(0L)); return rep; } }
StreamIdValidationResource extends SmpResource { @Override public Representation represent(final Variant variant) throws ResourceException { DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); transDef.setReadOnly(true); TransactionStatus currentStatus = transManager.getTransaction(transDef); boolean isValid; String typeString = WordUtils.capitalizeFully(type.toString()); try { switch (type) { case PERSON: PersonModelView pmv = getPersonMVByAccountId.execute(uniqueKey); isValid = pmv != null; break; case GROUP: List<DomainGroupModelView> groups = groupByShortNameDAO.execute(Collections.singletonList(uniqueKey)); isValid = groups.size() == 1; break; default: typeString = "Type"; throw new RuntimeException("only accepts person and group types."); } transManager.commit(currentStatus); } catch (Exception e) { log.warn("Error validating id", e); transManager.rollback(currentStatus); isValid = false; } String styleColor = isValid ? "green" : "red"; String responseString = isValid ? "Valid " : "Invalid "; String styledResponse = "<div style=\"color:" + styleColor + "; font-family:Arial; font-size:smaller\">" + responseString + typeString + "</div>"; Representation rep = new StringRepresentation(styledResponse, MediaType.TEXT_HTML); rep.setExpirationDate(new Date(0L)); return rep; } StreamIdValidationResource(final DomainMapper<String, PersonModelView> inGetPersonMVByAccountId,
            final GetDomainGroupsByShortNames inGroupByShortNameDAO, final PlatformTransactionManager inTransManager); }
StreamIdValidationResource extends SmpResource { @Override public Representation represent(final Variant variant) throws ResourceException { DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); transDef.setReadOnly(true); TransactionStatus currentStatus = transManager.getTransaction(transDef); boolean isValid; String typeString = WordUtils.capitalizeFully(type.toString()); try { switch (type) { case PERSON: PersonModelView pmv = getPersonMVByAccountId.execute(uniqueKey); isValid = pmv != null; break; case GROUP: List<DomainGroupModelView> groups = groupByShortNameDAO.execute(Collections.singletonList(uniqueKey)); isValid = groups.size() == 1; break; default: typeString = "Type"; throw new RuntimeException("only accepts person and group types."); } transManager.commit(currentStatus); } catch (Exception e) { log.warn("Error validating id", e); transManager.rollback(currentStatus); isValid = false; } String styleColor = isValid ? "green" : "red"; String responseString = isValid ? "Valid " : "Invalid "; String styledResponse = "<div style=\"color:" + styleColor + "; font-family:Arial; font-size:smaller\">" + responseString + typeString + "</div>"; Representation rep = new StringRepresentation(styledResponse, MediaType.TEXT_HTML); rep.setExpirationDate(new Date(0L)); return rep; } StreamIdValidationResource(final DomainMapper<String, PersonModelView> inGetPersonMVByAccountId,
            final GetDomainGroupsByShortNames inGroupByShortNameDAO, final PlatformTransactionManager inTransManager); @Override Representation represent(final Variant variant); }
StreamIdValidationResource extends SmpResource { @Override public Representation represent(final Variant variant) throws ResourceException { DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); transDef.setReadOnly(true); TransactionStatus currentStatus = transManager.getTransaction(transDef); boolean isValid; String typeString = WordUtils.capitalizeFully(type.toString()); try { switch (type) { case PERSON: PersonModelView pmv = getPersonMVByAccountId.execute(uniqueKey); isValid = pmv != null; break; case GROUP: List<DomainGroupModelView> groups = groupByShortNameDAO.execute(Collections.singletonList(uniqueKey)); isValid = groups.size() == 1; break; default: typeString = "Type"; throw new RuntimeException("only accepts person and group types."); } transManager.commit(currentStatus); } catch (Exception e) { log.warn("Error validating id", e); transManager.rollback(currentStatus); isValid = false; } String styleColor = isValid ? "green" : "red"; String responseString = isValid ? "Valid " : "Invalid "; String styledResponse = "<div style=\"color:" + styleColor + "; font-family:Arial; font-size:smaller\">" + responseString + typeString + "</div>"; Representation rep = new StringRepresentation(styledResponse, MediaType.TEXT_HTML); rep.setExpirationDate(new Date(0L)); return rep; } StreamIdValidationResource(final DomainMapper<String, PersonModelView> inGetPersonMVByAccountId,
            final GetDomainGroupsByShortNames inGroupByShortNameDAO, final PlatformTransactionManager inTransManager); @Override Representation represent(final Variant variant);  }