Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oap-mail: mongo persistence #358

Merged
merged 2 commits into from
Jan 17, 2025
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
31 changes: 31 additions & 0 deletions oap-mail/oap-mail-mongo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>oap-mail-parent</artifactId>
<groupId>oap</groupId>
<version>${oap.project.version}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>oap-mail-mongo</artifactId>

<dependencies>
<dependency>
<groupId>oap</groupId>
<artifactId>oap-mail</artifactId>
<version>${oap.project.version}</version>
</dependency>
<dependency>
<groupId>oap</groupId>
<artifactId>oap-storage-mongo</artifactId>
<version>${oap.project.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package oap.mail.mongo;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import oap.mail.MailQueuePersistence;
import oap.mail.Message;
import oap.reflect.TypeRef;
import oap.storage.mongo.JsonCodec;
import oap.storage.mongo.MongoClient;
import oap.util.Cuid;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;

import java.util.Iterator;

public class MailQueuePersistenceMongo implements MailQueuePersistence {
private final MongoCollection<MessageData> collection;

public MailQueuePersistenceMongo( MongoClient mongoClient, String collectionName ) {
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs( new JsonCodec<>( new TypeRef<MessageData>() {}, md -> md.id, id -> id ) ),
mongoClient.getCodecRegistry()
);
this.collection = mongoClient
.getCollection( collectionName, MessageData.class )
.withCodecRegistry( codecRegistry );
}

@Override
public void add( Message message ) {
collection.insertOne( new MessageData( Cuid.UNIQUE.next(), message ) );
}

@Override
public int size() {
return ( int ) collection.countDocuments();
}

@Override
public Iterator<Message> iterator() {
MongoCursor<MessageData> iterator = collection.find().iterator();
return new Iterator<>() {
MessageData messageData;

@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public Message next() {
messageData = iterator.next();
return messageData.message;
}

@Override
public void remove() {
collection.deleteOne( Filters.eq( "_id", messageData.id ) );
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package oap.mail.mongo;

import oap.mail.Message;

public class MessageData {
public final Message message;
public String id;

public MessageData( String id, Message message ) {
this.id = id;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name = oap-mail-mongo

dependsOn = [
oap-storage-mongo
]

services {
mail-queue-persistence-mongo {
implementation = oap.mail.mongo.MailQueuePersistenceMongo
parameters {
mongoClient = <modules.oap-storage-mongo.mongo-client>
collectionName = mails
}
}
}
10 changes: 10 additions & 0 deletions oap-mail/oap-mail-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,22 @@
<artifactId>oap-mail</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>oap</groupId>
<artifactId>oap-mail-mongo</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>oap</groupId>
<artifactId>oap-stdlib-test</artifactId>
<version>${oap.project.version}</version>
</dependency>
<dependency>
<groupId>oap</groupId>
<artifactId>oap-storage-mongo-test</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
14 changes: 4 additions & 10 deletions oap-mail/oap-mail-test/src/test/java/oap/mail/MailQueueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,24 @@ public MailQueueTest() {

@Test
public void persist() {
var location = testDirectoryFixture.testPath( "queue" );
Path location = testDirectoryFixture.testPath( "queue" );
MailQueue queue = prepareQueue( location );
queue.processing( reject() );
assertThat( location.resolve( "mail.gz" ) ).exists();
var queue2 = new MailQueue( location );
MailQueue queue2 = new MailQueue( new MailQueuePersistenceFile( location ) );
assertMessages( queue2.messages() )
.hasSize( 2 )
.bySubject( "subj1", MessageAssertion::assertMessage )
.bySubject( "subj2", MessageAssertion::assertMessage );

queue2.processing( accept() );
assertMessages( queue2.messages() ).isEmpty();
var queue3 = new MailQueue( location );
MailQueue queue3 = new MailQueue( new MailQueuePersistenceFile( location ) );
assertMessages( queue3.messages() ).isEmpty();
}

@Test
public void persistWithNullLocation() {
MailQueue queue = prepareQueue( null );
queue.processing( reject() );
}

private MailQueue prepareQueue( Path location ) {
var queue = new MailQueue( location );
MailQueue queue = new MailQueue( new MailQueuePersistenceFile( location ) );
queue.add( new Message( "subj1", "body", Lists.empty() ) );
queue.add( new Message( "subj2", "body", Lists.empty() ) );
Message message = new Message( "subj3", "body", Lists.empty() );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* The MIT License (MIT)
*
* Copyright (c) Open Application Platform Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package oap.mail.mongo;

import oap.mail.MailQueue;
import oap.mail.Message;
import oap.mail.test.MessageAssertion;
import oap.storage.mongo.MongoFixture;
import oap.testng.Fixtures;
import oap.util.Dates;
import oap.util.Lists;
import org.joda.time.DateTime;
import org.testng.annotations.Test;

import static oap.mail.test.MessagesAssertion.assertMessages;
import static oap.util.function.Functions.empty.accept;
import static oap.util.function.Functions.empty.reject;
import static org.assertj.core.api.Assertions.assertThat;

public class MailQueueMongoTest extends Fixtures {
private final MongoFixture mongoFixture;

public MailQueueMongoTest() {
mongoFixture = fixture( new MongoFixture() );
}

@Test
public void persist() {
MailQueue queue = prepareQueue();
queue.processing( reject() );
assertThat( mongoFixture.client().getCollection( "mails" ).countDocuments() ).isEqualTo( 2 );
MailQueue queue2 = new MailQueue( new MailQueuePersistenceMongo( mongoFixture.client(), "mails" ) );
assertMessages( queue2.messages() )
.hasSize( 2 )
.bySubject( "subj1", MessageAssertion::assertMessage )
.bySubject( "subj2", MessageAssertion::assertMessage );

queue2.processing( accept() );
assertMessages( queue2.messages() ).isEmpty();
MailQueue queue3 = new MailQueue( new MailQueuePersistenceMongo( mongoFixture.client(), "mails" ) );
assertMessages( queue3.messages() ).isEmpty();
}

private MailQueue prepareQueue() {
MailQueue queue = new MailQueue( new MailQueuePersistenceMongo( mongoFixture.client(), "mails" ) );
queue.add( new Message( "subj1", "body", Lists.empty() ) );
queue.add( new Message( "subj2", "body", Lists.empty() ) );
Message message = new Message( "subj3", "body", Lists.empty() );
message.created = DateTime.now().minus( Dates.w( 3 ) );
queue.add( message );
return queue;
}
}
12 changes: 9 additions & 3 deletions oap-mail/oap-mail/src/main/java/oap/mail/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.ToString;

import java.io.Serial;
import java.io.Serializable;

import static com.google.common.base.Preconditions.checkArgument;

@ToString
public class Attachment {
public class Attachment implements Serializable {
@Serial
private static final long serialVersionUID = -8962616411939449885L;

private final String contentId;
private final String file;
@JacksonXmlProperty( isAttribute = true )
Expand All @@ -46,8 +52,8 @@ public Attachment( String contentType, String content ) {
@JsonCreator
public Attachment( String contentType, String content, String contentId, String file, String name ) {
checkArgument( file != null
|| contentType.startsWith( "text/" )
|| contentType.startsWith( "image/" ),
|| contentType.startsWith( "text/" )
|| contentType.startsWith( "image/" ),
"contentType.startsWith( text/ ) || contentType.startsWith( image/ ) || file != null" );
this.contentType = contentType;
this.content = content;
Expand Down
7 changes: 6 additions & 1 deletion oap-mail/oap-mail/src/main/java/oap/mail/MailAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.apache.commons.lang3.ArrayUtils;

import javax.mail.internet.InternetAddress;
import java.io.Serial;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
Expand All @@ -38,7 +40,10 @@

@ToString
@EqualsAndHashCode
public class MailAddress {
public class MailAddress implements Serializable {
@Serial
private static final long serialVersionUID = -7106481927777678346L;

public final String personal;
public final String mail;

Expand Down
Loading
Loading