feat: jpa, messaging, notification, mongo, graphql 어댑터터 리펙토링

This commit is contained in:
DongHyeonka
2026-08-18 10:59:56 +09:00
parent f56dd3e4eb
commit 16e33ce986
24 changed files with 1343 additions and 37 deletions
@@ -0,0 +1,48 @@
package dev.caskeleton.messaging.runtime;
import dev.caskeleton.messaging.api.destination.DestinationName;
import dev.caskeleton.messaging.policy.DestinationProfile;
import dev.caskeleton.messaging.security.DestinationAccessPolicy;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* The access policy a deployment gets when it has not written one (MSG-INT-003).
*
* <p>{@link DestinationAccessPolicy} is three sets of destination names and has a {@code denyAll()}
* factory. Neither is a usable default on its own:
*
* <ul>
* <li><b>Deny everything</b> and the platform assembles, starts, and refuses every publish — a
* deployment that configured its destinations correctly still cannot send, and the error
* names an access policy nobody knew they had to write.
* <li><b>Allow everything</b> and the check is decoration. The policy exists because relying on
* broker ACLs alone means an accidental publish surfaces as a generic authorization error at
* runtime, in the adapter, with no record of which application module attempted it.
* </ul>
*
* <p>So the default is neither: <b>a deployment may publish to the destinations it declared.</b>
* Declaring a destination profile is already an act of configuration — it states the ordering
* guarantee, the retry policy and the dead-letter target — and a message to a destination nobody
* declared is not an access-control edge case, it is a typo or a module reaching past its own
* contract. That is exactly what this check is for, and it is now the thing it catches.
*
* <p>Consume and administer stay empty. A publisher's default has no business granting either, and
* a deployment that needs them replaces this bean — which is the point of it being a bean.
*/
public final class DeclaredDestinationAccess {
private DeclaredDestinationAccess() {}
/**
* Builds the policy from the declared destination profiles.
*
* @param destinations every destination the deployment configured
* @return a policy permitting publication to those destinations and nothing else
*/
public static DestinationAccessPolicy of(Map<DestinationName, DestinationProfile> destinations) {
Objects.requireNonNull(destinations, "destinations");
return new DestinationAccessPolicy(Set.copyOf(destinations.keySet()), Set.of(), Set.of());
}
}
@@ -0,0 +1,89 @@
package dev.caskeleton.messaging.runtime;
import dev.caskeleton.messaging.api.ContentType;
import dev.caskeleton.messaging.schema.MessageCodec;
import dev.caskeleton.messaging.schema.MessageCodecRegistry;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
* The codec registry the publisher resolves against (MSG-INT-003).
*
* <p>{@link MessageCodecRegistry} was an interface with no implementation anywhere — declared,
* consumed by {@link DefaultMessagePublisher}, and satisfiable by nothing. That is why the platform
* could not assemble a publisher even though every piece around it existed: the publisher's
* constructor named a type the repository never built.
*
* <p>The default codec is a deliberate choice rather than "the first one registered". Selecting one
* by iteration order means the encoding a message is written with depends on how the map was
* populated, which is a wire-format decision made by accident. The registry takes it explicitly and
* refuses to be constructed without it.
*
* <p>The raw-bytes codec is never eligible as the default — that is the contract's own rule, and
* the reason is that raw bytes silently disable schema validation for every destination that forgot
* to declare an encoding. A registry that could default to it would turn a missing declaration into
* a skipped check.
*/
public final class RegisteredMessageCodecs implements MessageCodecRegistry {
private final Map<ContentType, MessageCodec> byContentType;
private final MessageCodec defaultCodec;
private RegisteredMessageCodecs(
Map<ContentType, MessageCodec> byContentType, MessageCodec defaultCodec) {
this.byContentType = Map.copyOf(byContentType);
this.defaultCodec = defaultCodec;
}
/**
* Builds a registry from the codecs a deployment composed.
*
* @param defaultCodec the codec a destination with no declared encoding is written with
* @param codecs every codec available, including the default
* @return the registry
* @throws IllegalArgumentException when two codecs claim one content type, or the default is the
* raw-bytes codec
*/
public static RegisteredMessageCodecs of(MessageCodec defaultCodec, MessageCodec... codecs) {
Objects.requireNonNull(defaultCodec, "defaultCodec");
Objects.requireNonNull(codecs, "codecs");
if (ContentType.OCTET_STREAM.equals(defaultCodec.contentType())) {
throw new IllegalArgumentException(
"the raw bytes codec must not be the default: every destination that has not declared an "
+ "encoding would silently skip schema validation");
}
Map<ContentType, MessageCodec> byContentType = new LinkedHashMap<>();
register(byContentType, defaultCodec);
for (MessageCodec codec : codecs) {
register(byContentType, codec);
}
return new RegisteredMessageCodecs(byContentType, defaultCodec);
}
private static void register(Map<ContentType, MessageCodec> into, MessageCodec codec) {
Objects.requireNonNull(codec, "codec");
MessageCodec existing = into.putIfAbsent(codec.contentType(), codec);
if (existing != null && existing != codec) {
// Two codecs for one content type is not a preference to resolve at runtime: whichever wins
// decides how bytes on the wire are read by a consumer that was compiled against the other.
throw new IllegalArgumentException(
"two codecs claim content type "
+ codec.contentType()
+ "; a deployment must choose one");
}
}
@Override
public Optional<MessageCodec> find(ContentType contentType) {
Objects.requireNonNull(contentType, "contentType");
return Optional.ofNullable(byContentType.get(contentType));
}
@Override
public MessageCodec defaultCodec() {
return defaultCodec;
}
}
@@ -0,0 +1,67 @@
package dev.caskeleton.messaging.runtime;
import dev.caskeleton.messaging.transport.MessagingRuntime;
import dev.caskeleton.messaging.transport.MessagingTransport;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* One transport, presented as the runtime the registry leases (MSG-INT-003).
*
* <p>{@link MessagingRuntime} had no implementation, so nothing could be installed into {@code
* DefaultMessagingRuntimeRegistry} and every publish failed at {@code PUBLISH_RUNTIME_UNAVAILABLE}
* — a registry constructed empty, which is the same shape the notification provider registry had.
*
* <p>The generation is the credential generation a rotation increments; the registry drains leases
* per generation, which is what lets a rotated credential retire without cutting live publishes.
*/
public final class TransportMessagingRuntime implements MessagingRuntime {
private final String brokerName;
private final long generation;
private final MessagingTransport transport;
private final AtomicBoolean closed = new AtomicBoolean();
/**
* Creates the runtime.
*
* @param brokerName the broker id destination profiles name
* @param generation the credential generation
* @param transport the transport publishes go through
*/
public TransportMessagingRuntime(
String brokerName, long generation, MessagingTransport transport) {
this.brokerName = Objects.requireNonNull(brokerName, "brokerName");
this.generation = generation;
this.transport = Objects.requireNonNull(transport, "transport");
}
@Override
public String brokerName() {
return brokerName;
}
@Override
public long generation() {
return generation;
}
@Override
public MessagingTransport transport() {
return transport;
}
@Override
public boolean isClosed() {
return closed.get();
}
@Override
public void close() {
// Idempotent: the registry closes a drained generation, and a context shutdown may close it
// again. Closing a transport twice is not an error worth propagating into shutdown.
if (closed.compareAndSet(false, true)) {
transport.close();
}
}
}
@@ -0,0 +1,88 @@
package dev.caskeleton.messaging.runtime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import dev.caskeleton.messaging.api.ContentType;
import dev.caskeleton.messaging.api.MessageType;
import dev.caskeleton.messaging.api.SchemaVersion;
import dev.caskeleton.messaging.schema.EncodedMessage;
import dev.caskeleton.messaging.schema.MessageCodec;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* The registry {@link DefaultMessagePublisher} resolves against (MSG-INT-003).
*
* <p>{@code MessageCodecRegistry} was an interface with no implementation anywhere — declared,
* consumed by the publisher's constructor, and satisfiable by nothing. That is why the platform
* could not assemble a publisher although every piece around it existed and was tested.
*/
class RegisteredMessageCodecsTest {
@Test
@DisplayName("the default codec is the one the deployment named")
void theDefaultCodecIsTheOneNamed() {
MessageCodec json = codec(ContentType.JSON);
MessageCodec avro = codec(ContentType.AVRO);
assertThat(RegisteredMessageCodecs.of(json, avro).defaultCodec())
.as(
"selecting by iteration order makes the wire format depend on how a map was populated — "
+ "a format decision taken by accident")
.isSameAs(json);
}
@Test
@DisplayName("a registered content type resolves, an unregistered one does not")
void aRegisteredContentTypeResolves() {
RegisteredMessageCodecs codecs =
RegisteredMessageCodecs.of(codec(ContentType.JSON), codec(ContentType.AVRO));
assertThat(codecs.find(ContentType.AVRO)).isPresent();
assertThat(codecs.find(ContentType.PROTOBUF))
.as("absent rather than falling back: a silent fallback re-encodes somebody's message")
.isEmpty();
}
@Test
@DisplayName("raw bytes cannot be the default")
void rawBytesCannotBeTheDefault() {
assertThatThrownBy(() -> RegisteredMessageCodecs.of(codec(ContentType.OCTET_STREAM)))
.as(
"a destination that forgot to declare an encoding would silently skip schema "
+ "validation, which turns a missing declaration into a skipped check")
.hasMessageContaining("raw bytes codec must not be the default");
}
@Test
@DisplayName("two codecs claiming one content type is a configuration error")
void twoCodecsForOneContentTypeIsRefused() {
assertThatThrownBy(
() -> RegisteredMessageCodecs.of(codec(ContentType.JSON), codec(ContentType.JSON)))
.as(
"whichever won would decide how bytes on the wire are read by a consumer compiled "
+ "against the other")
.hasMessageContaining("two codecs claim content type");
}
private static MessageCodec codec(ContentType contentType) {
return new MessageCodec() {
@Override
public ContentType contentType() {
return contentType;
}
@Override
public EncodedMessage encode(MessageType type, SchemaVersion version, Object payload) {
throw new UnsupportedOperationException("registry test; nothing is encoded");
}
@Override
public <T> T decode(
MessageType type, SchemaVersion version, byte[] bytes, Class<T> payloadType) {
throw new UnsupportedOperationException("registry test; nothing is decoded");
}
};
}
}