DefaultUserInformation implements UserInformation { public static DefaultUserInformation fromJson(final JsonObject jsonObject) { checkNotNull(jsonObject, "jsonObject"); @Nullable final String defaultSubject = jsonObject.getValue(JsonFields.DEFAULT_SUBJECT).orElse(null); final List<String> subjects = jsonObject.getValueOrThrow(JsonFields.SUBJECTS) .stream() .filter(JsonValue::isString) .map(JsonValue::asString) .collect(Collectors.toList()); return new DefaultUserInformation(defaultSubject, subjects); } private  DefaultUserInformation(@Nullable final String defaultSubject,
            final List<String> subjects); static DefaultUserInformation fromAuthorizationContext(final AuthorizationContext context); static DefaultUserInformation fromJson(final JsonObject jsonObject); @Override Optional<String> getDefaultSubject(); @Override List<String> getSubjects(); @Override JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> predicate); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void fromJson() { final String defaultSubjectId = "google:i-am-user"; final String otherSubjectId = "eclipse:i-am-not-google"; final JsonObject json = JsonFactory.newObjectBuilder() .set("defaultSubject", defaultSubjectId) .set("subjects", JsonArray.of(defaultSubjectId, otherSubjectId)) .build(); final DefaultUserInformation userInformation = DefaultUserInformation.fromJson(json); assertThat(userInformation.getDefaultSubject()).contains(defaultSubjectId); assertThat(userInformation.getSubjects()).containsExactly(defaultSubjectId, otherSubjectId); }
@Test public void fromJsonThrowsOnMissingSubjects() { final JsonObject json = JsonFactory.newObjectBuilder() .set("defaultSubject", (String) null) .build(); assertThatExceptionOfType(JsonMissingFieldException.class) .isThrownBy(() -> DefaultUserInformation.fromJson(json)); }
@Test public void fromJsonThrowsOnNull() { assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> DefaultUserInformation.fromJson(null)); }