@Test public void givenEmptyJSONManyAssociationStateWhenAddingTwoRefsAtZeroIndexExpectCorrectOrder() throws JSONException { // Fake JSONManyAssociationState JSONObject state = new JSONObject(); state.put(JSONKeys.PROPERTIES, new JSONObject()); state.put(JSONKeys.ASSOCIATIONS, new JSONObject()); state.put(JSONKeys.MANY_ASSOCIATIONS, new JSONObject()); state.put(JSONKeys.NAMED_ASSOCIATIONS, new JSONObject()); JSONEntityState entityState = new JSONEntityState( null, null, "0", System.currentTimeMillis(), EntityReference.parseEntityReference("123"), EntityStatus.NEW, null, state); JSONManyAssociationState jsonState = new JSONManyAssociationState(entityState, new JSONArray()); jsonState.add(0, EntityReference.parseEntityReference("first")); jsonState.add(0, EntityReference.parseEntityReference("second")); assertThat(jsonState.count(), equalTo(2)); }
@Test public void givenEntityReferenceValueWhenSerializingAndDeserializingExpectEquals() { String serialized = valueSerialization.serialize(EntityReference.parseEntityReference("ABCD-1234")); assertThat(serialized, equalTo("ABCD-1234")); EntityReference deserialized = valueSerialization.deserialize(EntityReference.class, serialized); assertThat(deserialized, equalTo(EntityReference.parseEntityReference("ABCD-1234"))); }
@Override public EntityReference associationValueOf(QualifiedName stateName) { try { Object jsonValue = state.getJSONObject(JSONKeys.ASSOCIATIONS).opt(stateName.name()); if (jsonValue == null) { return null; } EntityReference value = jsonValue == JSONObject.NULL ? null : EntityReference.parseEntityReference((String) jsonValue); return value; } catch (JSONException e) { throw new EntityStoreException(e); } }
@Test public void givenJSONManyAssociationStateWhenChangingReferencesExpectCorrectBehavior() throws JSONException { // Fake JSONManyAssociationState JSONObject state = new JSONObject(); state.put(JSONKeys.PROPERTIES, new JSONObject()); state.put(JSONKeys.ASSOCIATIONS, new JSONObject()); state.put(JSONKeys.MANY_ASSOCIATIONS, new JSONObject()); state.put(JSONKeys.NAMED_ASSOCIATIONS, new JSONObject()); JSONEntityState entityState = new JSONEntityState( null, null, "0", System.currentTimeMillis(), EntityReference.parseEntityReference("123"), EntityStatus.NEW, null, state); JSONManyAssociationState jsonState = new JSONManyAssociationState(entityState, new JSONArray()); assertThat(jsonState.contains(EntityReference.parseEntityReference("NOT_PRESENT")), is(false)); jsonState.add(0, EntityReference.parseEntityReference("0")); jsonState.add(1, EntityReference.parseEntityReference("1")); jsonState.add(2, EntityReference.parseEntityReference("2")); assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(true)); assertThat(jsonState.get(0).identity(), equalTo("0")); assertThat(jsonState.get(1).identity(), equalTo("1")); assertThat(jsonState.get(2).identity(), equalTo("2")); assertThat(jsonState.count(), equalTo(3)); jsonState.remove(EntityReference.parseEntityReference("1")); assertThat(jsonState.count(), equalTo(2)); assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(false)); assertThat(jsonState.get(0).identity(), equalTo("0")); assertThat(jsonState.get(1).identity(), equalTo("2")); jsonState.add(2, EntityReference.parseEntityReference("1")); assertThat(jsonState.count(), equalTo(3)); jsonState.add(0, EntityReference.parseEntityReference("A")); jsonState.add(0, EntityReference.parseEntityReference("B")); jsonState.add(0, EntityReference.parseEntityReference("C")); assertThat(jsonState.count(), equalTo(6)); assertThat(jsonState.get(0).identity(), equalTo("C")); assertThat(jsonState.get(1).identity(), equalTo("B")); assertThat(jsonState.get(2).identity(), equalTo("A")); assertThat(jsonState.contains(EntityReference.parseEntityReference("C")), is(true)); assertThat(jsonState.contains(EntityReference.parseEntityReference("B")), is(true)); assertThat(jsonState.contains(EntityReference.parseEntityReference("A")), is(true)); assertThat(jsonState.contains(EntityReference.parseEntityReference("0")), is(true)); assertThat(jsonState.contains(EntityReference.parseEntityReference("2")), is(true)); assertThat(jsonState.contains(EntityReference.parseEntityReference("1")), is(true)); List<String> refList = toList( map( new Function<EntityReference, String>() { @Override public String map(EntityReference from) { return from.identity(); } }, jsonState)); assertThat(refList.isEmpty(), is(false)); assertArrayEquals(new String[] {"C", "B", "A", "0", "2", "1"}, refList.toArray()); }