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

Commit

Permalink
feat: add IdentityGenerator (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
kayman-mk authored May 19, 2022
1 parent a681680 commit 6ea8b67
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 18 deletions.
23 changes: 17 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,35 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.19</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ISenderReceiverCommunication {
/**
* @return an identifier for the object. Globally unique.
*/
UUID getId();
String getId();

void visit(AbstractCommunicationModelVisitor visitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.hlag.tools.commvis.analyzer.port;

public interface IIdentityGenerator {
String generateUniqueId();
}
Original file line number Diff line number Diff line change
@@ -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<String> actualUniqueIds = new ArrayList<>();

for (int i = 0; i < 50; i++) {
actualUniqueIds.add(identityGenerator.generateUniqueId());
}

Assertions.assertThat(actualUniqueIds).doesNotHaveDuplicates();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 6ea8b67

Please sign in to comment.