public LoadingClassifier(final String classResource) { this(new ClassResource(classResource)); }
/** * Creates a Challenge based on a JSON resource file * * @param checkName * The name of the check that will look for the json challenge info * @return The challenge based from the resource json file */ public static Challenge fromResource(final String checkName) { final ClassResource challenge = new ClassResource(checkName + FileSuffix.JSON.toString()); return challenge.getJSONResourceObject(Survey.class); }
private void loadFromTextResource(final Field field, final CoreTestRule rule, final CreationContext context, final String resourcePath) { final String packageName = rule.getClass().getPackage().getName().replaceAll("\\.", "/"); final String completeName = String.format("%s/%s", packageName, resourcePath); final ClassResource resource = new ClassResource(completeName); FileSuffix.suffixFor(completeName).ifPresent(suffix -> { if (suffix == FileSuffix.GZIP) { resource.setDecompressor(Decompressor.GZIP); } }); try { field.set(rule, new TextAtlasBuilder().read(resource)); } catch (IllegalArgumentException | IllegalAccessException e) { throw new CoreException("Error loading from text resource {}", resourcePath, e); } }
/** * 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); } }
final ClassResource resource = new ClassResource(completeName); FileSuffix.suffixFor(completeName).ifPresent(suffix ->
/** * 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")); }
/** * 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")); }
new ClassResource("org/openstreetmap/atlas/checks/base/InlineChallengeTest.json")); final BaseTestCheck testCheck = new BaseTestCheck(configuration); Assert.assertEquals("description", testCheck.getChallenge().getDescription().toLowerCase());