@Test public void testStoreRepresentation() throws ResourceException, IOException { String json = sut.convertBackgroundToJSON(background).toString(); StringRepresentation jsonRep = new StringRepresentation(json); final Background bg = new Background(new Person()); bg.setBackgroundItems(new ArrayList<BackgroundItem>(), BackgroundItemType.AFFILIATION); bg.setBackgroundItems(new ArrayList<BackgroundItem>(), BackgroundItemType.HONOR); bg.setBackgroundItems(new ArrayList<BackgroundItem>(), BackgroundItemType.INTEREST); bg.setBackgroundItems(new ArrayList<BackgroundItem>(), BackgroundItemType.SKILL); context.checking(new Expectations() { { oneOf(mapper).findOrCreatePersonBackground(with(uuid)); will(returnValue(bg)); oneOf(mapper).flush(uuid); } }); assertEquals(0, bg.getBackgroundItems(BackgroundItemType.AFFILIATION).size()); assertEquals(0, bg.getBackgroundItems(BackgroundItemType.HONOR).size()); assertEquals(0, bg.getBackgroundItems(BackgroundItemType.INTEREST).size()); assertEquals(0, bg.getBackgroundItems(BackgroundItemType.SKILL).size()); sut.storeRepresentation(jsonRep); context.assertIsSatisfied(); assertEquals(3, bg.getBackgroundItems(BackgroundItemType.AFFILIATION).size()); assertEquals(3, bg.getBackgroundItems(BackgroundItemType.HONOR).size()); assertEquals(3, bg.getBackgroundItems(BackgroundItemType.INTEREST).size()); assertEquals(3, bg.getBackgroundItems(BackgroundItemType.SKILL).size()); assertEquals("a", bg.getBackgroundItems(BackgroundItemType.AFFILIATION).get(0).toString()); assertEquals("d", bg.getBackgroundItems(BackgroundItemType.HONOR).get(0).toString()); assertEquals("1", bg.getBackgroundItems(BackgroundItemType.INTEREST).get(0).toString()); assertEquals("4", bg.getBackgroundItems(BackgroundItemType.SKILL).get(0).toString()); }
@Override public void storeRepresentation(final Representation entity) { Background bg = mapper.findOrCreatePersonBackground(uuid); try { String jsontext = entity.getText(); log.debug("PersonBackgroundResource accepted json for uuid: " + uuid); log.debug("PersonBackgroundResource is parsing json: " + jsontext); JSONObject jsonObject = JSONObject.fromObject(jsontext); if (jsonObject.containsKey(AFFILIATIONS_KEY) && jsonObject.containsKey(HONORSAWARDS_KEY) && jsonObject.containsKey(INTERESTSHOBBIES_KEY) && jsonObject.containsKey(SKILLSSPECIALTIES_KEY)) { validateJSONArray(jsonObject.getJSONArray(AFFILIATIONS_KEY), BackgroundItemType.AFFILIATION); clearBackgroundItemsByType(bg, BackgroundItemType.AFFILIATION); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(AFFILIATIONS_KEY), bg, BackgroundItemType.AFFILIATION); validateJSONArray(jsonObject.getJSONArray(HONORSAWARDS_KEY), BackgroundItemType.HONOR); clearBackgroundItemsByType(bg, BackgroundItemType.HONOR); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(HONORSAWARDS_KEY), bg, BackgroundItemType.HONOR); validateJSONArray(jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), BackgroundItemType.INTEREST); clearBackgroundItemsByType(bg, BackgroundItemType.INTEREST); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), bg, BackgroundItemType.INTEREST); validateJSONArray(jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), BackgroundItemType.SKILL); clearBackgroundItemsByType(bg, BackgroundItemType.SKILL); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), bg, BackgroundItemType.SKILL); mapper.flush(uuid); } else { String msg = "Dataset returned for background items " + "does not contain all of the necessary JSONArrays."; log.info(msg); JSONObject validationErrors = new JSONObject(); validationErrors.put(VALIDATION_ERRORS_KEY, msg); getAdaptedResponse().setEntity(validationErrors.toString(), MediaType.APPLICATION_JSON); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } } catch (ValidationException vex) { log.debug("Validation exception for " + uuid + " passing response back to caller"); } catch (Exception e) { String msg = "Unable to persist background items for " + uuid; log.error(msg, e); getAdaptedResponse().setEntity(msg, MediaType.TEXT_PLAIN); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } }
PersonBackgroundResource extends WritableResource { @Override public void storeRepresentation(final Representation entity) { Background bg = mapper.findOrCreatePersonBackground(uuid); try { String jsontext = entity.getText(); log.debug("PersonBackgroundResource accepted json for uuid: " + uuid); log.debug("PersonBackgroundResource is parsing json: " + jsontext); JSONObject jsonObject = JSONObject.fromObject(jsontext); if (jsonObject.containsKey(AFFILIATIONS_KEY) && jsonObject.containsKey(HONORSAWARDS_KEY) && jsonObject.containsKey(INTERESTSHOBBIES_KEY) && jsonObject.containsKey(SKILLSSPECIALTIES_KEY)) { validateJSONArray(jsonObject.getJSONArray(AFFILIATIONS_KEY), BackgroundItemType.AFFILIATION); clearBackgroundItemsByType(bg, BackgroundItemType.AFFILIATION); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(AFFILIATIONS_KEY), bg, BackgroundItemType.AFFILIATION); validateJSONArray(jsonObject.getJSONArray(HONORSAWARDS_KEY), BackgroundItemType.HONOR); clearBackgroundItemsByType(bg, BackgroundItemType.HONOR); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(HONORSAWARDS_KEY), bg, BackgroundItemType.HONOR); validateJSONArray(jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), BackgroundItemType.INTEREST); clearBackgroundItemsByType(bg, BackgroundItemType.INTEREST); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), bg, BackgroundItemType.INTEREST); validateJSONArray(jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), BackgroundItemType.SKILL); clearBackgroundItemsByType(bg, BackgroundItemType.SKILL); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), bg, BackgroundItemType.SKILL); mapper.flush(uuid); } else { String msg = "Dataset returned for background items " + "does not contain all of the necessary JSONArrays."; log.info(msg); JSONObject validationErrors = new JSONObject(); validationErrors.put(VALIDATION_ERRORS_KEY, msg); getAdaptedResponse().setEntity(validationErrors.toString(), MediaType.APPLICATION_JSON); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } } catch (ValidationException vex) { log.debug("Validation exception for " + uuid + " passing response back to caller"); } catch (Exception e) { String msg = "Unable to persist background items for " + uuid; log.error(msg, e); getAdaptedResponse().setEntity(msg, MediaType.TEXT_PLAIN); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } } }
PersonBackgroundResource extends WritableResource { @Override public void storeRepresentation(final Representation entity) { Background bg = mapper.findOrCreatePersonBackground(uuid); try { String jsontext = entity.getText(); log.debug("PersonBackgroundResource accepted json for uuid: " + uuid); log.debug("PersonBackgroundResource is parsing json: " + jsontext); JSONObject jsonObject = JSONObject.fromObject(jsontext); if (jsonObject.containsKey(AFFILIATIONS_KEY) && jsonObject.containsKey(HONORSAWARDS_KEY) && jsonObject.containsKey(INTERESTSHOBBIES_KEY) && jsonObject.containsKey(SKILLSSPECIALTIES_KEY)) { validateJSONArray(jsonObject.getJSONArray(AFFILIATIONS_KEY), BackgroundItemType.AFFILIATION); clearBackgroundItemsByType(bg, BackgroundItemType.AFFILIATION); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(AFFILIATIONS_KEY), bg, BackgroundItemType.AFFILIATION); validateJSONArray(jsonObject.getJSONArray(HONORSAWARDS_KEY), BackgroundItemType.HONOR); clearBackgroundItemsByType(bg, BackgroundItemType.HONOR); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(HONORSAWARDS_KEY), bg, BackgroundItemType.HONOR); validateJSONArray(jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), BackgroundItemType.INTEREST); clearBackgroundItemsByType(bg, BackgroundItemType.INTEREST); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), bg, BackgroundItemType.INTEREST); validateJSONArray(jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), BackgroundItemType.SKILL); clearBackgroundItemsByType(bg, BackgroundItemType.SKILL); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), bg, BackgroundItemType.SKILL); mapper.flush(uuid); } else { String msg = "Dataset returned for background items " + "does not contain all of the necessary JSONArrays."; log.info(msg); JSONObject validationErrors = new JSONObject(); validationErrors.put(VALIDATION_ERRORS_KEY, msg); getAdaptedResponse().setEntity(validationErrors.toString(), MediaType.APPLICATION_JSON); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } } catch (ValidationException vex) { log.debug("Validation exception for " + uuid + " passing response back to caller"); } catch (Exception e) { String msg = "Unable to persist background items for " + uuid; log.error(msg, e); getAdaptedResponse().setEntity(msg, MediaType.TEXT_PLAIN); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } }  }
PersonBackgroundResource extends WritableResource { @Override public void storeRepresentation(final Representation entity) { Background bg = mapper.findOrCreatePersonBackground(uuid); try { String jsontext = entity.getText(); log.debug("PersonBackgroundResource accepted json for uuid: " + uuid); log.debug("PersonBackgroundResource is parsing json: " + jsontext); JSONObject jsonObject = JSONObject.fromObject(jsontext); if (jsonObject.containsKey(AFFILIATIONS_KEY) && jsonObject.containsKey(HONORSAWARDS_KEY) && jsonObject.containsKey(INTERESTSHOBBIES_KEY) && jsonObject.containsKey(SKILLSSPECIALTIES_KEY)) { validateJSONArray(jsonObject.getJSONArray(AFFILIATIONS_KEY), BackgroundItemType.AFFILIATION); clearBackgroundItemsByType(bg, BackgroundItemType.AFFILIATION); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(AFFILIATIONS_KEY), bg, BackgroundItemType.AFFILIATION); validateJSONArray(jsonObject.getJSONArray(HONORSAWARDS_KEY), BackgroundItemType.HONOR); clearBackgroundItemsByType(bg, BackgroundItemType.HONOR); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(HONORSAWARDS_KEY), bg, BackgroundItemType.HONOR); validateJSONArray(jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), BackgroundItemType.INTEREST); clearBackgroundItemsByType(bg, BackgroundItemType.INTEREST); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), bg, BackgroundItemType.INTEREST); validateJSONArray(jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), BackgroundItemType.SKILL); clearBackgroundItemsByType(bg, BackgroundItemType.SKILL); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), bg, BackgroundItemType.SKILL); mapper.flush(uuid); } else { String msg = "Dataset returned for background items " + "does not contain all of the necessary JSONArrays."; log.info(msg); JSONObject validationErrors = new JSONObject(); validationErrors.put(VALIDATION_ERRORS_KEY, msg); getAdaptedResponse().setEntity(validationErrors.toString(), MediaType.APPLICATION_JSON); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } } catch (ValidationException vex) { log.debug("Validation exception for " + uuid + " passing response back to caller"); } catch (Exception e) { String msg = "Unable to persist background items for " + uuid; log.error(msg, e); getAdaptedResponse().setEntity(msg, MediaType.TEXT_PLAIN); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } }  void setMapper(final BackgroundMapper inMapper); @Override Representation represent(final Variant variant); @Override void storeRepresentation(final Representation entity); JSONObject convertBackgroundToJSON(final Background bg); }
PersonBackgroundResource extends WritableResource { @Override public void storeRepresentation(final Representation entity) { Background bg = mapper.findOrCreatePersonBackground(uuid); try { String jsontext = entity.getText(); log.debug("PersonBackgroundResource accepted json for uuid: " + uuid); log.debug("PersonBackgroundResource is parsing json: " + jsontext); JSONObject jsonObject = JSONObject.fromObject(jsontext); if (jsonObject.containsKey(AFFILIATIONS_KEY) && jsonObject.containsKey(HONORSAWARDS_KEY) && jsonObject.containsKey(INTERESTSHOBBIES_KEY) && jsonObject.containsKey(SKILLSSPECIALTIES_KEY)) { validateJSONArray(jsonObject.getJSONArray(AFFILIATIONS_KEY), BackgroundItemType.AFFILIATION); clearBackgroundItemsByType(bg, BackgroundItemType.AFFILIATION); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(AFFILIATIONS_KEY), bg, BackgroundItemType.AFFILIATION); validateJSONArray(jsonObject.getJSONArray(HONORSAWARDS_KEY), BackgroundItemType.HONOR); clearBackgroundItemsByType(bg, BackgroundItemType.HONOR); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(HONORSAWARDS_KEY), bg, BackgroundItemType.HONOR); validateJSONArray(jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), BackgroundItemType.INTEREST); clearBackgroundItemsByType(bg, BackgroundItemType.INTEREST); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(INTERESTSHOBBIES_KEY), bg, BackgroundItemType.INTEREST); validateJSONArray(jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), BackgroundItemType.SKILL); clearBackgroundItemsByType(bg, BackgroundItemType.SKILL); convertJSONArrayToBackgroundItems( jsonObject.getJSONArray(SKILLSSPECIALTIES_KEY), bg, BackgroundItemType.SKILL); mapper.flush(uuid); } else { String msg = "Dataset returned for background items " + "does not contain all of the necessary JSONArrays."; log.info(msg); JSONObject validationErrors = new JSONObject(); validationErrors.put(VALIDATION_ERRORS_KEY, msg); getAdaptedResponse().setEntity(validationErrors.toString(), MediaType.APPLICATION_JSON); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } } catch (ValidationException vex) { log.debug("Validation exception for " + uuid + " passing response back to caller"); } catch (Exception e) { String msg = "Unable to persist background items for " + uuid; log.error(msg, e); getAdaptedResponse().setEntity(msg, MediaType.TEXT_PLAIN); getAdaptedResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } }  void setMapper(final BackgroundMapper inMapper); @Override Representation represent(final Variant variant); @Override void storeRepresentation(final Representation entity); JSONObject convertBackgroundToJSON(final Background bg); static final String MAIN_KEY; static final String UUID_KEY; static final String AFFILIATIONS_KEY; static final String HONORSAWARDS_KEY; static final String INTERESTSHOBBIES_KEY; static final String SKILLSSPECIALTIES_KEY; }