diff --git a/pom.xml b/pom.xml index 3585679..e6af937 100644 --- a/pom.xml +++ b/pom.xml @@ -162,17 +162,16 @@ - - com.google.code.gson - gson - 2.9.0 - - org.junit.jupiter junit-jupiter test + + org.mockito + mockito-core + 4.4.0 + org.assertj assertj-core @@ -180,6 +179,18 @@ test + + com.google.code.gson + gson + 2.9.0 + provided + + + org.springframework + spring-context + 5.3.19 + provided + javax.ws.rs javax.ws.rs-api diff --git a/src/main/java/com/hlag/tools/commvis/analyzer/adapter/IdentityGenerator.java b/src/main/java/com/hlag/tools/commvis/analyzer/adapter/IdentityGenerator.java new file mode 100644 index 0000000..ce2d8d7 --- /dev/null +++ b/src/main/java/com/hlag/tools/commvis/analyzer/adapter/IdentityGenerator.java @@ -0,0 +1,18 @@ +package com.hlag.tools.commvis.analyzer.adapter; + +import com.hlag.tools.commvis.analyzer.port.IIdentityGenerator; +import org.springframework.stereotype.Component; + +import java.util.UUID; + +/** + * Simple class which generates unique ids. As it does not belong to the core competency of the application it has + * been moved to the adapter package to improve the testability. + */ +@Component +public class IdentityGenerator implements IIdentityGenerator { + @Override + public String generateUniqueId() { + return UUID.randomUUID().toString(); + } +} 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 2f4cdca..4d43bd9 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,21 +1,23 @@ package com.hlag.tools.commvis.analyzer.model; -public class EndpointFactory { - private static final EndpointFactory singleton = new EndpointFactory(); +import com.hlag.tools.commvis.analyzer.port.IIdentityGenerator; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; - public static EndpointFactory get() { - return singleton; - } +@Component +@RequiredArgsConstructor +public class EndpointFactory { + private final IIdentityGenerator identityGenerator; public HttpConsumer createHttpConsumer(String className, String methodName, String type, String path) { - return new HttpConsumer(className, methodName, type, path); + return new HttpConsumer(className, methodName, type, path, identityGenerator.generateUniqueId()); } public HttpProducer createHttpProducer(String className, String methodName, String type, String path, String destinationProjectId) { - return new HttpProducer(className, methodName, type, path, destinationProjectId); + return new HttpProducer(className, methodName, type, path, destinationProjectId, identityGenerator.generateUniqueId()); } public JmsReceiver createJmsReceiver(String className, String destinationType, String destination) { - return new JmsReceiver(className, destinationType, destination); + return new JmsReceiver(className, destinationType, destination, identityGenerator.generateUniqueId()); } } diff --git a/src/main/java/com/hlag/tools/commvis/analyzer/model/HttpConsumer.java b/src/main/java/com/hlag/tools/commvis/analyzer/model/HttpConsumer.java index f3ce046..b6c1035 100644 --- a/src/main/java/com/hlag/tools/commvis/analyzer/model/HttpConsumer.java +++ b/src/main/java/com/hlag/tools/commvis/analyzer/model/HttpConsumer.java @@ -22,7 +22,7 @@ public class HttpConsumer implements ISenderReceiverCommunication { String path; @SerializedName(value="id") - UUID id = UUID.randomUUID(); + String id; @Override public void visit(AbstractCommunicationModelVisitor visitor) { diff --git a/src/main/java/com/hlag/tools/commvis/analyzer/model/HttpProducer.java b/src/main/java/com/hlag/tools/commvis/analyzer/model/HttpProducer.java index 3ca5a35..80c5188 100644 --- a/src/main/java/com/hlag/tools/commvis/analyzer/model/HttpProducer.java +++ b/src/main/java/com/hlag/tools/commvis/analyzer/model/HttpProducer.java @@ -42,7 +42,7 @@ public class HttpProducer implements ISenderReceiverCommunication { String destinationProjectId; @SerializedName(value="id") - UUID id = UUID.randomUUID(); + String id; @Override public void visit(AbstractCommunicationModelVisitor visitor) { diff --git a/src/main/java/com/hlag/tools/commvis/analyzer/model/ISenderReceiverCommunication.java b/src/main/java/com/hlag/tools/commvis/analyzer/model/ISenderReceiverCommunication.java index a52d1d0..81db6e2 100644 --- a/src/main/java/com/hlag/tools/commvis/analyzer/model/ISenderReceiverCommunication.java +++ b/src/main/java/com/hlag/tools/commvis/analyzer/model/ISenderReceiverCommunication.java @@ -9,7 +9,7 @@ public interface ISenderReceiverCommunication { /** * @return an identifier for the object. Globally unique. */ - UUID getId(); + String getId(); void visit(AbstractCommunicationModelVisitor visitor); } \ No newline at end of file diff --git a/src/main/java/com/hlag/tools/commvis/analyzer/model/JmsReceiver.java b/src/main/java/com/hlag/tools/commvis/analyzer/model/JmsReceiver.java index 3b1662d..59419d8 100644 --- a/src/main/java/com/hlag/tools/commvis/analyzer/model/JmsReceiver.java +++ b/src/main/java/com/hlag/tools/commvis/analyzer/model/JmsReceiver.java @@ -23,7 +23,7 @@ public class JmsReceiver implements ISenderReceiverCommunication { String destination; @SerializedName(value="id") - UUID id = UUID.randomUUID(); + String id; @Override public void visit(AbstractCommunicationModelVisitor visitor) { diff --git a/src/main/java/com/hlag/tools/commvis/analyzer/port/IIdentityGenerator.java b/src/main/java/com/hlag/tools/commvis/analyzer/port/IIdentityGenerator.java new file mode 100644 index 0000000..b170617 --- /dev/null +++ b/src/main/java/com/hlag/tools/commvis/analyzer/port/IIdentityGenerator.java @@ -0,0 +1,5 @@ +package com.hlag.tools.commvis.analyzer.port; + +public interface IIdentityGenerator { + String generateUniqueId(); +} diff --git a/src/test/java/com/hlag/tools/commvis/analyzer/adapter/IdentityGeneratorTest.java b/src/test/java/com/hlag/tools/commvis/analyzer/adapter/IdentityGeneratorTest.java new file mode 100644 index 0000000..fc9a815 --- /dev/null +++ b/src/test/java/com/hlag/tools/commvis/analyzer/adapter/IdentityGeneratorTest.java @@ -0,0 +1,28 @@ +package com.hlag.tools.commvis.analyzer.adapter; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +class IdentityGeneratorTest { + private IdentityGenerator identityGenerator; + + @BeforeEach + void init() { + identityGenerator = new IdentityGenerator(); + } + + @Test + void shouldGenerateUniqueIds_whenGenerateUniqueId() { + List actualUniqueIds = new ArrayList<>(); + + for (int i = 0; i < 50; i++) { + actualUniqueIds.add(identityGenerator.generateUniqueId()); + } + + Assertions.assertThat(actualUniqueIds).doesNotHaveDuplicates(); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..3352d06 --- /dev/null +++ b/src/test/java/com/hlag/tools/commvis/analyzer/model/EndpointFactoryTest.java @@ -0,0 +1,60 @@ +package com.hlag.tools.commvis.analyzer.model; + +import com.hlag.tools.commvis.analyzer.port.IIdentityGenerator; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +class EndpointFactoryTest { + private static final String FIXED_ID = "MY-UNIQUE-ID"; + + @Mock + private IIdentityGenerator identityGenerator; + @InjectMocks + private EndpointFactory factory; + + @BeforeEach + void init() { + MockitoAnnotations.openMocks(this); + + Mockito.doReturn(FIXED_ID).when(identityGenerator).generateUniqueId(); + } + + @Test + void shouldSetAllFields_whenCreateHttpConsumer() { + HttpConsumer actualHttpConsumer = factory.createHttpConsumer("className", "methodName", "type", "path"); + + Assertions.assertThat(actualHttpConsumer.getClassName()).isEqualTo("className"); + Assertions.assertThat(actualHttpConsumer.getMethodName()).isEqualTo("methodName"); + Assertions.assertThat(actualHttpConsumer.getType()).isEqualTo("type"); + Assertions.assertThat(actualHttpConsumer.getPath()).isEqualTo("path"); + Assertions.assertThat(actualHttpConsumer.getId()).isEqualTo(FIXED_ID); + } + + @Test + void shouldSetAllFields_whenCreateHttpProducer() { + HttpProducer actualHttpProducer = factory.createHttpProducer("className", "methodName", "type", "path", "destinationProjectId"); + + Assertions.assertThat(actualHttpProducer.getClassName()).isEqualTo("className"); + Assertions.assertThat(actualHttpProducer.getMethodName()).isEqualTo("methodName"); + Assertions.assertThat(actualHttpProducer.getType()).isEqualTo("type"); + Assertions.assertThat(actualHttpProducer.getPath()).isEqualTo("path"); + Assertions.assertThat(actualHttpProducer.getDestinationProjectId()).isEqualTo("destinationProjectId"); + Assertions.assertThat(actualHttpProducer.getId()).isEqualTo(FIXED_ID); + + } + + @Test + void createJmsReceiver() { + JmsReceiver actualJmsReceiver = factory.createJmsReceiver("className", "destinationType", "destination"); + + Assertions.assertThat(actualJmsReceiver.getClassName()).isEqualTo("className"); + Assertions.assertThat(actualJmsReceiver.getDestinationType()).isEqualTo("destinationType"); + Assertions.assertThat(actualJmsReceiver.getDestination()).isEqualTo("destination"); + Assertions.assertThat(actualJmsReceiver.getId()).isEqualTo(FIXED_ID); + } +} \ No newline at end of file