Search in sources :

Example 1 with ClassResource

use of org.openstreetmap.atlas.streaming.resource.ClassResource in project atlas-checks by osmlab.

the class ChallengeSerializationTest method serializationWithNameTest.

/**
 * Test if a challenge can be deserialized from a test JSON file with the json resource
 * containing a name for the challenge. And verifies that the name is set correctly.
 */
@Test
public void serializationWithNameTest() {
    // If stored json has name specified, it should keep it
    // Name provided to toGeoJsonFeatureCollection method should be ignored
    final Challenge deserializedChallenge = this.getChallenge("challenges/testChallenge3.json");
    final ClassResource challenge = new ClassResource("challenges/testChallenge3.json");
    final String raw = challenge.readAndClose();
    final JsonObject deserializedJson = deserializedChallenge.toJson("123456789");
    final JsonObject rawJson = getGson().fromJson(raw, JsonObject.class);
    Assert.assertEquals(deserializedJson.get("name"), rawJson.get("name"));
    Assert.assertEquals(deserializedJson.get("description"), rawJson.get("description"));
    Assert.assertEquals(deserializedJson.get("blurb"), rawJson.get("blurb"));
    Assert.assertEquals(deserializedJson.get("instruction"), rawJson.get("instruction"));
}
Also used : ClassResource(org.openstreetmap.atlas.streaming.resource.ClassResource) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 2 with ClassResource

use of org.openstreetmap.atlas.streaming.resource.ClassResource in project atlas-checks by osmlab.

the class ChallengeSerializationTest method serializationWithoutNameTest.

/**
 * Test if a challenge can be deserialized from a test JSON file with the json resource
 * containing no name for the challenge, instead the challenge name set from the code.
 */
@Test
public void serializationWithoutNameTest() {
    // If stored json does not have name specified, it should use the name provided to
    // toGeoJsonFeatureCollection
    final Challenge deserializedChallenge = this.getChallenge("challenges/testChallenge2.json");
    final ClassResource challenge = new ClassResource("challenges/testChallenge2.json");
    final String raw = challenge.readAndClose();
    final JsonObject deserializedJson = deserializedChallenge.toJson("123456789");
    final JsonObject rawJson = getGson().fromJson(raw, JsonObject.class);
    Assert.assertEquals("123456789", deserializedJson.get("name").getAsString());
    Assert.assertNull(rawJson.get("name"));
    Assert.assertEquals(deserializedJson.get("description"), rawJson.get("description"));
    Assert.assertEquals(deserializedJson.get("blurb"), rawJson.get("blurb"));
    Assert.assertEquals(deserializedJson.get("instruction"), rawJson.get("instruction"));
}
Also used : ClassResource(org.openstreetmap.atlas.streaming.resource.ClassResource) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 3 with ClassResource

use of org.openstreetmap.atlas.streaming.resource.ClassResource in project atlas-checks by osmlab.

the class ChallengeSerializationTest method getChallenge.

/**
 * Helper function that converts the resource file into a {@link Challenge}
 *
 * @param resource
 *            The path to the resource file
 * @return A {@link Challenge} object representing the provided resource.
 */
private Challenge getChallenge(final String resource) {
    final ClassResource challengeResource = new ClassResource(resource);
    final JsonObject challengeJSON = challengeResource.getJSONResourceObject(JsonObject.class);
    return getGson().fromJson(challengeJSON, Challenge.class);
}
Also used : ClassResource(org.openstreetmap.atlas.streaming.resource.ClassResource) JsonObject(com.google.gson.JsonObject)

Example 4 with ClassResource

use of org.openstreetmap.atlas.streaming.resource.ClassResource in project atlas-checks by osmlab.

the class ChallengeTest method testChallengeJSON.

@Test
public void testChallengeJSON() {
    final Configuration configuration = new StandardConfiguration(new ClassResource("org/openstreetmap/atlas/checks/base/InlineChallengeTest.json"));
    final BaseTestCheck testCheck = new BaseTestCheck(configuration);
    Assert.assertEquals("description", testCheck.getChallenge().getDescription().toLowerCase());
    Assert.assertEquals("blurb", testCheck.getChallenge().getBlurb().toLowerCase());
    Assert.assertEquals("instruction", testCheck.getChallenge().getInstruction().toLowerCase());
    Assert.assertEquals(ChallengeDifficulty.EASY, testCheck.getChallenge().getDifficulty());
    Assert.assertEquals(ChallengePriority.LOW, testCheck.getChallenge().getDefaultPriority());
    final JsonObject challengeJSON = testCheck.getChallenge().toJson("TestChallenge");
    final JsonObject highPriority = new Gson().fromJson(challengeJSON.get(Challenge.KEY_HIGH_PRIORITY).getAsString(), JsonObject.class);
    final JsonArray rules = highPriority.getAsJsonArray(Challenge.KEY_PRIORITY_RULES);
    Assert.assertEquals(4, rules.size());
    final JsonObject firstRule = rules.get(0).getAsJsonObject();
    Assert.assertEquals(Challenge.VALUE_RULE_ID, firstRule.get(Challenge.KEY_RULE_ID).getAsString());
    Assert.assertEquals(Challenge.VALUE_RULE_FIELD, firstRule.get(Challenge.KEY_RULE_FIELD).getAsString());
    Assert.assertEquals(Challenge.VALUE_RULE_OPERATOR, firstRule.get(Challenge.KEY_RULE_OPERATOR).getAsString());
    Assert.assertEquals(Challenge.VALUE_RULE_TYPE, firstRule.get(Challenge.KEY_RULE_TYPE).getAsString());
    Assert.assertEquals("highway.motorway", firstRule.get(Challenge.KEY_RULE_VALUE).getAsString());
    Assert.assertEquals("highway.motorway_link", rules.get(1).getAsJsonObject().get(Challenge.KEY_RULE_VALUE).getAsString());
    Assert.assertEquals("highway.trunk", rules.get(2).getAsJsonObject().get(Challenge.KEY_RULE_VALUE).getAsString());
    Assert.assertEquals("highway.trunk_link", rules.get(3).getAsJsonObject().get(Challenge.KEY_RULE_VALUE).getAsString());
}
Also used : ClassResource(org.openstreetmap.atlas.streaming.resource.ClassResource) JsonArray(com.google.gson.JsonArray) Configuration(org.openstreetmap.atlas.utilities.configuration.Configuration) StandardConfiguration(org.openstreetmap.atlas.utilities.configuration.StandardConfiguration) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) StandardConfiguration(org.openstreetmap.atlas.utilities.configuration.StandardConfiguration) BaseTestCheck(org.openstreetmap.atlas.checks.base.checks.BaseTestCheck) Test(org.junit.Test)

Aggregations

JsonObject (com.google.gson.JsonObject)4 ClassResource (org.openstreetmap.atlas.streaming.resource.ClassResource)4 Test (org.junit.Test)3 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 BaseTestCheck (org.openstreetmap.atlas.checks.base.checks.BaseTestCheck)1 Configuration (org.openstreetmap.atlas.utilities.configuration.Configuration)1 StandardConfiguration (org.openstreetmap.atlas.utilities.configuration.StandardConfiguration)1