Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions java_socketio_chatroom_server/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/it/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
eclipse.preferences.version=1
encoding//src/it/java=UTF-8
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
156 changes: 136 additions & 20 deletions java_socketio_chatroom_server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<jacoco.plugin.version>0.8.7</jacoco.plugin.version>
<pitest.plugin.version>1.6.6</pitest.plugin.version>
<coveralls.plugin.version>4.3.0</coveralls.plugin.version>

<mongo.driver.version>3.12.8</mongo.driver.version>
<javax.version>4.0.1</javax.version>
<jetty-server.version>9.4.41.v20210516</jetty-server.version>
<jetty-servlet.version>9.4.41.v20210516</jetty-servlet.version>
Expand Down Expand Up @@ -59,6 +59,11 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongo.driver.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down Expand Up @@ -118,6 +123,10 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down Expand Up @@ -168,6 +177,18 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
Expand Down Expand Up @@ -210,10 +231,60 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<executions>
<execution>
<goals>
<!-- binds by default to the phase "initialize" -->
<goal>prepare-agent</goal>
<!-- binds by default to the phase "verify" -->
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<!-- binds by default to the phase "verify" -->
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>1</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.pitest</groupId>
Expand All @@ -232,6 +303,11 @@
<artifactId>coveralls-maven-plugin</artifactId>
<version>${coveralls.plugin.version}</version>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.36.0</version>
</plugin>
</plugins>
</pluginManagement>

Expand All @@ -240,18 +316,44 @@
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/ServerWrapper.*</exclude>
<exclude>**/ServerWrapper$*.*</exclude>
</excludes>
</configuration>
<artifactId>maven-failsafe-plugin</artifactId>
<!-- FIXME: pitest plugin is not able to connect to db when property
is set -->
<!-- <configuration> <systemPropertyVariables> <mongo.port>${mongo.mapped.port}</mongo.port>
</systemPropertyVariables> </configuration> -->
</plugin>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<name>mongo:4.2.14</name>
<run>
<ports>
<!-- FIXME: should be <port>mongo.mapped.port:27017</port> -->
<port>27017:27017</port>
</ports>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>docker-start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>docker-stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand All @@ -274,14 +376,20 @@
<!-- configured in pluginManagement -->
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/Message.*</exclude>
<exclude>**/ServerWrapper.*</exclude>
<exclude>**/ServerWrapper$*.*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
Expand All @@ -294,9 +402,17 @@
<!-- configured also in pluginManagement -->
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<configuration>
<excludedClasses>
<param>io.github.marcodiri.java_socketio_chatroom_server.model.*</param>
</excludedClasses>
<targetTests>
<param>io.github.marcodiri.java_socketio_chatroom_server.*</param>
</targetTests>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<phase>integration-test</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.github.marcodiri.java_socketio_chatroom_server.repository.mongo;

import static io.github.marcodiri.java_socketio_chatroom_server.repository.mongo.ServerMongoRepository.CHATROOM_DB_NAME;
import static io.github.marcodiri.java_socketio_chatroom_server.repository.mongo.ServerMongoRepository.MESSAGES_COLLECTION_NAME;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import org.bson.Document;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import io.github.marcodiri.java_socketio_chatroom_server.model.Message;

public class ServerMongoRepositoryIT {
private static int mongoPort = Integer.parseInt(System.getProperty("mongo.port", "27017"));
private MongoClient client;
private ServerMongoRepository serverRepository;
private MongoCollection<Document> messagesCollection;

@Before
public void setup() {
client = new MongoClient(
new ServerAddress("localhost", mongoPort)
);
serverRepository = new ServerMongoRepository(client);
MongoDatabase database = client.getDatabase(CHATROOM_DB_NAME);
database.drop();
messagesCollection = database.getCollection(MESSAGES_COLLECTION_NAME);
}

@Test
public void testFindAll() {
Timestamp ts1 = new Timestamp(System.currentTimeMillis());
Timestamp ts2 = new Timestamp(System.currentTimeMillis());

messagesCollection.insertMany(asList(
new Document()
.append("timestamp", ts1.getTime())
.append("user", "user1")
.append("message", "message1"),
new Document()
.append("timestamp", ts2.getTime())
.append("user", "user2")
.append("message", "message2")
));

assertThat(serverRepository.findAll())
.containsExactly(
new Message(ts1, "user1", "message1"),
new Message(ts2, "user2", "message2")
);
}

@Test
public void testSave() {
Message msg = new Message(new Timestamp(System.currentTimeMillis()), "user", "message");
serverRepository.save(msg);
assertThat(readAllMessages()).containsExactly(msg);
}

private List<Message> readAllMessages() {
return StreamSupport.stream(messagesCollection.find().spliterator(), false)
.map(d -> new Message(
new Timestamp(Long.parseLong(d.get("timestamp").toString())),
"" + d.get("user"),
"" + d.get("message")))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.github.marcodiri.java_socketio_chatroom_server.model;

import java.sql.Timestamp;
import java.util.Objects;

public final class Message {
private final Timestamp timestamp;
private final String user;
private final String message;

public Message(Timestamp timestamp, String user, String message) {
this.timestamp = new Timestamp(timestamp.getTime());
this.user = user;
this.message = message;
}

public Timestamp getTimestamp() {
return new Timestamp(timestamp.getTime());
}

public String getUser() {
return user;
}

public String getMessage() {
return message;
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Message{");
sb.append("timestamp=").append(timestamp);
sb.append(", user=").append(user);
sb.append(", message=").append(message);
sb.append('}');
return sb.toString();
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Message msg = (Message) o;
return Objects.equals(timestamp, msg.timestamp) && Objects.equals(user, msg.user) && Objects.equals(message, msg.message);
}

@Override
public int hashCode() {
return Objects.hash(timestamp, user, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.marcodiri.java_socketio_chatroom_server.repository;

import java.util.List;

import io.github.marcodiri.java_socketio_chatroom_server.model.Message;

public interface ServerRepository {
public List<Message> findAll();

public void save(Message message);
}
Loading