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

Java 15 support #1

Merged
merged 2 commits into from
Oct 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up JDK 14
- name: Set up JDK 15
uses: actions/setup-java@v1
with:
java-version: 14
java-version: 15
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
Expand Down
19 changes: 12 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ apply plugin: 'application'
archivesBaseName = 'todo-backend'
version = '0.1.0-SNAPSHOT'

sourceCompatibility = 14
targetCompatibility = 14
sourceCompatibility = 15
targetCompatibility = 15

repositories {
mavenLocal()
Expand All @@ -26,23 +26,28 @@ test {
jvmArgs(['--enable-preview'])
}

javadoc.options {
addBooleanOption('-enable-preview', true)
addStringOption('-release', '15')
}

dependencies {
implementation 'com.github.tonivade:purefun-monad:2.2-SNAPSHOT'
implementation 'com.github.tonivade:purefun-effect:2.2-SNAPSHOT'
implementation 'com.github.tonivade:purefun-instances:2.2-SNAPSHOT'
implementation 'com.github.tonivade:zeromock-server:0.14.0-SNAPSHOT'
implementation 'com.github.tonivade:puredbc:0.2.0-SNAPSHOT'
implementation 'com.github.tonivade:purecfg:0.2.0-SNAPSHOT'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.github.tonivade:purejson:0.1.0-SNAPSHOT'
implementation 'com.zaxxer:HikariCP:3.4.2'
implementation 'org.flywaydb:flyway-core:5.2.4'
runtime 'com.h2database:h2:1.4.200'
testImplementation 'com.github.tonivade:zeromock-junit5:0.14.0-SNAPSHOT'
testImplementation 'com.github.tonivade:zeromock-client:0.14.0-SNAPSHOT'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
testImplementation 'org.junit.platform:junit-platform-runner:1.6.2'
testImplementation 'org.mockito:mockito-core:3.5.9'
testImplementation 'org.mockito:mockito-junit-jupiter:3.5.9'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
testImplementation 'org.junit.platform:junit-platform-runner:1.7.0'
testImplementation 'org.mockito:mockito-core:3.5.11'
testImplementation 'org.mockito:mockito-junit-jupiter:3.5.11'
}

application {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
41 changes: 37 additions & 4 deletions src/main/java/com/github/tonivade/todo/application/TodoAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@
*/
package com.github.tonivade.todo.application;

import static com.github.tonivade.json.JsonElement.array;
import static com.github.tonivade.json.JsonElement.entry;
import static com.github.tonivade.json.JsonElement.object;
import static com.github.tonivade.json.JsonPrimitive.number;
import static com.github.tonivade.json.JsonPrimitive.string;
import static com.github.tonivade.purefun.Function1.cons;
import static com.github.tonivade.purefun.Precondition.checkNonNull;
import static com.github.tonivade.purefun.effect.TaskOf.toTask;
import static com.github.tonivade.zeromock.api.Deserializers.jsonToObject;
import static com.github.tonivade.zeromock.api.Extractors.pathParam;
import static com.github.tonivade.zeromock.api.Serializers.throwableToJson;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.NoSuchElementException;

import com.github.tonivade.json.Json;
import com.github.tonivade.json.JsonElement;
import com.github.tonivade.json.Reflection;
import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Function2;
import com.github.tonivade.purefun.Operator1;
Expand Down Expand Up @@ -97,7 +107,7 @@ public UIO<HttpResponse> deleteAll(HttpRequest request) {

private Task<TodoDTO> getTodo(HttpRequest request) {
return Task.task(request::body)
.map(jsonToObject(TodoDTO.class));
.map(jsonToObject(json -> new Json().fromJson(json, TodoDTO.class)));
}

private Task<Integer> getId(HttpRequest request) {
Expand Down Expand Up @@ -136,17 +146,40 @@ private <T> Function1<Option<T>, Operator1<Todo>> toOperation(Function2<Todo, T,
}

private Function1<Throwable, HttpResponse> fromError(Function1<Bytes, HttpResponse> toResponse) {
return throwableToJson().andThen(toResponse);
return throwableToJson(serializeThrowable()).andThen(toResponse);
}

private Function1<Throwable, String> serializeThrowable() {
return error -> {
JsonElement object = object(
entry("type", string(error.getClass().getName())),
entry("message", string(error.getMessage())),
entry("stack", array(serializeStack(error.getStackTrace())))
);
return Json.serialize(object);
};
}

private Iterable<JsonElement> serializeStack(StackTraceElement[] stackTrace) {
var items = new ArrayList<JsonElement>();
for (StackTraceElement stackTraceElement : stackTrace) {
items.add(object(
entry("className", string(stackTraceElement.getClassName())),
entry("methodName", string(stackTraceElement.getMethodName())),
entry("lineNumber", number(stackTraceElement.getLineNumber()))));
}
return items;
}

private Function1<Todo, HttpResponse> fromTodo(Function1<Bytes, HttpResponse> toResponse) {
return Serializers.<TodoDTO>objectToJson()
return Serializers.<TodoDTO>objectToJson(new Json()::toString)
.compose(TodoDTO::fromDomain)
.andThen(toResponse);
}

private Function1<Sequence<Todo>, HttpResponse> fromSequence(Function1<Bytes, HttpResponse> toResponse) {
return Serializers.<Sequence<TodoDTO>>objectToJson()
Type listOfTodos = new Reflection<Sequence<TodoDTO>>() {}.getType();
return Serializers.<Sequence<TodoDTO>>objectToJson(value -> new Json().toString(value, listOfTodos))
.<Sequence<Todo>>compose(seq -> seq.map(TodoDTO::fromDomain))
.andThen(toResponse);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[server]
host = "localhost"
port = 8080
port = 8081

[database]
url = "jdbc:h2:mem:todo"
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/github/tonivade/todo/EndToEndTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package com.github.tonivade.todo;

import static com.github.tonivade.zeromock.api.Bytes.asBytes;
import static com.github.tonivade.zeromock.api.Deserializers.jsonToObject;
import static com.github.tonivade.zeromock.api.Bytes.asString;
import static com.github.tonivade.zeromock.api.Requests.delete;
import static com.github.tonivade.zeromock.api.Requests.get;
import static com.github.tonivade.zeromock.api.Requests.post;
Expand All @@ -18,16 +18,16 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import com.github.tonivade.json.Json;
import com.github.tonivade.json.Reflection;
import com.github.tonivade.todo.application.TodoDTO;
import com.github.tonivade.zeromock.api.Deserializers;
import com.github.tonivade.zeromock.api.HttpRequest;
import com.github.tonivade.zeromock.api.HttpResponse;
import com.github.tonivade.zeromock.api.HttpStatus;
import com.github.tonivade.zeromock.api.HttpUIOService;
import com.github.tonivade.zeromock.client.UIOHttpClient;
import com.github.tonivade.zeromock.junit5.MockHttpServerExtension;
import com.github.tonivade.zeromock.server.UIOMockHttpServer;
import com.google.gson.reflect.TypeToken;

@ExtendWith(MockHttpServerExtension.class)
public class EndToEndTest {
Expand Down Expand Up @@ -171,12 +171,12 @@ public void updateTitleOrderAndCompleted(UIOMockHttpServer server, UIOHttpClient
}

private List<TodoDTO> parseList(HttpResponse response) {
Type listOfTodos = new TypeToken<List<TodoDTO>>() {}.getType();
return Deserializers.<List<TodoDTO>>jsonTo(listOfTodos).apply(response.body());
Type listOfTodos = new Reflection<List<TodoDTO>>() {}.getType();
return new Json().fromJson(asString(response.body()), listOfTodos);
}

private TodoDTO parseItem(HttpResponse response) {
return jsonToObject(TodoDTO.class).apply(response.body());
return new Json().fromJson(asString(response.body()), TodoDTO.class);
}

private HttpRequest deleteAll() {
Expand Down