From 5949559bab4fd8ea1b651c444e1d89936396787b Mon Sep 17 00:00:00 2001 From: Matthias Kay Date: Sun, 12 Jun 2022 16:45:14 +0200 Subject: [PATCH] refactor: add factory methods for consumers/producers by annotations (#40) --- .../analyzer/model/EndpointFactory.java | 20 ++++- .../analyzer/model/EndpointFactoryTest.java | 84 ++++++++++++++++--- 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/hlag/tools/commvis/analyzer/model/EndpointFactory.java b/src/main/java/com/hlag/tools/commvis/analyzer/model/EndpointFactory.java index 3d68bb5..ab3e066 100644 --- a/src/main/java/com/hlag/tools/commvis/analyzer/model/EndpointFactory.java +++ b/src/main/java/com/hlag/tools/commvis/analyzer/model/EndpointFactory.java @@ -1,9 +1,6 @@ package com.hlag.tools.commvis.analyzer.model; -import com.hlag.tools.commvis.analyzer.annotation.VisualizeKafkaConsumer; -import com.hlag.tools.commvis.analyzer.annotation.VisualizeKafkaProducer; -import com.hlag.tools.commvis.analyzer.annotation.VisualizeSnsProducer; -import com.hlag.tools.commvis.analyzer.annotation.VisualizeSqsViaSnsConsumer; +import com.hlag.tools.commvis.analyzer.annotation.*; import com.hlag.tools.commvis.analyzer.port.IIdentityGenerator; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -19,26 +16,41 @@ public HttpConsumer createHttpConsumer(String className, String methodName, Stri return new HttpConsumer(className, methodName, type, path, identityGenerator.generateUniqueId()); } + @Deprecated public HttpProducer createHttpProducer(String className, String methodName, String type, String path, String destinationProjectId) { return new HttpProducer(className, methodName, type, path, destinationProjectId, identityGenerator.generateUniqueId()); } + public HttpProducer createHttpProducer(VisualizeHttpsCall annotation, Method method) { + return new HttpProducer(method.getDeclaringClass().getCanonicalName(), method.getName(), annotation.type(), annotation.path(), annotation.projectId(), identityGenerator.generateUniqueId()); + } + public JmsReceiver createJmsReceiver(String className, String destinationType, String destination) { return new JmsReceiver(className, destinationType, destination, identityGenerator.generateUniqueId()); } + @Deprecated public SqsConsumer createSqsReceiver(String className, String methodName, String queueName) { return new SqsConsumer(className, methodName, queueName, identityGenerator.generateUniqueId()); } + public SqsConsumer createSqsConsumer(VisualizeSqsConsumer annotation, Method method) { + return new SqsConsumer(method.getDeclaringClass().getCanonicalName(), method.getName(), annotation.queueName(), identityGenerator.generateUniqueId()); + } + public SqsViaSnsConsumer createSqsViaSnsConsumer(VisualizeSqsViaSnsConsumer annotation, Method method) { return new SqsViaSnsConsumer(method.getDeclaringClass().getCanonicalName(), method.getName(), annotation.topicName(), identityGenerator.generateUniqueId()); } + @Deprecated public SqsProducer createSqsProducer(String className, String methodName, String queueName, String destinationProjectId) { return new SqsProducer(className, methodName, queueName, destinationProjectId, identityGenerator.generateUniqueId()); } + public SqsProducer createSqsProducer(VisualizeSqsProducer annotation, Method method) { + return new SqsProducer(method.getDeclaringClass().getCanonicalName(), method.getName(), annotation.queueName(), annotation.projectId(), identityGenerator.generateUniqueId()); + } + public SnsProducer createSnsProducer(VisualizeSnsProducer annotation, Method method) { return new SnsProducer(method.getDeclaringClass().getCanonicalName(), method.getName(), annotation.topicName(), annotation.projectId(), identityGenerator.generateUniqueId()); } diff --git a/src/test/java/com/hlag/tools/commvis/analyzer/model/EndpointFactoryTest.java b/src/test/java/com/hlag/tools/commvis/analyzer/model/EndpointFactoryTest.java index 366857b..b73d1f9 100644 --- a/src/test/java/com/hlag/tools/commvis/analyzer/model/EndpointFactoryTest.java +++ b/src/test/java/com/hlag/tools/commvis/analyzer/model/EndpointFactoryTest.java @@ -1,7 +1,6 @@ package com.hlag.tools.commvis.analyzer.model; -import com.hlag.tools.commvis.analyzer.annotation.VisualizeSnsProducer; -import com.hlag.tools.commvis.analyzer.annotation.VisualizeSqsViaSnsConsumer; +import com.hlag.tools.commvis.analyzer.annotation.*; import com.hlag.tools.commvis.analyzer.port.IIdentityGenerator; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -20,6 +19,21 @@ public void produceSnsMessage() {} @VisualizeSqsViaSnsConsumer(topicName = "topic1", projectName = "4712") public void consumeSqsViaSnsMessage() {} + + @VisualizeKafkaConsumer(topicName = "topic2", projectName = "4713") + public void consumeKafkaMessage() {} + + @VisualizeKafkaProducer(topicName = "topic3", projectId = "4714") + public void produceKafkaMessage() {} + + @VisualizeSqsProducer(queueName = "queue4", projectId = "4715") + public void produceSqsMessage() {} + + @VisualizeSqsConsumer(queueName = "queue5", projectName = "4716") + public void consumeSqsMessage() {} + + @VisualizeHttpsCall(type = "GET", path = "a/b/c", projectId = "4717") + public void produceHttpsMessage() {} } @Mock @@ -78,17 +92,6 @@ void shouldSetAllFields_whenCreateSqsReceiver() { Assertions.assertThat(actualSqsConsumer.getId()).isEqualTo(FIXED_ID); } - @Test - void shouldSetAllFields_whenCreateSqsProducer() { - SqsProducer actualSqsProducer = factory.createSqsProducer("className", "methodName", "queueName", "destinationProjectId"); - - Assertions.assertThat(actualSqsProducer.getClassName()).isEqualTo("className"); - Assertions.assertThat(actualSqsProducer.getMethodName()).isEqualTo("methodName"); - Assertions.assertThat(actualSqsProducer.getQueueName()).isEqualTo("queueName"); - Assertions.assertThat(actualSqsProducer.getDestinationProjectId()).isEqualTo("destinationProjectId"); - Assertions.assertThat(actualSqsProducer.getId()).isEqualTo(FIXED_ID); - } - @Test void shouldSetAllFields_whenCreateSnsProducer() throws NoSuchMethodException { SnsProducer actualSnsProducer = factory.createSnsProducer(TestProducersAndConsumers.class.getDeclaredMethod("produceSnsMessage").getAnnotationsByType(VisualizeSnsProducer.class)[0], TestProducersAndConsumers.class.getDeclaredMethod("produceSnsMessage")); @@ -109,4 +112,59 @@ void shouldSetAllFields_whenCreateSqsViaSnsConsumer() throws NoSuchMethodExcepti Assertions.assertThat(actualSqsViaSnsConsumer.getTopicName()).isEqualTo("topic1"); Assertions.assertThat(actualSqsViaSnsConsumer.getId()).isEqualTo(FIXED_ID); } + + @Test + void shouldSetAllFields_whenCreateKafkaConsumer() throws NoSuchMethodException { + KafkaConsumer actualKafkaConsumer = factory.createKafkaConsumer(TestProducersAndConsumers.class.getDeclaredMethod("consumeKafkaMessage").getAnnotationsByType(VisualizeKafkaConsumer.class)[0], TestProducersAndConsumers.class.getDeclaredMethod("consumeKafkaMessage")); + + Assertions.assertThat(actualKafkaConsumer.getClassName()).isEqualTo("com.hlag.tools.commvis.analyzer.model.EndpointFactoryTest.TestProducersAndConsumers"); + Assertions.assertThat(actualKafkaConsumer.getMethodName()).isEqualTo("consumeKafkaMessage"); + Assertions.assertThat(actualKafkaConsumer.getTopicName()).isEqualTo("topic2"); + Assertions.assertThat(actualKafkaConsumer.getId()).isEqualTo(FIXED_ID); + } + + @Test + void shouldSetAllFields_whenCreateKafkaProducer() throws NoSuchMethodException { + KafkaProducer actualKafkaProducer = factory.createKafkaProducer(TestProducersAndConsumers.class.getDeclaredMethod("produceKafkaMessage").getAnnotationsByType(VisualizeKafkaProducer.class)[0], TestProducersAndConsumers.class.getDeclaredMethod("produceKafkaMessage")); + + Assertions.assertThat(actualKafkaProducer.getClassName()).isEqualTo("com.hlag.tools.commvis.analyzer.model.EndpointFactoryTest.TestProducersAndConsumers"); + Assertions.assertThat(actualKafkaProducer.getMethodName()).isEqualTo("produceKafkaMessage"); + Assertions.assertThat(actualKafkaProducer.getTopicName()).isEqualTo("topic3"); + Assertions.assertThat(actualKafkaProducer.getDestinationProjectId()).isEqualTo("4714"); + Assertions.assertThat(actualKafkaProducer.getId()).isEqualTo(FIXED_ID); + } + + + @Test + void shouldSetAllFields_whenCreateSqsProducer() throws NoSuchMethodException { + SqsProducer actualSqsProducer = factory.createSqsProducer(TestProducersAndConsumers.class.getDeclaredMethod("produceSqsMessage").getAnnotationsByType(VisualizeSqsProducer.class)[0], TestProducersAndConsumers.class.getDeclaredMethod("produceSqsMessage")); + + Assertions.assertThat(actualSqsProducer.getClassName()).isEqualTo("com.hlag.tools.commvis.analyzer.model.EndpointFactoryTest.TestProducersAndConsumers"); + Assertions.assertThat(actualSqsProducer.getMethodName()).isEqualTo("produceSqsMessage"); + Assertions.assertThat(actualSqsProducer.getQueueName()).isEqualTo("queue4"); + Assertions.assertThat(actualSqsProducer.getDestinationProjectId()).isEqualTo("4715"); + Assertions.assertThat(actualSqsProducer.getId()).isEqualTo(FIXED_ID); + } + + @Test + void shouldSetAllFields_whenCreateSqsConsumer() throws NoSuchMethodException { + SqsConsumer actualSqsConsumer = factory.createSqsConsumer(TestProducersAndConsumers.class.getDeclaredMethod("consumeSqsMessage").getAnnotationsByType(VisualizeSqsConsumer.class)[0], TestProducersAndConsumers.class.getDeclaredMethod("consumeSqsMessage")); + + Assertions.assertThat(actualSqsConsumer.getClassName()).isEqualTo("com.hlag.tools.commvis.analyzer.model.EndpointFactoryTest.TestProducersAndConsumers"); + Assertions.assertThat(actualSqsConsumer.getMethodName()).isEqualTo("consumeSqsMessage"); + Assertions.assertThat(actualSqsConsumer.getQueueName()).isEqualTo("queue5"); + Assertions.assertThat(actualSqsConsumer.getId()).isEqualTo(FIXED_ID); + } + + @Test + void shouldSetAllFields_whenCreateHttpProducer_givenAnnotationAndMethod() throws NoSuchMethodException { + HttpProducer actualHttpProducer = factory.createHttpProducer(TestProducersAndConsumers.class.getDeclaredMethod("produceHttpsMessage").getAnnotationsByType(VisualizeHttpsCall.class)[0], TestProducersAndConsumers.class.getDeclaredMethod("produceHttpsMessage")); + + Assertions.assertThat(actualHttpProducer.getClassName()).isEqualTo("com.hlag.tools.commvis.analyzer.model.EndpointFactoryTest.TestProducersAndConsumers"); + Assertions.assertThat(actualHttpProducer.getMethodName()).isEqualTo("produceHttpsMessage"); + Assertions.assertThat(actualHttpProducer.getType()).isEqualTo("GET"); + Assertions.assertThat(actualHttpProducer.getPath()).isEqualTo("a/b/c"); + Assertions.assertThat(actualHttpProducer.getDestinationProjectId()).isEqualTo("4717"); + Assertions.assertThat(actualHttpProducer.getId()).isEqualTo(FIXED_ID); + } } \ No newline at end of file