Skip to content

Commit

Permalink
feat: add interfaces for consumers and producers
Browse files Browse the repository at this point in the history
  • Loading branch information
kayman-mk committed Jun 9, 2022
1 parent 39c42a3 commit 9a37e2b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 12 deletions.
Expand Up @@ -10,7 +10,7 @@
*/
@Value
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class HttpConsumer implements ISenderReceiverCommunication {
public class HttpConsumer implements ISenderReceiverCommunication, IConsumer {
@SerializedName(value="class_name")
String className;
@SerializedName(value="method_name")
Expand All @@ -24,13 +24,15 @@ public class HttpConsumer implements ISenderReceiverCommunication {
@SerializedName(value="id")
String id;

/**
*
* @param producer the producer tested
* @return {@code true} if this consumer is receives messages from the {@code producer}.
*/
public boolean isProducedBy(HttpProducer producer) {
return type.equals(producer.getType()) && path.equals(producer.getPath());
@Override
public boolean isProducedBy(IProducer producer) {
if (producer instanceof HttpProducer) {
HttpProducer httpProducer = (HttpProducer) producer;

return type.equals(httpProducer.getType()) && path.equals(httpProducer.getPath());
}

return false;
}

@Override
Expand Down
Expand Up @@ -10,7 +10,7 @@
*/
@Value
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class HttpProducer implements ISenderReceiverCommunication {
public class HttpProducer implements ISenderReceiverCommunication, IProducer {
/**
* The full qualified classname where the producer lives.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/hlag/tools/commvis/analyzer/model/IConsumer.java
@@ -0,0 +1,13 @@
package com.hlag.tools.commvis.analyzer.model;

/**
* Indentifies an {@link ISenderReceiverCommunication} as a consumer of something.
*/
public interface IConsumer {
/**
*
* @param producer the producer tested
* @return {@code true} if this consumer receives messages from the {@code producer}.
*/
boolean isProducedBy(IProducer producer);
}
@@ -0,0 +1,8 @@
package com.hlag.tools.commvis.analyzer.model;

/**
* Indentifies an {@link ISenderReceiverCommunication} as a producer of something.
*/
public interface IProducer {
String getDestinationProjectId();
}
Expand Up @@ -10,7 +10,7 @@
*/
@Value
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class JmsReceiver implements ISenderReceiverCommunication {
public class JmsReceiver implements ISenderReceiverCommunication, IConsumer {
@SerializedName(value="class_name")
String className;

Expand All @@ -29,4 +29,10 @@ public class JmsReceiver implements ISenderReceiverCommunication {
public void visit(AbstractCommunicationModelVisitor visitor) {
visitor.visit(this);
}

@Override
public boolean isProducedBy(IProducer producer) {
// we have no producers so far
return false;
}
}
Expand Up @@ -10,7 +10,7 @@
*/
@Value
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class SqsConsumer implements ISenderReceiverCommunication {
public class SqsConsumer implements ISenderReceiverCommunication, IConsumer {
@SerializedName(value="class_name")
String className;

Expand All @@ -27,4 +27,15 @@ public class SqsConsumer implements ISenderReceiverCommunication {
public void visit(AbstractCommunicationModelVisitor visitor) {
visitor.visit(this);
}

@Override
public boolean isProducedBy(IProducer producer) {
if (producer instanceof SqsProducer) {
SqsProducer sqsProducer = (SqsProducer) producer;

return queueName.equals(sqsProducer.getQueueName());
}

return false;
}
}
Expand Up @@ -10,7 +10,7 @@
*/
@Value
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class SqsProducer implements ISenderReceiverCommunication {
public class SqsProducer implements ISenderReceiverCommunication, IProducer {
/**
* the class name where the producer was found.
*/
Expand Down
@@ -1,13 +1,20 @@
package com.hlag.tools.commvis.analyzer.model;

import com.google.gson.annotations.SerializedName;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;

import static org.assertj.core.api.Assertions.assertThat;

class SqsConsumerTest {
private SqsConsumer sqsConsumer;

@BeforeEach
void init() {
sqsConsumer = new SqsConsumer("class", "method", "queue", "id");
}
@Test
void shouldHaveSerializedNameAnnotationOnFiled_toDecoupleTheFieldNameFromJson() {
Field[] declaredFields = SqsConsumer.class.getDeclaredFields();
Expand All @@ -18,4 +25,22 @@ void shouldHaveSerializedNameAnnotationOnFiled_toDecoupleTheFieldNameFromJson()
assertThat(actualAnnotation).withFailMessage(() -> String.format("Field %s has no @SerializedName annotation.", f.getName())).isNotNull();
}
}

@Test
void shouldReturnTrue_whenIsProducedBy_givenProducerIsForSameQueue() {
SqsProducer givenProducer = new SqsProducer("class1", "method1", "queue", "destinationProject", "id1");

boolean actualIsProducedBy = sqsConsumer.isProducedBy(givenProducer);

assertThat(actualIsProducedBy).isTrue();
}

@Test
void shouldReturnFalse_whenIsProducedBy_givenProducerIsForOtherQueue() {
SqsProducer givenProducer = new SqsProducer("class1", "method1", "queue-other", "destinationProject", "id1");

boolean actualIsProducedBy = sqsConsumer.isProducedBy(givenProducer);

assertThat(actualIsProducedBy).isFalse();
}
}

0 comments on commit 9a37e2b

Please sign in to comment.