Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
refactor: add factory methods for consumers/producers by annotations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kayman-mk authored Jun 12, 2022
1 parent 103a73b commit 5949559
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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"));
Expand All @@ -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);
}
}

0 comments on commit 5949559

Please sign in to comment.