Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAILBOX-360 Scala QuotaUpdateEvent and tests
- Loading branch information
1 parent
07b68d3
commit eb5874d
Showing
4 changed files
with
1,051 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
--> | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>apache-james-mailbox</artifactId> | ||
<groupId>org.apache.james</groupId> | ||
<version>3.3.0-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>apache-james-mailbox-event-json</artifactId> | ||
<name>Apache James :: Mailbox :: Event :: JSON</name> | ||
<description>Apache James Mailbox Event JSON Scala Serialization</description> | ||
|
||
<properties> | ||
<scala.base>2.12</scala.base> | ||
<scala.version>${scala.base}.7</scala.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.beachape</groupId> | ||
<artifactId>enumeratum_${scala.base}</artifactId> | ||
<version>1.5.13</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.typesafe.play</groupId> | ||
<artifactId>play-json_${scala.base}</artifactId> | ||
<version>2.6.10</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.javacrumbs.json-unit</groupId> | ||
<artifactId>json-unit-assertj</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.james</groupId> | ||
<artifactId>apache-james-mailbox-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.julienrf</groupId> | ||
<artifactId>play-json-derived-codecs_${scala.base}</artifactId> | ||
<version>4.0.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.scala-lang</groupId> | ||
<artifactId>scala-library</artifactId> | ||
<version>${scala.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.scala-lang.modules</groupId> | ||
<artifactId>scala-java8-compat_${scala.base}</artifactId> | ||
<version>0.9.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>net.alchim31.maven</groupId> | ||
<artifactId>scala-maven-plugin</artifactId> | ||
<version>3.4.4</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.0.2</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<reuseForks>true</reuseForks> | ||
<forkCount>1C</forkCount> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>net.alchim31.maven</groupId> | ||
<artifactId>scala-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>scala-compile-first</id> | ||
<phase>process-resources</phase> | ||
<goals> | ||
<goal>add-source</goal> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>scala-test-compile</id> | ||
<phase>process-test-resources</phase> | ||
<goals> | ||
<goal>testCompile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<addArgs>-Xlog-implicits</addArgs> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
135 changes: 135 additions & 0 deletions
135
mailbox/event/json/src/main/scala/org/apache/james/event/json/QuotaEvent.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/** ************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
* ***************************************************************/ | ||
|
||
package org.apache.james.event.json | ||
|
||
import java.time.Instant | ||
import java.util.Optional | ||
|
||
import julienrf.json.derived | ||
import org.apache.james.core.quota.{QuotaCount, QuotaSize, QuotaValue} | ||
import org.apache.james.core.{Domain, User} | ||
import org.apache.james.mailbox.MailboxListener.{QuotaEvent => JavaQuotaEvent, QuotaUsageUpdatedEvent => JavaQuotaUsageUpdatedEvent} | ||
import org.apache.james.mailbox.model.{QuotaRoot, Quota => JavaQuota} | ||
import play.api.libs.json.{JsError, JsNull, JsNumber, JsObject, JsResult, JsString, JsSuccess, Json, OFormat, Reads, Writes} | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.compat.java8.OptionConverters | ||
|
||
private sealed trait QuotaEvent { | ||
def getQuotaRoot: QuotaRoot | ||
|
||
def toJava: JavaQuotaEvent | ||
} | ||
|
||
private object DTO { | ||
|
||
case class Quota[T <: QuotaValue[T]](used: T, limit: T, limits: Map[JavaQuota.Scope, T]) { | ||
def toJava: JavaQuota[T] = | ||
JavaQuota.builder[T] | ||
.used(used) | ||
.computedLimit(limit) | ||
.limitsByScope(limits.asJava) | ||
.build() | ||
} | ||
|
||
case class QuotaUsageUpdatedEvent(user: User, quotaRoot: QuotaRoot, countQuota: Quota[QuotaCount], | ||
sizeQuota: Quota[QuotaSize], time: Instant) extends QuotaEvent { | ||
override def getQuotaRoot: QuotaRoot = quotaRoot | ||
|
||
override def toJava: JavaQuotaEvent = | ||
new JavaQuotaUsageUpdatedEvent(user, getQuotaRoot, countQuota.toJava, sizeQuota.toJava, time) | ||
} | ||
|
||
} | ||
|
||
private object JsonSerialize { | ||
implicit val userWriters: Writes[User] = (user: User) => JsString(user.asString) | ||
implicit val quotaRootWrites: Writes[QuotaRoot] = quotaRoot => JsString(quotaRoot.getValue) | ||
implicit val quotaValueWrites: Writes[QuotaValue[_]] = value => if (value.isUnlimited) JsNull else JsNumber(value.asLong()) | ||
implicit val quotaScopeWrites: Writes[JavaQuota.Scope] = value => JsString(value.name) | ||
implicit val quotaCountWrites: Writes[DTO.Quota[QuotaCount]] = Json.writes[DTO.Quota[QuotaCount]] | ||
implicit val quotaSizeWrites: Writes[DTO.Quota[QuotaSize]] = Json.writes[DTO.Quota[QuotaSize]] | ||
|
||
implicit val userReads: Reads[User] = { | ||
case JsString(userAsString) => JsSuccess(User.fromUsername(userAsString)) | ||
case _ => JsError() | ||
} | ||
implicit val quotaRootReads: Reads[QuotaRoot] = { | ||
case JsString(quotaRoot) => JsSuccess(QuotaRoot.quotaRoot(quotaRoot, Optional.empty[Domain])) | ||
case _ => JsError() | ||
} | ||
implicit val quotaCountReads: Reads[QuotaCount] = { | ||
case JsNumber(count) => JsSuccess(QuotaCount.count(count.toLong)) | ||
case JsNull => JsSuccess(QuotaCount.unlimited()) | ||
case _ => JsError() | ||
} | ||
implicit val quotaSizeReads: Reads[QuotaSize] = { | ||
case JsNumber(size) => JsSuccess(QuotaSize.size(size.toLong)) | ||
case JsNull => JsSuccess(QuotaSize.unlimited()) | ||
case _ => JsError() | ||
} | ||
implicit val quotaScopeReads: Reads[JavaQuota.Scope] = { | ||
case JsString(value) => JsSuccess(JavaQuota.Scope.valueOf(value)) | ||
case _ => JsError() | ||
} | ||
|
||
implicit def scopeMapReads[V](implicit vr: Reads[V]): Reads[Map[JavaQuota.Scope, V]] = | ||
Reads.mapReads[JavaQuota.Scope, V] { str => | ||
Json.fromJson[JavaQuota.Scope](JsString(str)) | ||
} | ||
|
||
implicit def scopeMapWrite[V](implicit vr: Writes[V]): Writes[Map[JavaQuota.Scope, V]] = | ||
(m: Map[JavaQuota.Scope, V]) => { | ||
JsObject(m.map { case (k, v) => (k.toString, vr.writes(v)) }.toSeq) | ||
} | ||
|
||
implicit val quotaCReads: Reads[DTO.Quota[QuotaCount]] = Json.reads[DTO.Quota[QuotaCount]] | ||
implicit val quotaSReads: Reads[DTO.Quota[QuotaSize]] = Json.reads[DTO.Quota[QuotaSize]] | ||
|
||
implicit val quotaEventOFormat: OFormat[QuotaEvent] = derived.oformat() | ||
|
||
def toJson(event: QuotaEvent): String = Json.toJson(event).toString() | ||
|
||
def fromJson(json: String): JsResult[QuotaEvent] = Json.fromJson[QuotaEvent](Json.parse(json)) | ||
} | ||
|
||
object QuotaEvent { | ||
|
||
private def toScala[T <: QuotaValue[T]](java: JavaQuota[T]): DTO.Quota[T] = | ||
DTO.Quota(used = java.getUsed, limit = java.getLimit, limits = java.getLimitByScope.asScala.toMap) | ||
|
||
private def toScala(event: JavaQuotaUsageUpdatedEvent): DTO.QuotaUsageUpdatedEvent = | ||
DTO.QuotaUsageUpdatedEvent( | ||
user = event.getUser, | ||
quotaRoot = event.getQuotaRoot, | ||
countQuota = toScala(event.getCountQuota), | ||
sizeQuota = toScala(event.getSizeQuota), | ||
time = event.getInstant) | ||
|
||
def toJson(event: JavaQuotaEvent): String = event match { | ||
case e: JavaQuotaUsageUpdatedEvent => JsonSerialize.toJson(toScala(e)) | ||
case _ => throw new RuntimeException("no encoder found") | ||
} | ||
|
||
def fromJson(json: String): JsResult[JavaQuotaEvent] = { | ||
JsonSerialize.fromJson(json) | ||
.map(event => event.toJava) | ||
} | ||
} |
Oops, something went wrong.