From 9257bc21471485a83f21be039199f761378b8659 Mon Sep 17 00:00:00 2001 From: Andy Coates <8012398+big-andy-coates@users.noreply.github.com> Date: Thu, 8 Sep 2022 13:43:36 +0100 Subject: [PATCH] Make `ComponentValidator` internal (#24) ...as its no longer used outside of the `ResourceInitializer`. --- metadata/README.md | 23 ++++++++++++++----- .../platform/metadata/ResourceHandler.java | 6 ++--- .../resource/ResourceInitializer.java | 4 ++-- .../platform/resource/ComponentValidator.java | 8 ++----- .../resource/ResourceInitializerTest.java | 1 + .../resource/ComponentValidatorTest.java | 5 ++-- 6 files changed, 27 insertions(+), 20 deletions(-) rename resource/src/main/java/org/creekservice/{api => internal}/platform/resource/ComponentValidator.java (97%) rename resource/src/test/java/org/creekservice/{api => internal}/platform/resource/ComponentValidatorTest.java (98%) diff --git a/metadata/README.md b/metadata/README.md index be2f0ff..31138a7 100644 --- a/metadata/README.md +++ b/metadata/README.md @@ -56,12 +56,13 @@ Resource descriptors define the resources a component uses in its [inputs](#inpu There are corresponding `ComponentInput`, `ComponentInternal` and `ComponentOutput` marker interfaces, respectively. Authors of creek services are not expected to implement the above marker interfaces. Creek extensions, -(such as [creek-kafka][1]), expose their own interfaces, which extend the marker interfaces, and which can be used to +(such as [creek-kafka][1]), expose their own interfaces, which extend these marker interfaces, and which can be used to define a resource the component uses, e.g. a Kafka Topic. > ### NOTE -> Extensions expose resource _interfaces_ rather than _classes_ to keep code coupling to a minimum, minimising chances of -> dependency hell, etc. +> The reason extensions expose resource _interfaces_ rather than _classes_ is to keep code coupling to a minimum, +> thereby minimising chances of dependency hell. Extensions provide example code that can be cut & paste for +> creating the required implementations. ### Resource initialization @@ -92,17 +93,23 @@ When the service starts, Creek will automatically create the resource when initi > Future plans are to support a mode where owned resources are created by an initialization tool, prior to deployment. > See [issue-68][2]. +The owned resource types provided by extensions will define helper methods to obtain an unowned descriptor from an +owned one. For example, the `OwnedKafkaTopicOutput` has a `toInput` method to obtain an unowned `KafkaTopicInput`. + ##### Unowned Resources Resources tagged with the `UnownedResource` interface are conceptually owns by another service. For example, services generally consume Kafka the _owned_ output topics of upstream services. Therefore, the service's descriptor will define an _unowned_ descriptor for such resource, e.g. defining the -topic name, key & value types, partition count, etc. The unowned descriptor is created by calling `toInput` on -the owned descriptor. +topic name, key & value types, partition count, etc. When the service starts, Creek will _not_ initialize unowned resources. - + +Unowned resource types provided by extensions should not normally be directly created. Instead, the unowned descriptor +should be created by calling an appropriate helper method on the owned resource. For example, an unowned `KafkaTopicInput` +is obtained by calling `toInput` on the associated `OwnedKafkaTopicOutput`. + ##### Shared Resources Resources tagged with the `SharedResource` interface are conceptually not owned by any service. @@ -113,6 +120,10 @@ that wish to use it. Shared resources are initialised via the [Init tool](https://github.com/creek-service/creek-platform/issues/7) before any service that requires them are deployed. +Shared resource types provided by extensions will implement the `ComponentInput`, `ComponentInternal` and/or `ComponentOutput` +interfaces, as appropriate for the resource type. This allows a shared single definition to be used directly as a component's +input, internal or output. + ##### Unmanaged Resources Any resource descriptor that does not implement one of the resource initialization marker interfaces are deemed not diff --git a/metadata/src/main/java/org/creekservice/api/platform/metadata/ResourceHandler.java b/metadata/src/main/java/org/creekservice/api/platform/metadata/ResourceHandler.java index fa0109f..a752b9d 100644 --- a/metadata/src/main/java/org/creekservice/api/platform/metadata/ResourceHandler.java +++ b/metadata/src/main/java/org/creekservice/api/platform/metadata/ResourceHandler.java @@ -51,11 +51,11 @@ public interface ResourceHandler { *

Instructs an extension to ensure the resources described by the supplied descriptor exist * and are initialized. * - *

Implementations should consider outputting a warning or failing if the resource alreay + *

Implementations should consider outputting a warning or failing if the resource already * exists, but does not match the expected configuration. * - * @param resources the resource instances to ensure exists and are initialized. Resources must - * be {@link ResourceDescriptor#isCreatable creatable}. + * @param resources the resource instances to ensure exists and are initialized. Resources + * passed will be {@link ResourceDescriptor#isCreatable creatable}. */ void ensure(Collection resources); } diff --git a/resource/src/main/java/org/creekservice/api/platform/resource/ResourceInitializer.java b/resource/src/main/java/org/creekservice/api/platform/resource/ResourceInitializer.java index 285bcee..74c5eeb 100644 --- a/resource/src/main/java/org/creekservice/api/platform/resource/ResourceInitializer.java +++ b/resource/src/main/java/org/creekservice/api/platform/resource/ResourceInitializer.java @@ -22,7 +22,6 @@ import static org.creekservice.api.platform.metadata.ResourceDescriptor.isShared; import static org.creekservice.api.platform.metadata.ResourceDescriptor.isUnmanaged; import static org.creekservice.api.platform.metadata.ResourceDescriptor.isUnowned; -import static org.creekservice.api.platform.resource.ComponentValidator.componentValidator; import java.net.URI; import java.util.Collection; @@ -37,6 +36,7 @@ import org.creekservice.api.platform.metadata.ComponentDescriptor; import org.creekservice.api.platform.metadata.ResourceDescriptor; import org.creekservice.api.platform.metadata.ResourceHandler; +import org.creekservice.internal.platform.resource.ComponentValidator; /** * Initializer of resources. @@ -67,7 +67,7 @@ public interface ResourceHandlers { * @return an initializer instance. */ public static ResourceInitializer resourceInitializer(final ResourceHandlers handlers) { - return new ResourceInitializer(handlers, componentValidator()); + return new ResourceInitializer(handlers, new ComponentValidator()); } @VisibleForTesting diff --git a/resource/src/main/java/org/creekservice/api/platform/resource/ComponentValidator.java b/resource/src/main/java/org/creekservice/internal/platform/resource/ComponentValidator.java similarity index 97% rename from resource/src/main/java/org/creekservice/api/platform/resource/ComponentValidator.java rename to resource/src/main/java/org/creekservice/internal/platform/resource/ComponentValidator.java index 4fc67e1..dc72804 100644 --- a/resource/src/main/java/org/creekservice/api/platform/resource/ComponentValidator.java +++ b/resource/src/main/java/org/creekservice/internal/platform/resource/ComponentValidator.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.creekservice.api.platform.resource; +package org.creekservice.internal.platform.resource; import java.lang.reflect.Method; @@ -36,11 +36,7 @@ public final class ComponentValidator { private static final Pattern CTRL_CHAR = Pattern.compile("\\p{Cntrl}"); - public static ComponentValidator componentValidator() { - return new ComponentValidator(); - } - - private ComponentValidator() {} + public ComponentValidator() {} public void validate(final ComponentDescriptor... components) { Arrays.stream(components).forEach(this::validateComponent); diff --git a/resource/src/test/java/org/creekservice/api/platform/resource/ResourceInitializerTest.java b/resource/src/test/java/org/creekservice/api/platform/resource/ResourceInitializerTest.java index 161365a..09209c9 100644 --- a/resource/src/test/java/org/creekservice/api/platform/resource/ResourceInitializerTest.java +++ b/resource/src/test/java/org/creekservice/api/platform/resource/ResourceInitializerTest.java @@ -40,6 +40,7 @@ import org.creekservice.api.platform.metadata.ResourceHandler; import org.creekservice.api.platform.metadata.SharedResource; import org.creekservice.api.platform.metadata.UnownedResource; +import org.creekservice.internal.platform.resource.ComponentValidator; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; diff --git a/resource/src/test/java/org/creekservice/api/platform/resource/ComponentValidatorTest.java b/resource/src/test/java/org/creekservice/internal/platform/resource/ComponentValidatorTest.java similarity index 98% rename from resource/src/test/java/org/creekservice/api/platform/resource/ComponentValidatorTest.java rename to resource/src/test/java/org/creekservice/internal/platform/resource/ComponentValidatorTest.java index 1a2ecb2..0dac744 100644 --- a/resource/src/test/java/org/creekservice/api/platform/resource/ComponentValidatorTest.java +++ b/resource/src/test/java/org/creekservice/internal/platform/resource/ComponentValidatorTest.java @@ -14,9 +14,8 @@ * limitations under the License. */ -package org.creekservice.api.platform.resource; +package org.creekservice.internal.platform.resource; -import static org.creekservice.api.platform.resource.ComponentValidator.componentValidator; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.matchesRegex; @@ -60,7 +59,7 @@ class ComponentValidatorTest { @Mock(name = "jane") private AggregateDescriptor aggregate; - private final ComponentValidator validator = componentValidator(); + private final ComponentValidator validator = new ComponentValidator(); @BeforeEach void setUp() {