Search in sources :

Example 1 with JSONObject

use of com.sdicons.json.model.JSONObject in project gocd by gocd.

the class MaterialRevisionsJsonBuilderTest method shouldNotProcessPackageMaterialComment.

@Test
public void shouldNotProcessPackageMaterialComment() throws Exception {
    HashMap<String, String> map = new HashMap<>();
    map.put("TYPE", "PACKAGE_MATERIAL");
    map.put("TRACKBACK_URL", "google.com");
    map.put("COMMENT", "comment");
    String packageMaterialComment = JsonHelper.toJsonString(map);
    Modification modification = new Modification("user", packageMaterialComment, "some@com", new Date(), "1234");
    materialRevisions = new MaterialRevisions(new MaterialRevision(MaterialsMother.packageMaterial(), modification));
    JSONArray jsonArray = buildJson();
    JSONObject modificationJson = (JSONObject) ((JSONArray) ((JSONObject) jsonArray.get(0)).get("modifications")).get(0);
    assertThat(modificationJson.get("comment").render(false), is(JsonHelper.toJsonString(packageMaterialComment)));
}
Also used : Modification(com.thoughtworks.go.domain.materials.Modification) JSONObject(com.sdicons.json.model.JSONObject) HashMap(java.util.HashMap) MaterialRevisions(com.thoughtworks.go.domain.MaterialRevisions) JSONArray(com.sdicons.json.model.JSONArray) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) MaterialRevision(com.thoughtworks.go.domain.MaterialRevision) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 2 with JSONObject

use of com.sdicons.json.model.JSONObject in project gocd by gocd.

the class MaterialRevisionsJsonBuilderTest method shouldShowEmptyFolderInJson.

@Test
public void shouldShowEmptyFolderInJson() {
    svnMaterial.setFolder(null);
    JSONArray revisions = buildJson();
    assertThat(revisions.size(), is(1));
    JSONObject jsonValue = (JSONObject) revisions.get(0);
    assertThat(jsonValue.get("folder").render(false), is("\"\""));
}
Also used : JSONObject(com.sdicons.json.model.JSONObject) JSONArray(com.sdicons.json.model.JSONArray) Test(org.junit.jupiter.api.Test)

Example 3 with JSONObject

use of com.sdicons.json.model.JSONObject in project gocd by gocd.

the class JsonValue method getObject.

public JsonValue getObject(Object... keys) {
    JsonValue current = this;
    for (Object key : keys) {
        if (key instanceof String) {
            if (!(current.jsonValue instanceof JSONObject)) {
                throw new IllegalArgumentException(format("Key '%s' does not refer to any attribute of %s", key, current));
            }
            JSONValue value = ((JSONObject) current.jsonValue).get((String) key);
            if (value == null) {
                throw new IllegalArgumentException(format("Key '%s' does not refer to any attribute of %s", key, current));
            }
            current = new JsonValue(value);
        } else if (key instanceof Number) {
            int index = ((Number) key).intValue();
            try {
                current = current.getItem(index);
            } catch (Exception e) {
                String message = String.format("Can not find #%s element in %s", index, current);
                throw new RuntimeException(message, e);
            }
        } else {
            throw new RuntimeException("Could not understand key " + key);
        }
        if (current == null) {
            throw new RuntimeException("No such key " + key + "\n Possible keys are : " + current);
        }
    }
    return current;
}
Also used : JSONValue(com.sdicons.json.model.JSONValue) JSONObject(com.sdicons.json.model.JSONObject) JSONObject(com.sdicons.json.model.JSONObject)

Example 4 with JSONObject

use of com.sdicons.json.model.JSONObject in project gocd by gocd.

the class MaterialRevisionsJsonBuilderTest method shouldShowEachModificationInJson.

@Test
public void shouldShowEachModificationInJson() {
    JSONArray revisions = buildJson();
    JSONObject revision = (JSONObject) revisions.get(0);
    JSONArray jsonArray = (JSONArray) revision.get("modifications");
    assertThat(jsonArray.size(), is(3));
    JSONObject modification = (JSONObject) jsonArray.get(2);
    assertAttributeInJson(modification, "user", ModificationsMother.MOD_USER);
    assertAttributeInJson(modification, "comment", ModificationsMother.MOD_COMMENT);
    assertAttributeInJson(modification, "date", DateUtils.formatISO8601(ModificationsMother.TWO_DAYS_AGO_CHECKIN));
    JSONArray modifiedFiles = (JSONArray) modification.get("modifiedFiles");
    assertThat(modifiedFiles.size(), is(1));
    JSONObject file = (JSONObject) modifiedFiles.get(0);
    assertAttributeInJson(file, "action", ModificationsMother.MOD_MODIFIED_ACTION.toString());
    assertAttributeInJson(file, "fileName", ModificationsMother.MOD_FILE_BUILD_XML);
}
Also used : JSONObject(com.sdicons.json.model.JSONObject) JSONArray(com.sdicons.json.model.JSONArray) Test(org.junit.jupiter.api.Test)

Example 5 with JSONObject

use of com.sdicons.json.model.JSONObject in project gocd by gocd.

the class MaterialRevisionsJsonBuilderTest method shouldContainModificationChanged.

@Test
public void shouldContainModificationChanged() throws Exception {
    materialRevisions.getMaterialRevision(0).markAsChanged();
    JSONArray revisions = buildJson();
    JSONObject revision = (JSONObject) revisions.get(0);
    JSONArray jsonArray = (JSONArray) revision.get("modifications");
    assertAttributeInJson(revision, "changed", "true");
}
Also used : JSONObject(com.sdicons.json.model.JSONObject) JSONArray(com.sdicons.json.model.JSONArray) Test(org.junit.jupiter.api.Test)

Aggregations

JSONObject (com.sdicons.json.model.JSONObject)7 JSONArray (com.sdicons.json.model.JSONArray)6 Test (org.junit.jupiter.api.Test)6 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)2 MaterialRevision (com.thoughtworks.go.domain.MaterialRevision)2 JSONValue (com.sdicons.json.model.JSONValue)1 MaterialRevisions (com.thoughtworks.go.domain.MaterialRevisions)1 Modification (com.thoughtworks.go.domain.materials.Modification)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1