dynamic forms

This commit is contained in:
Markus Gylling 2022-03-01 17:19:22 +01:00
parent 8ed10ab503
commit 126384fe30
3 changed files with 67 additions and 46 deletions

View File

@ -2,6 +2,11 @@ package org.oneedtech.inspect.oneroster;
import static org.oneedtech.inspect.util.net.HttpMethod.GET;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.oneedtech.inspect.core.MappedJsonInspector;
@ -9,6 +14,8 @@ import org.oneedtech.inspect.core.schema.TransactionKey;
import org.oneedtech.inspect.schema.SchemaKeys;
import org.oneedtech.inspect.util.resource.ResourceType;
import com.google.common.collect.ImmutableSet;
/**
* An Inspector for OneRoster 1.1.
*
@ -17,7 +24,8 @@ import org.oneedtech.inspect.util.resource.ResourceType;
/*
* TODO - what would make the building of this faster? - Autogen of mapping
* between endpoint/responseCode and schemas TODO - what would make maintenance
* between endpoint/responseCode and schemas
* TODO - what would make maintenance
* of this faster? - webhooks when schemas are updated
*/
public class OneRoster11Inspector extends MappedJsonInspector {
@ -31,14 +39,15 @@ public class OneRoster11Inspector extends MappedJsonInspector {
public Builder() {
super();
set(ResourceType.JSON) // ?refine needed?
// add default tests
.add(TransactionKeys.ACADEMIC_SESSIONS_GET_200, SchemaKeys.OR_11_GETALLACADEMICSESSIONS_11_JSON)
.add(TransactionKeys.CLASSES_GET_200, SchemaKeys.OR_11_GETALLCLASSES_11_JSON)
.add(TransactionKeys.COURSES_GET_200, SchemaKeys.OR_11_GETALLCOURSES_11_JSON)
.add(TransactionKeys.DEMOGRAPHICS_GET_200, SchemaKeys.OR_11_GETALLDEMOGRAPHICS_11_JSON)
.add(TransactionKeys.ENROLLMENTS_GET_200, SchemaKeys.OR_11_GETALLENROLLMENTS_11_JSON)
.add(TransactionKeys.ORGS_GET_200, SchemaKeys.OR_11_GETALLORGS_11_JSON)
.add(TransactionKeys.USERS_GET_200, SchemaKeys.OR_11_GETALLUSERS_11_JSON);
// add default tests
//TODO autogen matter:
.add(TransactionKeys.GET_ALL_ACADEMIC_SESSIONS_200, SchemaKeys.OR_11_GETALLACADEMICSESSIONS_11_JSON)
.add(TransactionKeys.GET_ALL_CLASSES_200, SchemaKeys.OR_11_GETALLCLASSES_11_JSON)
.add(TransactionKeys.GET_ALL_COURSES_200, SchemaKeys.OR_11_GETALLCOURSES_11_JSON)
.add(TransactionKeys.GET_ALL_DEMOGRAPHICS_200, SchemaKeys.OR_11_GETALLDEMOGRAPHICS_11_JSON)
.add(TransactionKeys.GET_ALL_ENROLLMENTS_200, SchemaKeys.OR_11_GETALLENROLLMENTS_11_JSON)
.add(TransactionKeys.GET_ALL_ORGS_200, SchemaKeys.OR_11_GETALLORGS_11_JSON)
.add(TransactionKeys.GET_ALL_USERS_200, SchemaKeys.OR_11_GETALLUSERS_11_JSON);
}
@SuppressWarnings("unchecked")
@ -52,15 +61,16 @@ public class OneRoster11Inspector extends MappedJsonInspector {
private static final int[] OK = new int[] { 200 };
@SuppressWarnings("unused")
private static final int[] ERR = IntStream.range(400, 500).toArray();
//TODO autogen matter:
public static final TransactionKey GET_ALL_ACADEMIC_SESSIONS_200 = new TransactionKey("getAllAcademicSessions", GET, "/academicSessions", OK);
public static final TransactionKey GET_ALL_CLASSES_200 = new TransactionKey("getAllClasses", GET, "/classes", OK);
public static final TransactionKey GET_ALL_COURSES_200 = new TransactionKey("getAllCourses", GET, "/courses", OK);
public static final TransactionKey GET_ALL_DEMOGRAPHICS_200 = new TransactionKey("getAllDemographics", GET, "/demographics", OK);
public static final TransactionKey GET_ALL_ENROLLMENTS_200 = new TransactionKey("getAllEnrollments", GET, "/enrollments", OK);
public static final TransactionKey GET_ALL_ORGS_200 = new TransactionKey("getAllOrgs", GET, "/orgs", OK);
public static final TransactionKey GET_ALL_USERS_200 = new TransactionKey("getAllUsers", GET, "/users", OK);
public static final TransactionKey ACADEMIC_SESSIONS_GET_200 = new TransactionKey(GET, "/academicSessions", OK);
public static final TransactionKey CLASSES_GET_200 = new TransactionKey(GET, "/classes", OK);
public static final TransactionKey COURSES_GET_200 = new TransactionKey(GET, "/courses", OK);
public static final TransactionKey DEMOGRAPHICS_GET_200 = new TransactionKey(GET, "/demographics", OK);
public static final TransactionKey ENROLLMENTS_GET_200 = new TransactionKey(GET, "/enrollments", OK);
public static final TransactionKey ORGS_GET_200 = new TransactionKey(GET, "/orgs", OK);
public static final TransactionKey USERS_GET_200 = new TransactionKey(GET, "/users", OK);
}
// private static final Logger logger = LogManager.getLogger();

View File

@ -5,11 +5,17 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.oneedtech.inspect.core.probe.Outcome.ERROR;
import static org.oneedtech.inspect.core.probe.Outcome.WARNING;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.ACADEMIC_SESSIONS_GET_200;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.GET_ALL_ACADEMIC_SESSIONS_200;
import static org.oneedtech.inspect.oneroster.Samples.OR11.AS678;
import static org.oneedtech.inspect.oneroster.Samples.OR11.AS678I;
import static org.oneedtech.inspect.oneroster.Samples.OR11.D198;
import static org.oneedtech.inspect.test.Assertions.*;
import static org.oneedtech.inspect.test.Assertions.assertErrorCount;
import static org.oneedtech.inspect.test.Assertions.assertFatalCount;
import static org.oneedtech.inspect.test.Assertions.assertHasOnlyErrors;
import static org.oneedtech.inspect.test.Assertions.assertInvalid;
import static org.oneedtech.inspect.test.Assertions.assertNotRun;
import static org.oneedtech.inspect.test.Assertions.assertNotRunCount;
import static org.oneedtech.inspect.test.Assertions.assertValid;
import static org.oneedtech.inspect.test.Sample.fieldsToList;
import static org.oneedtech.inspect.util.net.HttpMethod.GET;
@ -17,6 +23,7 @@ import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@ -31,6 +38,7 @@ import org.oneedtech.inspect.core.probe.json.JsonPropertyPredicateProbe;
import org.oneedtech.inspect.core.probe.json.JsonPropertyPresentProbe;
import org.oneedtech.inspect.core.report.Report;
import org.oneedtech.inspect.core.report.ReportItems;
import org.oneedtech.inspect.core.schema.TransactionKey;
import org.oneedtech.inspect.test.Sample;
import org.oneedtech.inspect.util.resource.Resource;
import org.oneedtech.inspect.util.resource.StringResource;
@ -104,11 +112,20 @@ class OneRoster11Tests {
}
@Test
void testTransactionKeys() throws Exception {
Collection<TransactionKey> coll = TransactionKey.toMap( OneRoster11Inspector.TransactionKeys.class).values();
assertTrue(coll.size() > 0);
List<String> strings = TransactionKey.toMap(OneRoster11Inspector.TransactionKeys.class).values().stream().map(k->k.toString()).toList();
assertTrue(strings.size() > 0);
}
@Test
void testAllSamples() throws Exception {
List<Sample> input = fieldsToList(Samples.OR11.class.getFields());
for(Sample sample : input) {
for(Sample sample : input) {
String body = sample.asString();
String endPoint = sample.getTransactionKey().orElseThrow().getEndpoint();
int code = sample.getTransactionKey().orElseThrow().getStatusCodes()[0];
@ -137,21 +154,21 @@ class OneRoster11Tests {
//.set(OneRoster11Inspector.TransactionKeys.ACADEMIC_SESSIONS_GET_200)
//this one succeeds
.add(ACADEMIC_SESSIONS_GET_200, new JsonPropertyPresentProbe("$.academicSessions[*]", "status",
.add(GET_ALL_ACADEMIC_SESSIONS_200, new JsonPropertyPresentProbe("$.academicSessions[*]", "status",
"You really should include status", WARNING))
//this one warns
.add(ACADEMIC_SESSIONS_GET_200, new JsonPropertyPresentProbe("$.academicSessions[*]", "foo",
.add(GET_ALL_ACADEMIC_SESSIONS_200, new JsonPropertyPresentProbe("$.academicSessions[*]", "foo",
"Think again about including foo", WARNING))
//this one errors
.add(ACADEMIC_SESSIONS_GET_200, new JsonPropertyPredicateProbe("$.academicSessions[*].status",
.add(GET_ALL_ACADEMIC_SESSIONS_200, new JsonPropertyPredicateProbe("$.academicSessions[*].status",
JsonPredicates.valueEquals("foo"),
"The status field does not have expected value 'foo'", ERROR))
//this one warns
.add(ACADEMIC_SESSIONS_GET_200, new JsonPropertyPredicateProbe("$.academicSessions[*].type",
.add(GET_ALL_ACADEMIC_SESSIONS_200, new JsonPropertyPredicateProbe("$.academicSessions[*].type",
JsonPredicates.valueMatches(Pattern.compile("(foo|bar)")),
"The type field should have either of the values 'foo' or 'bar'", WARNING))
//this one succeeds
.add(ACADEMIC_SESSIONS_GET_200, new MyCustomTest())
.add(GET_ALL_ACADEMIC_SESSIONS_200, new MyCustomTest())
.build();

View File

@ -1,12 +1,6 @@
package org.oneedtech.inspect.oneroster;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.ACADEMIC_SESSIONS_GET_200;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.CLASSES_GET_200;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.COURSES_GET_200;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.DEMOGRAPHICS_GET_200;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.ENROLLMENTS_GET_200;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.ORGS_GET_200;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.USERS_GET_200;
import static org.oneedtech.inspect.oneroster.OneRoster11Inspector.TransactionKeys.*;
import org.oneedtech.inspect.test.Sample;
@ -14,23 +8,23 @@ public class Samples {
public static final class OR11 {
//valid
public static final Sample AS678 = new Sample("or11/1574845678/academicSessions.json", true, ACADEMIC_SESSIONS_GET_200);
public static final Sample CL678 = new Sample("or11/1574845678/classes.json", true, CLASSES_GET_200);
public static final Sample CO678 = new Sample("or11/1574845678/courses.json", true, COURSES_GET_200);
public static final Sample EN678 = new Sample("or11/1574845678/enrollments.json", true, ENROLLMENTS_GET_200);
public static final Sample OR678 = new Sample("or11/1574845678/orgs.json", true, ORGS_GET_200);
public static final Sample US678 = new Sample("or11/1574845678/users.json", true, USERS_GET_200);
public static final Sample AS198 = new Sample("or11/1598444198/academicSessions.json",true, ACADEMIC_SESSIONS_GET_200);
public static final Sample CL198 = new Sample("or11/1598444198/classes.json", true, CLASSES_GET_200);
public static final Sample CO198 = new Sample("or11/1598444198/courses.json", true, COURSES_GET_200);
public static final Sample OR198 = new Sample("or11/1598444198/orgs.json", true, ORGS_GET_200);
public static final Sample US198 = new Sample("or11/1598444198/users.json", true, USERS_GET_200);
public static final Sample AS678 = new Sample("or11/1574845678/academicSessions.json", true, GET_ALL_ACADEMIC_SESSIONS_200);
public static final Sample CL678 = new Sample("or11/1574845678/classes.json", true, GET_ALL_CLASSES_200);
public static final Sample CO678 = new Sample("or11/1574845678/courses.json", true, GET_ALL_COURSES_200);
public static final Sample EN678 = new Sample("or11/1574845678/enrollments.json", true, GET_ALL_ENROLLMENTS_200);
public static final Sample OR678 = new Sample("or11/1574845678/orgs.json", true, GET_ALL_ORGS_200);
public static final Sample US678 = new Sample("or11/1574845678/users.json", true, GET_ALL_USERS_200);
public static final Sample AS198 = new Sample("or11/1598444198/academicSessions.json",true, GET_ALL_ACADEMIC_SESSIONS_200);
public static final Sample CL198 = new Sample("or11/1598444198/classes.json", true, GET_ALL_CLASSES_200);
public static final Sample CO198 = new Sample("or11/1598444198/courses.json", true, GET_ALL_COURSES_200);
public static final Sample OR198 = new Sample("or11/1598444198/orgs.json", true, GET_ALL_ORGS_200);
public static final Sample US198 = new Sample("or11/1598444198/users.json", true, GET_ALL_USERS_200);
//invalid
public static final Sample AS678I = new Sample("or11/1574845678/academicSessions-invalid.json", false, ACADEMIC_SESSIONS_GET_200);
public static final Sample AS678I = new Sample("or11/1574845678/academicSessions-invalid.json", false, GET_ALL_ACADEMIC_SESSIONS_200);
//TODO which is correct, schema or sample? demographics "true" vs boolean
public static final Sample D678I = new Sample("or11/1574845678/demographics.json", false, DEMOGRAPHICS_GET_200);
public static final Sample D198 = new Sample("or11/1598444198/demographics.json", false, DEMOGRAPHICS_GET_200);
public static final Sample D678I = new Sample("or11/1574845678/demographics.json", false, GET_ALL_DEMOGRAPHICS_200);
public static final Sample D198 = new Sample("or11/1598444198/demographics.json", false, GET_ALL_DEMOGRAPHICS_200);
}
}