Skip to content

Commit

Permalink
Merge pull request #1419 from eddumelendez
Browse files Browse the repository at this point in the history
* pr/1419:
  Polish "Generate test application when Compose or testcontainers are selected for neo4j"
  Generate test application when Compose or testcontainers are selected for neo4j

Closes gh-1419
  • Loading branch information
mhalbritter committed Feb 29, 2024
2 parents 9574a42 + d9266ee commit 9fa955b
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,6 +26,7 @@
* @author Stephane Nicoll
* @author Moritz Halbritter
* @author Chris Bono
* @author Eddú Meléndez
*/
public class SimpleDockerServiceResolver implements DockerServiceResolver {

Expand All @@ -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());
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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/notverysecret"))));
}
};
}

private boolean isNeo4jEnabled(Build build) {
return build.dependencies().has("data-neo4j") || build.dependencies().has("spring-ai-vectordb-neo4j");
}

}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static Iterable<ImplicitDependency> 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/")));
Expand Down
1 change: 1 addition & 0 deletions start-site/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ static Stream<Arguments> 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
Expand Down
7 changes: 7 additions & 0 deletions start-site/src/test/resources/compose/neo4j.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
neo4j:
image: 'neo4j:latest'
environment:
- 'NEO4J_AUTH=neo4j/notverysecret'
ports:
- '7687'

0 comments on commit 9fa955b

Please sign in to comment.