@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")));
  }
 public List<Field> listFieldGroupFields(Field field) {
   List<Field> groupFields = new ArrayList<Field>();
   for (FieldGroupFieldInstance instance : data.groupFields()) {
     Field theField = ((FieldGroupValue.Data) instance).fieldGroup().get();
     if (EntityReference.getEntityReference(theField)
         .identity()
         .equals(EntityReference.getEntityReference(field).identity())) {
       groupFields.add(instance.getFieldGroupField());
     }
   }
   return groupFields;
 }
 public void removeFieldGroupFields(Field field) {
   List<FieldGroupFieldInstance> toBeRemoved = new ArrayList<FieldGroupFieldInstance>();
   for (FieldGroupFieldInstance instance : data.groupFields()) {
     Field theField = ((FieldGroupValue.Data) instance).fieldGroup().get();
     if (EntityReference.getEntityReference(theField)
         .identity()
         .equals(EntityReference.getEntityReference(field).identity())) {
       toBeRemoved.add(instance);
     }
   }
   for (FieldGroupFieldInstance instance : toBeRemoved) {
     fieldGroupFieldsRemoved(null, instance);
   }
 }
 @Override
 public void setAssociationValue(QualifiedName stateName, EntityReference newEntity) {
   try {
     cloneStateIfGlobalStateLoaded();
     state
         .getJSONObject(JSONKeys.ASSOCIATIONS)
         .put(stateName.name(), newEntity == null ? null : newEntity.identity());
     markUpdated();
   } catch (JSONException e) {
     throw new EntityStoreException(e);
   }
 }
  @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);
    }
  }
  @After
  public void printResult() throws Exception {
    UnitOfWork uow = assembler.unitOfWorkFactory().newUnitOfWork(newUsecase("Print Gantt chart"));

    try {
      List<ActivityData> activities = uow.get(ProjectData.class, PROJECT).allActivities();

      StringBuilder sb = new StringBuilder();
      sb.append("\n\n### Gantt chart showing planned activities:\n");
      Collections.sort(
          activities,
          new Comparator<ActivityData>() {
            public int compare(ActivityData a1, ActivityData a2) {
              if (a1.earlyStart().get().equals(a2.earlyStart().get()))
                return getEntityReference(a1)
                    .identity()
                    .compareTo(getEntityReference(a2).identity());
              else return a1.earlyStart().get() - a2.earlyStart().get();
            }
          });

      for (ActivityData activity : activities) {
        String id = EntityReference.getEntityReference(activity).identity();
        Integer start = activity.earlyStart().get();
        Integer duration = activity.duration().get();
        for (int i = 1; i < start; i++) {
          sb.append("  ");
        }
        sb.append(id);
        for (int i = 0; i < duration - 1; i++) {
          sb.append(" =");
        }
        sb.append("\n");
      }

      sb.append("1 2 3 4 5 6 7 8 9");
      System.out.println(sb.toString());
    } finally {
      uow.discard();
    }
    System.out.println("----------------------------------------------------------");
  }
  @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());
  }
 private File getDataFile(EntityReference ref) {
   return getDataFile(ref.identity());
 }