ImmutableSource implements Source { public static Source fromJson(final JsonObject jsonObject, final int index) { final Set<String> readSources = jsonObject.getValue(JsonFields.ADDRESSES) .map(array -> array.stream() .map(JsonValue::asString) .collect(Collectors.toSet())).orElse(Collections.emptySet()); final int readConsumerCount = jsonObject.getValue(JsonFields.CONSUMER_COUNT).orElse(DEFAULT_CONSUMER_COUNT); final Integer readQos = jsonObject.getValue(JsonFields.QOS).orElse(null); final JsonArray authContext = jsonObject.getValue(JsonFields.AUTHORIZATION_CONTEXT) .orElseGet(() -> JsonArray.newBuilder().build()); final List<AuthorizationSubject> authorizationSubjects = authContext.stream() .filter(JsonValue::isString) .map(JsonValue::asString) .map(AuthorizationSubject::newInstance) .collect(Collectors.toList()); final AuthorizationContext readAuthorizationContext = AuthorizationModelFactory.newAuthContext(DittoAuthorizationContextType.PRE_AUTHENTICATED_CONNECTION, authorizationSubjects); final Enforcement readEnforcement = jsonObject.getValue(JsonFields.ENFORCEMENT).map(ImmutableEnforcement::fromJson).orElse(null); final FilteredAcknowledgementRequest readAcknowledgementRequests = jsonObject.getValue(JsonFields.ACKNOWLEDGEMENT_REQUESTS) .map(FilteredAcknowledgementRequest::fromJson) .orElse(null); final HeaderMapping readHeaderMapping = jsonObject.getValue(JsonFields.HEADER_MAPPING).map(ImmutableHeaderMapping::fromJson).orElse(null); final PayloadMapping readPayloadMapping = jsonObject.getValue(JsonFields.PAYLOAD_MAPPING) .map(ImmutablePayloadMapping::fromJson) .orElse(ConnectivityModelFactory.emptyPayloadMapping()); final boolean replyTargetEnabled = jsonObject.getValue(JsonFields.REPLY_TARGET_ENABLED).orElse(DEFAULT_REPLY_TARGET_ENABLED); final ReplyTarget readReplyTarget = jsonObject.getValue(JsonFields.REPLY_TARGET) .flatMap(ImmutableReplyTarget::fromJsonOptional) .orElse(null); return new Builder() .addresses(readSources) .qos(readQos) .authorizationContext(readAuthorizationContext) .consumerCount(readConsumerCount) .index(index) .enforcement(readEnforcement) .acknowledgementRequests(readAcknowledgementRequests) .headerMapping(readHeaderMapping) .payloadMapping(readPayloadMapping) .replyTargetEnabled(replyTargetEnabled) .replyTarget(readReplyTarget) .build(); } private  ImmutableSource(final Builder builder); @Override Set<String> getAddresses(); @Override int getConsumerCount(); @Override AuthorizationContext getAuthorizationContext(); @Override int getIndex(); @Override Optional<Integer> getQos(); @Override Optional<Enforcement> getEnforcement(); @Override Optional<FilteredAcknowledgementRequest> getAcknowledgementRequests(); @Override Optional<HeaderMapping> getHeaderMapping(); @Override PayloadMapping getPayloadMapping(); @Override Optional<ReplyTarget> getReplyTarget(); @Override boolean isReplyTargetEnabled(); @Override JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> thePredicate); static Source fromJson(final JsonObject jsonObject, final int index); @Override boolean equals(final Object o); @Override int hashCode(); @Override String toString();  }
@Test public void fromJsonReturnsExpected() { final Source actual = ImmutableSource.fromJson(SOURCE_JSON_WITH_AUTH_CONTEXT, 0); assertThat(actual).isEqualTo(SOURCE_WITH_AUTH_CONTEXT); }
@Test public void mqttFromJsonReturnsExpected() { final Source actual = ImmutableSource.fromJson(MQTT_SOURCE_JSON, 0); assertThat(actual).isEqualTo(MQTT_SOURCE); }