diff --git a/.env.example b/.env.example index 2785890..3c9a23a 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ +ROUTE_SERVICE_ENDPOINT="http://0.0.0.0:8888" DATABASE_URL="jdbc:postgresql://host:port/postgres?user=postgres&password=password" diff --git a/InAndOut-API-Modelling b/InAndOut-API-Modelling index 7a1ab8a..5a3381c 160000 --- a/InAndOut-API-Modelling +++ b/InAndOut-API-Modelling @@ -1 +1 @@ -Subproject commit 7a1ab8ae54bd88a272f2dfc3b1638a62c0004e78 +Subproject commit 5a3381c5918825f9b3ad7d757222f04c7842b648 diff --git a/README.md b/README.md index 5f75cec..adde7ad 100644 --- a/README.md +++ b/README.md @@ -1 +1,25 @@ # InAndOut-Route-Service + +## Setup + +This project uses git submodules for API modelling. To ensure the project builds correctly, initialize the submodules when cloning: + +```bash +git clone --recurse-submodules +``` + +Alternatively, if you've already cloned the repository, run: + +```bash +git submodule update --init --recursive +``` + +## Build + +To build the project, use the Gradle wrapper: + +```bash +./gradlew build +``` + +It is recommended to install the [Smithy CLI](https://smithy.io/2.0/guides/smithy-cli/cli_installation.html) for diverse model checks and validation. diff --git a/build.gradle.kts b/build.gradle.kts index d8f4d13..1b56704 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,96 +1,70 @@ -plugins { - java - application - id("org.springframework.boot") version "4.0.3" - id("io.spring.dependency-management") version "1.1.7" - id("org.graalvm.buildtools.native") version "0.11.4" -} +group = "com.shopping.inandout" +version = "0.0.1" +description = "A RESTful web service for handling customer queries and computing TSP solutions" -java { - toolchain { - languageVersion = JavaLanguageVersion.of(25) - } +allprojects { + repositories { + mavenLocal() + mavenCentral() + } } -application { - mainClass = "com.shopping.inandout.routeservice.RouteServiceApplication" +plugins { + `java-library` + application + id("software.amazon.smithy.gradle.smithy-base") + jacoco } -configurations { - compileOnly { - extendsFrom(configurations.annotationProcessor.get()) - } +application { + mainClass = "com.shopping.inandout.routeservice.RouteServiceWrapper" } -group = "com.shopping.inandout" -version = "0.0.1-SNAPSHOT" -description = "A RESTful web service for handling customer queries and computing TSP solutions" - repositories { mavenCentral() mavenLocal() } -val smithyJavaVersion: String by project - dependencies { - implementation("software.amazon.smithy.java:client-core:0.0.3") - implementation("software.amazon.smithy.java.codegen:plugins:$smithyJavaVersion") - // Core library for the Java client - // Adds the server implementation of the `RestJson1` protocol - implementation("software.amazon.smithy.java:aws-server-restjson:$smithyJavaVersion") + val smithyJavaVersion: String by project + + // Smithy java model generation + smithyBuild("software.amazon.smithy.java.codegen:plugins:$smithyJavaVersion") + implementation(project(":InAndOut-API-Modelling")) + // Adds an HTTP server implementation based on netty implementation("software.amazon.smithy.java:server-netty:$smithyJavaVersion") + // Adds the server implementation of the `RestJson1` protocol + implementation("software.amazon.smithy.java:aws-server-restjson:$smithyJavaVersion") - implementation("org.springframework.boot:spring-boot-starter-jdbc") - implementation("org.springframework.boot:spring-boot-starter-r2dbc") - implementation("org.springframework.boot:spring-boot-starter-restclient") - implementation("org.springframework.boot:spring-boot-starter-security") - implementation("org.springframework.boot:spring-boot-starter-security-oauth2-authorization-server") - implementation("org.springframework.boot:spring-boot-starter-security-oauth2-client") - implementation("org.springframework.boot:spring-boot-starter-session-jdbc") - implementation("org.springframework.boot:spring-boot-starter-webclient") - implementation("org.springframework.boot:spring-boot-starter-webmvc") - implementation("org.springframework.boot:spring-boot-starter-webservices") - compileOnly("org.projectlombok:lombok") - developmentOnly("org.springframework.boot:spring-boot-devtools") - annotationProcessor("org.projectlombok:lombok") - testImplementation("org.springframework.boot:spring-boot-starter-jdbc-test") - testImplementation("org.springframework.boot:spring-boot-starter-r2dbc-test") - testImplementation("org.springframework.boot:spring-boot-starter-restclient-test") - testImplementation("org.springframework.boot:spring-boot-starter-security-oauth2-authorization-server-test") - testImplementation("org.springframework.boot:spring-boot-starter-security-oauth2-client-test") - testImplementation("org.springframework.boot:spring-boot-starter-security-test") - testImplementation("org.springframework.boot:spring-boot-starter-session-jdbc-test") - testImplementation("org.springframework.boot:spring-boot-starter-webclient-test") - testImplementation("org.springframework.boot:spring-boot-starter-webmvc-test") - testImplementation("org.springframework.boot:spring-boot-starter-webservices-test") - testRuntimeOnly("org.junit.platform:junit-platform-launcher") - testRuntimeOnly("com.h2database:h2") - testRuntimeOnly("io.r2dbc:r2dbc-h2") -} + compileOnly("org.projectlombok:lombok:1.18.30") + annotationProcessor("org.projectlombok:lombok:1.18.30") + + testCompileOnly("org.projectlombok:lombok:1.18.30") + testAnnotationProcessor("org.projectlombok:lombok:1.18.30") -afterEvaluate { - sourceSets { - main { - java.srcDir("build/java-client/java-client-codegen") - } - } + testImplementation("org.junit.jupiter:junit-jupiter:5.7.1") + testImplementation("org.mockito:mockito-junit-jupiter:3.11.2") + testImplementation("org.mockito:mockito-core:3.11.2") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") } -tasks.register("smithyBuild") { - group = "build" - description = "Java models codegen from Smithy model." - commandLine("cmd", "/c", "smithy", "build", - "--config", ".\\InAndOut-API-Modelling\\smithy-build.json", - "--projection", "java-client", - "--output", "build/") +// Add generated source code to the compilation sourceSet +afterEvaluate { + val serverPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "java-server-codegen") + sourceSets.main.get().java.srcDir(serverPath) } tasks.named("compileJava") { dependsOn("smithyBuild") } -tasks.withType { - useJUnitPlatform() -} +tasks.named("test") { + useJUnitPlatform() + + maxHeapSize = "1G" + testLogging { + events("passed", "skipped", "failed") + exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL + } +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 61285a6..d997cfc 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 37f78a6..dbc3ce4 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.0-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/settings.gradle.kts b/settings.gradle.kts index c9160d0..6e145ec 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,8 +1,10 @@ +rootProject.name = "routeservice" + pluginManagement { val smithyGradleVersion: String by settings plugins { - id("software.amazon.smithy.gradle.smithy-jar") version smithyGradleVersion - id("software.amazon.smithy.gradle.smithy-base") version smithyGradleVersion + id("software.amazon.smithy.gradle.smithy-jar").version(smithyGradleVersion) + id("software.amazon.smithy.gradle.smithy-base").version(smithyGradleVersion) } repositories { @@ -12,4 +14,5 @@ pluginManagement { } } -rootProject.name = "routeservice" +// Subprojects +include("InAndOut-API-Modelling") diff --git a/smithy-build.json b/smithy-build.json new file mode 100644 index 0000000..2dd8076 --- /dev/null +++ b/smithy-build.json @@ -0,0 +1,9 @@ +{ + "version": "1.0", + "plugins": { + "java-server-codegen": { + "service": "com.shopping.inandout#RouteService", + "namespace": "com.shopping.inandout" + } + } +} diff --git a/src/main/java/com/shopping/inandout/routeservice/RouteServiceApplication.java b/src/main/java/com/shopping/inandout/routeservice/RouteServiceApplication.java deleted file mode 100644 index 6b8454a..0000000 --- a/src/main/java/com/shopping/inandout/routeservice/RouteServiceApplication.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.shopping.inandout.routeservice; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class RouteServiceApplication { - - public static void main(String[] args) { - SpringApplication.run(RouteServiceApplication.class, args); - } -} diff --git a/src/main/java/com/shopping/inandout/routeservice/RouteServiceWrapper.java b/src/main/java/com/shopping/inandout/routeservice/RouteServiceWrapper.java new file mode 100644 index 0000000..5dfbed8 --- /dev/null +++ b/src/main/java/com/shopping/inandout/routeservice/RouteServiceWrapper.java @@ -0,0 +1,69 @@ +package com.shopping.inandout.routeservice; + +import com.shopping.inandout.service.RouteService; + +import com.shopping.inandout.routeservice.activities.CreateRouteActivity; +import com.shopping.inandout.routeservice.activities.DeleteRouteActivity; +import com.shopping.inandout.routeservice.activities.GetRouteActivity; + +import java.net.URI; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.logging.Level; +import java.util.logging.Logger; + +import software.amazon.smithy.java.server.Service; +import software.amazon.smithy.java.server.Server; + +public class RouteServiceWrapper implements Runnable { + private static final Logger LOGGER = Logger.getLogger(RouteServiceWrapper.class.getName()); + + public static void main(String... args) throws RuntimeException { + new RouteServiceWrapper().run(); + } + + @Override + public void run() { + URI uri = URI.create( + System.getenv() + .getOrDefault("ROUTE_SERVICE_ENDPOINT", "http://0.0.0.0:8888")); + + Service service = RouteService.builder() + .addCreateRouteOperation(new CreateRouteActivity()) + .addDeleteRouteOperation(new DeleteRouteActivity()) + .addGetRouteOperation(new GetRouteActivity()) + .build(); + + Server server = Server.builder() + .endpoints(uri) + .addService(service) + .build(); + + CountDownLatch latch = new CountDownLatch(1); + Runtime.getRuntime().addShutdownHook( + new Thread(() -> shutdownServer(server, latch))); + + LOGGER.info("Starting server..."); + server.start(); + + try { + latch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + private void shutdownServer(Server server, CountDownLatch latch) { + LOGGER.info("Stopping server..."); + try { + server.shutdown().get(); + } catch (InterruptedException e) { + LOGGER.log(Level.SEVERE, "Error shutting down server", e); + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + LOGGER.log(Level.SEVERE, "Error shutting down server", e); + } finally { + latch.countDown(); + } + } +} diff --git a/src/main/java/com/shopping/inandout/routeservice/activities/CreateRouteActivity.java b/src/main/java/com/shopping/inandout/routeservice/activities/CreateRouteActivity.java new file mode 100644 index 0000000..45b3f3e --- /dev/null +++ b/src/main/java/com/shopping/inandout/routeservice/activities/CreateRouteActivity.java @@ -0,0 +1,13 @@ +package com.shopping.inandout.routeservice.activities; + +import com.shopping.inandout.service.CreateRouteOperation; +import com.shopping.inandout.model.CreateRouteInput; +import com.shopping.inandout.model.CreateRouteOutput; + +import software.amazon.smithy.java.server.RequestContext; + +public class CreateRouteActivity implements CreateRouteOperation { + public CreateRouteOutput createRoute(CreateRouteInput input, RequestContext context) { + throw new UnsupportedOperationException("Not (yet) supported operation"); + } +} diff --git a/src/main/java/com/shopping/inandout/routeservice/activities/DeleteRouteActivity.java b/src/main/java/com/shopping/inandout/routeservice/activities/DeleteRouteActivity.java new file mode 100644 index 0000000..ae87298 --- /dev/null +++ b/src/main/java/com/shopping/inandout/routeservice/activities/DeleteRouteActivity.java @@ -0,0 +1,13 @@ +package com.shopping.inandout.routeservice.activities; + +import com.shopping.inandout.service.DeleteRouteOperation; +import com.shopping.inandout.model.DeleteRouteInput; +import com.shopping.inandout.model.DeleteRouteOutput; + +import software.amazon.smithy.java.server.RequestContext; + +public class DeleteRouteActivity implements DeleteRouteOperation { + public DeleteRouteOutput deleteRoute(DeleteRouteInput input, RequestContext context) { + throw new UnsupportedOperationException("Not (yet) supported operation"); + } +} diff --git a/src/main/java/com/shopping/inandout/routeservice/activities/GetRouteActivity.java b/src/main/java/com/shopping/inandout/routeservice/activities/GetRouteActivity.java new file mode 100644 index 0000000..89a94df --- /dev/null +++ b/src/main/java/com/shopping/inandout/routeservice/activities/GetRouteActivity.java @@ -0,0 +1,13 @@ +package com.shopping.inandout.routeservice.activities; + +import com.shopping.inandout.service.GetRouteOperation; +import com.shopping.inandout.model.GetRouteInput; +import com.shopping.inandout.model.GetRouteOutput; + +import software.amazon.smithy.java.server.RequestContext; + +public class GetRouteActivity implements GetRouteOperation { + public GetRouteOutput getRoute(GetRouteInput input, RequestContext context) { + throw new UnsupportedOperationException("Not (yet) supported operation"); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 0016d12..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -spring.application.name=routeservice diff --git a/src/test/java/com/shopping/inandout/routeservice/RouteServiceApplicationTests.java b/src/test/java/com/shopping/inandout/routeservice/RouteServiceApplicationTests.java deleted file mode 100644 index d528e6f..0000000 --- a/src/test/java/com/shopping/inandout/routeservice/RouteServiceApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.shopping.inandout.routeservice; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class RouteServiceApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/src/test/java/com/shopping/inandout/routeservice/RouteServiceWrapperTest.java b/src/test/java/com/shopping/inandout/routeservice/RouteServiceWrapperTest.java new file mode 100644 index 0000000..605fae8 --- /dev/null +++ b/src/test/java/com/shopping/inandout/routeservice/RouteServiceWrapperTest.java @@ -0,0 +1,22 @@ +package com.shopping.inandout.routeservice; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import software.amazon.smithy.java.server.Server; + +import java.lang.reflect.Method; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class RouteServiceWrapperTest { +}