Search in sources :

Example 31 with JsonValue

use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.

the class StoreLatestSnapshotTest method testThreeThreeDevelopmentVersion.

// @Test
public void testThreeThreeDevelopmentVersion() throws JsonException, IOException, HttpException {
    // Setup json
    json.put("upgradeVersionState", UpgradeVersionState.DEVELOPMENT);
    json.put("currentVersionState", UpgradeVersionState.DEVELOPMENT);
    jsonModules.put("core", latestDevelopmentCore);
    jsonModules.put("mangoApi", latestDevelopmentCore);
    String url = baseUrl + "/servlet/versionCheck";
    HttpPost post = new HttpPost(url);
    StringWriter stringWriter = new StringWriter();
    new JsonWriter(JSON_CONTEXT, stringWriter).writeObject(json);
    String requestData = stringWriter.toString();
    post.setEntity(new StringEntity(requestData));
    String responseData = HttpUtils4.getTextContent(getHttpClient(30000), post, 1);
    JsonTypeReader jsonReader = new JsonTypeReader(responseData);
    JsonValue response = jsonReader.read();
    // printResponse(response);
    Assert.assertNotNull(response.getJsonValue("versionState"));
    Assert.assertEquals("DEVELOPMENT", response.getJsonValue("versionState").toString());
    // Assert newInstalls-oldCore should be empty
    Assert.assertNotNull(response.getJsonValue("newInstalls-oldCore"));
    JsonArray newInstallsOldCore = response.getJsonValue("newInstalls-oldCore").toJsonArray();
    Assert.assertEquals(true, newInstallsOldCore.size() == 0);
    // Assert update-versionState
    Assert.assertNull(response.getJsonValue("update-versionState"));
    // Assert updates for this core iff major upgrade available
    Assert.assertNotNull(response.getJsonValue("updates"));
    JsonArray updates = response.getJsonValue("updates").toJsonArray();
    Assert.assertEquals(true, updates.size() == 0);
    // Assert upgrades
    Assert.assertNotNull(response.getJsonValue("upgrades"));
    JsonArray upgrades = response.getJsonValue("upgrades").toJsonArray();
    Assert.assertEquals(true, upgrades.size() > 0);
    for (JsonValue upgrade : upgrades) {
        Assert.assertEquals(true, upgrade.getJsonValue("fullVersion").toString().startsWith(latestDevelopmentCorePrefix));
    }
    // Assert newInstalls should be for latest production core
    Assert.assertNotNull(response.getJsonValue("newInstalls"));
    JsonArray newInstalls = response.getJsonValue("newInstalls").toJsonArray();
    Assert.assertEquals(true, newInstalls.size() > 0);
    for (JsonValue install : newInstalls) {
        Assert.assertEquals(true, install.getJsonValue("fullVersion").toString().startsWith(latestDevelopmentCorePrefix));
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) JsonValue(com.serotonin.json.type.JsonValue) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 32 with JsonValue

use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.

the class StoreLatestSnapshotTest method testThreeTwoDevelopmentVersion.

// @Test
public void testThreeTwoDevelopmentVersion() throws JsonException, IOException, HttpException {
    // Setup json
    json.put("upgradeVersionState", UpgradeVersionState.DEVELOPMENT);
    json.put("currentVersionState", UpgradeVersionState.DEVELOPMENT);
    jsonModules.put("core", "3.2.2-SNAPSHOT");
    jsonModules.put("mangoApi", "3.2.3-SNAPSHOT");
    String url = baseUrl + "/servlet/versionCheck";
    HttpPost post = new HttpPost(url);
    StringWriter stringWriter = new StringWriter();
    new JsonWriter(JSON_CONTEXT, stringWriter).writeObject(json);
    String requestData = stringWriter.toString();
    post.setEntity(new StringEntity(requestData));
    String responseData = HttpUtils4.getTextContent(getHttpClient(30000), post, 1);
    JsonTypeReader jsonReader = new JsonTypeReader(responseData);
    JsonValue response = jsonReader.read();
    printResponse(response);
    Assert.assertNotNull(response.getJsonValue("versionState"));
    Assert.assertEquals("DEVELOPMENT", response.getJsonValue("versionState").toString());
    // Assert newInstalls-oldCore should be empty
    Assert.assertNotNull(response.getJsonValue("newInstalls-oldCore"));
    JsonArray newInstallsOldCore = response.getJsonValue("newInstalls-oldCore").toJsonArray();
    Assert.assertEquals(true, newInstallsOldCore.size() == 0);
    // Assert update-versionState
    Assert.assertNull(response.getJsonValue("update-versionState"));
    // Assert updates for this core iff major upgrade available
    Assert.assertNotNull(response.getJsonValue("updates"));
    JsonArray updates = response.getJsonValue("updates").toJsonArray();
    Assert.assertEquals(true, updates.size() == 0);
    // Assert upgrades
    Assert.assertNotNull(response.getJsonValue("upgrades"));
    JsonArray upgrades = response.getJsonValue("upgrades").toJsonArray();
    Assert.assertEquals(true, upgrades.size() > 0);
    for (JsonValue upgrade : upgrades) {
        Assert.assertEquals(true, upgrade.getJsonValue("fullVersion").toString().startsWith(latestDevelopmentCorePrefix));
    }
    // Assert newInstalls should be for latest production core
    Assert.assertNotNull(response.getJsonValue("newInstalls"));
    JsonArray newInstalls = response.getJsonValue("newInstalls").toJsonArray();
    Assert.assertEquals(true, newInstalls.size() > 0);
    for (JsonValue install : newInstalls) {
        Assert.assertEquals(true, install.getJsonValue("fullVersion").toString().startsWith(latestDevelopmentCorePrefix));
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) JsonValue(com.serotonin.json.type.JsonValue) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 33 with JsonValue

use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.

the class StoreLatestSnapshotTest method testTwoEightEightReleaseVersion.

// @Test
public void testTwoEightEightReleaseVersion() throws JsonException, IOException, HttpException {
    // Setup json
    json.put("upgradeVersionState", UpgradeVersionState.PRODUCTION);
    json.put("currentVersionState", UpgradeVersionState.PRODUCTION);
    jsonModules.put("core", "2.8.8");
    jsonModules.put("mangoApi", "1.2.0");
    String url = baseUrl + "/servlet/versionCheck";
    HttpPost post = new HttpPost(url);
    StringWriter stringWriter = new StringWriter();
    new JsonWriter(JSON_CONTEXT, stringWriter).writeObject(json);
    String requestData = stringWriter.toString();
    post.setEntity(new StringEntity(requestData));
    String responseData = HttpUtils4.getTextContent(getHttpClient(30000), post, 1);
    JsonTypeReader jsonReader = new JsonTypeReader(responseData);
    JsonValue response = jsonReader.read();
    // printResponse(response);
    Assert.assertNotNull(response.getJsonValue("versionState"));
    Assert.assertEquals("PRODUCTION", response.getJsonValue("versionState").toString());
    // Assert newInstalls-oldCore should be for 2.8
    Assert.assertNotNull(response.getJsonValue("newInstalls-oldCore"));
    JsonArray newInstallsOldCore = response.getJsonValue("newInstalls-oldCore").toJsonArray();
    Assert.assertEquals(true, newInstallsOldCore.size() > 0);
    for (JsonValue nioc : newInstallsOldCore) {
        if (nioc.getJsonValue("name").toString().equals("meta"))
            Assert.assertEquals(true, nioc.getJsonValue("fullVersion").toString().equals("2.3.0"));
    }
    // Assert update-versionState
    Assert.assertNotNull(response.getJsonValue("update-versionState"));
    Assert.assertEquals("PRODUCTION", response.getJsonValue("update-versionState").toString());
    // Assert updates for current core
    Assert.assertNotNull(response.getJsonValue("updates"));
    JsonArray updates = response.getJsonValue("updates").toJsonArray();
    Assert.assertEquals(true, updates.size() > 0);
    for (JsonValue upgrade : updates) {
        if (upgrade.getJsonValue("name").toString().equals("mangoApi"))
            Assert.assertEquals(true, upgrade.getJsonValue("fullVersion").toString().equals("1.2.1"));
    }
    // Assert upgrades
    Assert.assertNotNull(response.getJsonValue("upgrades"));
    JsonArray upgrades = response.getJsonValue("upgrades").toJsonArray();
    Assert.assertEquals(true, upgrades.size() > 0);
    for (JsonValue upgrade : upgrades) {
        if (upgrade.getJsonValue("name").toString().equals("core")) {
            Assert.assertEquals(true, upgrade.getJsonValue("fullVersion").toString().startsWith(latestProductionCore));
        } else {
            Assert.assertEquals(true, upgrade.getJsonValue("fullVersion").toString().startsWith(latestProductionCorePrefix));
        }
    }
    // Assert newInstalls should be for latest production core
    Assert.assertNotNull(response.getJsonValue("newInstalls"));
    JsonArray newInstalls = response.getJsonValue("newInstalls").toJsonArray();
    Assert.assertEquals(true, newInstalls.size() > 0);
    for (JsonValue install : newInstalls) {
        Assert.assertEquals(true, install.getJsonValue("fullVersion").toString().startsWith(latestProductionCorePrefix));
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) JsonValue(com.serotonin.json.type.JsonValue) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 34 with JsonValue

use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.

the class StoreLatestSnapshotTest method testThreeTwoReleaseVersion.

// @Test
public void testThreeTwoReleaseVersion() throws JsonException, IOException, HttpException {
    // Setup json
    json.put("upgradeVersionState", UpgradeVersionState.PRODUCTION);
    json.put("currentVersionState", UpgradeVersionState.PRODUCTION);
    jsonModules.put("core", "3.2.1");
    jsonModules.put("mangoApi", "3.2.1");
    String url = baseUrl + "/servlet/versionCheck";
    HttpPost post = new HttpPost(url);
    StringWriter stringWriter = new StringWriter();
    new JsonWriter(JSON_CONTEXT, stringWriter).writeObject(json);
    String requestData = stringWriter.toString();
    post.setEntity(new StringEntity(requestData));
    String responseData = HttpUtils4.getTextContent(getHttpClient(30000), post, 1);
    JsonTypeReader jsonReader = new JsonTypeReader(responseData);
    JsonValue response = jsonReader.read();
    printResponse(response);
    Assert.assertNull(response.getJsonValue("versionState"));
    // Assert newInstalls-oldCore should be empty
    Assert.assertNotNull(response.getJsonValue("newInstalls-oldCore"));
    JsonArray newInstallsOldCore = response.getJsonValue("newInstalls-oldCore").toJsonArray();
    Assert.assertEquals(true, newInstallsOldCore.size() == 0);
    // Assert update-versionState
    Assert.assertNull(response.getJsonValue("update-versionState"));
    // Assert updates for this core iff major upgrade available
    Assert.assertNotNull(response.getJsonValue("updates"));
    JsonArray updates = response.getJsonValue("updates").toJsonArray();
    Assert.assertEquals(true, updates.size() == 0);
    // Assert upgrades
    Assert.assertNotNull(response.getJsonValue("upgrades"));
    JsonArray upgrades = response.getJsonValue("upgrades").toJsonArray();
    Assert.assertEquals(true, upgrades.size() == 1);
    Assert.assertEquals(true, upgrades.get(0).getJsonValue("name").toString().equals("mangoApi"));
    Assert.assertEquals(true, upgrades.get(0).getJsonValue("fullVersion").toString().equals("3.2.2"));
    // Assert newInstalls should be for latest production core
    Assert.assertNotNull(response.getJsonValue("newInstalls"));
    JsonArray newInstalls = response.getJsonValue("newInstalls").toJsonArray();
    Assert.assertEquals(true, newInstalls.size() > 0);
    for (JsonValue install : newInstalls) {
        Assert.assertEquals(true, install.getJsonValue("fullVersion").toString().startsWith(latestProductionCorePrefix));
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) JsonValue(com.serotonin.json.type.JsonValue) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 35 with JsonValue

use of com.serotonin.json.type.JsonValue in project ma-core-public by infiniteautomation.

the class SubclassTest method main.

public static void main(String[] args) throws Exception {
    JsonContext context = new JsonContext();
    context.addResolver(new TypeResolver() {

        @Override
        public Class<?> resolve(JsonValue jsonValue) throws JsonException {
            if (jsonValue.toJsonObject().containsKey("sub1Value"))
                return Subclass1.class;
            if (jsonValue.toJsonObject().containsKey("sub2Value"))
                return Subclass2.class;
            throw new JsonException("Unknown BaseClass: " + jsonValue);
        }
    }, BaseClass.class);
    // context.addFactory(new ObjectFactory() {
    // @Override
    // public Object create(JsonValue jsonValue) throws JsonException {
    // if (jsonValue.toJsonObject().hasProperty("sub1Value"))
    // return new Subclass1();
    // if (jsonValue.toJsonObject().hasProperty("sub2Value"))
    // return new Subclass2();
    // throw new JsonException("Unknown BaseClass: " + jsonValue);
    // }
    // }, BaseClass.class);
    // List<BaseClass> list = new ArrayList<BaseClass>();
    // list.add(new Subclass1());
    // list.add(new Subclass2());
    // 
    // String json = JsonWriter.writeToString(context, list);
    // 
    // System.out.println(json);
    String json = "[{\"id\":\"Subclass1\",\"sub1Value\":\"a\",\"baseValue\":\"b\"},{\"myId\":\"Subclass2\",\"sub2Value\":\"c\",\"baseValue\":\"d\"}]";
    JsonReader reader = new JsonReader(context, json);
    TypeDefinition type = new TypeDefinition(List.class, BaseClass.class);
    Object read = reader.read(type);
    System.out.println(read);
}
Also used : JsonException(com.serotonin.json.JsonException) JsonContext(com.serotonin.json.JsonContext) TypeResolver(com.serotonin.json.spi.TypeResolver) JsonValue(com.serotonin.json.type.JsonValue) JsonReader(com.serotonin.json.JsonReader) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Aggregations

JsonValue (com.serotonin.json.type.JsonValue)41 JsonArray (com.serotonin.json.type.JsonArray)26 JsonObject (com.serotonin.json.type.JsonObject)21 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)15 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)15 JsonWriter (com.serotonin.json.JsonWriter)10 StringWriter (java.io.StringWriter)10 HttpPost (org.apache.http.client.methods.HttpPost)10 StringEntity (org.apache.http.entity.StringEntity)10 JsonException (com.serotonin.json.JsonException)8 IOException (java.io.IOException)7 JsonString (com.serotonin.json.type.JsonString)5 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)5 User (com.serotonin.m2m2.vo.User)5 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 IntStringPair (com.serotonin.db.pair.IntStringPair)3 JsonReader (com.serotonin.json.JsonReader)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 Type (java.lang.reflect.Type)3