From 1c2c5db456061845eb7ecf46c5ccdaa01b4741f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 28 Feb 2024 10:17:58 -0600 Subject: [PATCH 1/2] Generate test application when Compose or testcontainers are selected for neo4j It covers data-neo4j and spring-ai-vectordb-neo4j. See gh-1419 --- .../SimpleDockerServiceResolver.java | 8 ++- .../Neo4jProjectGenerationConfiguration.java | 65 +++++++++++++++++++ .../dependency/neo4j/package-info.java | 20 ++++++ .../TestcontainersModuleRegistry.java | 2 +- .../main/resources/META-INF/spring.factories | 1 + ...4jProjectGenerationConfigurationTests.java | 48 ++++++++++++++ ...rsProjectGenerationConfigurationTests.java | 14 ++-- .../src/test/resources/compose/neo4j.yaml | 7 ++ 8 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfiguration.java create mode 100644 start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/package-info.java create mode 100644 start-site/src/test/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfigurationTests.java create mode 100644 start-site/src/test/resources/compose/neo4j.yaml diff --git a/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java b/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java index bb5ec99a7c5..8af48305899 100644 --- a/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java +++ b/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ * @author Stephane Nicoll * @author Moritz Halbritter * @author Chris Bono + * @author Eddú Meléndez */ public class SimpleDockerServiceResolver implements DockerServiceResolver { @@ -40,6 +41,7 @@ public SimpleDockerServiceResolver() { this.dockerServices.put("mariaDb", mariaDb()); this.dockerServices.put("mongoDb", mongoDb()); this.dockerServices.put("mysql", mysql()); + this.dockerServices.put("neo4j", neo4j()); this.dockerServices.put("oracleFree", oracleFree()); this.dockerServices.put("oracleXe", oracleXe()); this.dockerServices.put("postgres", postgres()); @@ -91,6 +93,10 @@ private static DockerService mysql() { return DockerService.withImageAndTag("mysql").website("https://hub.docker.com/_/mysql").ports(3306).build(); } + private static DockerService neo4j() { + return DockerService.withImageAndTag("neo4j").website("https://hub.docker.com/_/neo4j").ports(7687).build(); + } + private static DockerService oracleFree() { return DockerService.withImageAndTag("gvenzl/oracle-free") .website("https://hub.docker.com/r/gvenzl/oracle-free") diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfiguration.java new file mode 100644 index 00000000000..aeefff2bd5f --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfiguration.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed 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 + * + * https://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 io.spring.start.site.extension.dependency.neo4j; + +import io.spring.initializr.generator.buildsystem.Build; +import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; +import io.spring.start.site.container.ComposeFileCustomizer; +import io.spring.start.site.container.DockerServiceResolver; +import io.spring.start.site.container.ServiceConnections.ServiceConnection; +import io.spring.start.site.container.ServiceConnectionsCustomizer; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Configuration for generation of projects that depend on Neo4j. + * + * @author Eddú Meléndez + */ +@Configuration(proxyBeanMethods = false) +public class Neo4jProjectGenerationConfiguration { + + private static final String TESTCONTAINERS_CLASS_NAME = "org.testcontainers.containers.Neo4jContainer"; + + @Bean + @ConditionalOnRequestedDependency("testcontainers") + ServiceConnectionsCustomizer neo4jServiceConnectionsCustomizer(Build build, DockerServiceResolver serviceResolver) { + return (serviceConnections) -> { + if (isNeo4jEnabled(build)) { + serviceResolver.doWith("neo4j", (service) -> serviceConnections + .addServiceConnection(ServiceConnection.ofContainer("neo4j", service, TESTCONTAINERS_CLASS_NAME))); + } + }; + } + + @Bean + @ConditionalOnRequestedDependency("docker-compose") + ComposeFileCustomizer neo4jComposeFileCustomizer(Build build, DockerServiceResolver serviceResolver) { + return (composeFile) -> { + if (isNeo4jEnabled(build)) { + serviceResolver.doWith("neo4j", (service) -> composeFile.services() + .add("neo4j", service.andThen((builder) -> builder.environment("NEO4J_AUTH", "neo4j/secret")))); + } + }; + } + + private boolean isNeo4jEnabled(Build build) { + return build.dependencies().has("data-neo4j") || build.dependencies().has("spring-ai-vectordb-neo4j"); + } + +} diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/package-info.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/package-info.java new file mode 100644 index 00000000000..65f1fa66e20 --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed 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 + * + * https://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. + */ + +/** + * Extensions for generation of projects that depend on Neo4j. + */ +package io.spring.start.site.extension.dependency.neo4j; diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersModuleRegistry.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersModuleRegistry.java index 8e7b7edc9bc..303334c8441 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersModuleRegistry.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersModuleRegistry.java @@ -60,7 +60,7 @@ static Iterable create(Version platformVersion) { .customizeHelpDocument(addReferenceLink("Elasticsearch Container", "elasticsearch/"))); builders.add(onDependencies("data-mongodb", "data-mongodb-reactive").customizeBuild(addModule("mongodb")) .customizeHelpDocument(addReferenceLink("MongoDB Module", "databases/mongodb/"))); - builders.add(onDependencies("data-neo4j").customizeBuild(addModule("neo4j")) + builders.add(onDependencies("data-neo4j", "spring-ai-vectordb-neo4j").customizeBuild(addModule("neo4j")) .customizeHelpDocument(addReferenceLink("Neo4j Module", "databases/neo4j/"))); builders.add(onDependencies("data-r2dbc").customizeBuild(addModule("r2dbc")) .customizeHelpDocument(addReferenceLink("R2DBC support", "databases/r2dbc/"))); diff --git a/start-site/src/main/resources/META-INF/spring.factories b/start-site/src/main/resources/META-INF/spring.factories index 0a290ecb124..0dc90b938f3 100644 --- a/start-site/src/main/resources/META-INF/spring.factories +++ b/start-site/src/main/resources/META-INF/spring.factories @@ -15,6 +15,7 @@ io.spring.start.site.extension.dependency.hilla.HillaProjectGenerationConfigurat io.spring.start.site.extension.dependency.mariadb.MariaDbProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.mongodb.MongoDbProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.mysql.MysqlProjectGenerationConfiguration,\ +io.spring.start.site.extension.dependency.neo4j.Neo4jProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.observability.ObservabilityProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.oracle.OracleProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.picocli.PicocliProjectGenerationConfiguration,\ diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfigurationTests.java new file mode 100644 index 00000000000..33b1bbb9453 --- /dev/null +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfigurationTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed 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 + * + * https://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 io.spring.start.site.extension.dependency.neo4j; + +import io.spring.initializr.generator.test.project.ProjectStructure; +import io.spring.initializr.web.project.ProjectRequest; +import io.spring.start.site.extension.AbstractExtensionTests; +import org.junit.jupiter.api.Test; + +import org.springframework.core.io.ClassPathResource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link Neo4jProjectGenerationConfiguration}. + * + * @author Eddú Meléndez + */ +class Neo4jProjectGenerationConfigurationTests extends AbstractExtensionTests { + + @Test + void doesNothingWithoutDockerCompose() { + ProjectRequest request = createProjectRequest("web", "data-neo4j"); + ProjectStructure structure = generateProject(request); + assertThat(structure.getProjectDirectory().resolve("compose.yaml")).doesNotExist(); + } + + @Test + void createsNeo4jService() { + ProjectRequest request = createProjectRequest("docker-compose", "data-neo4j"); + assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/neo4j.yaml")); + } + +} diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersProjectGenerationConfigurationTests.java index 929ff8a9b78..8d713c72ac2 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersProjectGenerationConfigurationTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersProjectGenerationConfigurationTests.java @@ -74,13 +74,13 @@ static Stream supportedEntriesBuild() { Arguments.arguments("data-couchbase-reactive", "couchbase"), Arguments.arguments("data-elasticsearch", "elasticsearch"), Arguments.arguments("data-mongodb", "mongodb"), Arguments.arguments("data-mongodb-reactive", "mongodb"), - Arguments.arguments("data-neo4j", "neo4j"), Arguments.arguments("data-r2dbc", "r2dbc"), - Arguments.arguments("db2", "db2"), Arguments.arguments("kafka", "kafka"), - Arguments.arguments("kafka-streams", "kafka"), Arguments.arguments("mariadb", "mariadb"), - Arguments.arguments("mysql", "mysql"), Arguments.arguments("postgresql", "postgresql"), - Arguments.arguments("oracle", "oracle-xe"), Arguments.arguments("pulsar", "pulsar"), - Arguments.arguments("pulsar-reactive", "pulsar"), Arguments.arguments("solace", "solace"), - Arguments.arguments("sqlserver", "mssqlserver")); + Arguments.arguments("data-neo4j", "neo4j"), Arguments.arguments("spring-ai-vectordb-neo4j", "neo4j"), + Arguments.arguments("data-r2dbc", "r2dbc"), Arguments.arguments("db2", "db2"), + Arguments.arguments("kafka", "kafka"), Arguments.arguments("kafka-streams", "kafka"), + Arguments.arguments("mariadb", "mariadb"), Arguments.arguments("mysql", "mysql"), + Arguments.arguments("postgresql", "postgresql"), Arguments.arguments("oracle", "oracle-xe"), + Arguments.arguments("pulsar", "pulsar"), Arguments.arguments("pulsar-reactive", "pulsar"), + Arguments.arguments("solace", "solace"), Arguments.arguments("sqlserver", "mssqlserver")); } @ParameterizedTest diff --git a/start-site/src/test/resources/compose/neo4j.yaml b/start-site/src/test/resources/compose/neo4j.yaml new file mode 100644 index 00000000000..a909ca1f0d2 --- /dev/null +++ b/start-site/src/test/resources/compose/neo4j.yaml @@ -0,0 +1,7 @@ +services: + neo4j: + image: 'neo4j:latest' + environment: + - 'NEO4J_AUTH=neo4j/secret' + ports: + - '7687' From d9266eec0214fbc5eeff37321ab3acecea6d8260 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 29 Feb 2024 11:21:12 +0100 Subject: [PATCH 2/2] Polish "Generate test application when Compose or testcontainers are selected for neo4j" See gh-1419 --- .../dependency/neo4j/Neo4jProjectGenerationConfiguration.java | 3 ++- start-site/src/test/resources/compose/neo4j.yaml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfiguration.java index aeefff2bd5f..78037d1c7d4 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/neo4j/Neo4jProjectGenerationConfiguration.java @@ -53,7 +53,8 @@ ComposeFileCustomizer neo4jComposeFileCustomizer(Build build, DockerServiceResol return (composeFile) -> { if (isNeo4jEnabled(build)) { serviceResolver.doWith("neo4j", (service) -> composeFile.services() - .add("neo4j", service.andThen((builder) -> builder.environment("NEO4J_AUTH", "neo4j/secret")))); + .add("neo4j", + service.andThen((builder) -> builder.environment("NEO4J_AUTH", "neo4j/notverysecret")))); } }; } diff --git a/start-site/src/test/resources/compose/neo4j.yaml b/start-site/src/test/resources/compose/neo4j.yaml index a909ca1f0d2..f295c847bf8 100644 --- a/start-site/src/test/resources/compose/neo4j.yaml +++ b/start-site/src/test/resources/compose/neo4j.yaml @@ -2,6 +2,6 @@ services: neo4j: image: 'neo4j:latest' environment: - - 'NEO4J_AUTH=neo4j/secret' + - 'NEO4J_AUTH=neo4j/notverysecret' ports: - '7687'