Skip to content

Commit

Permalink
fix: Vert.x generator and enricher applicable when io.vertx depende…
Browse files Browse the repository at this point in the history
…ncies present

Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed Jul 29, 2020
1 parent c24963d commit 015b624
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Usage:
### 1.0.0-SNAPSHOT
* Fix #290: Bump Fabric8 Kubernetes Client to v4.10.3
* Fix #273: Added new parameter to allow for custom _app_ name
* Fix #329: Vert.x generator and enricher applicable when `io.vertx` dependencies present

### 1.0.0-rc-1 (2020-07-23)
* Fix #252: Replace Quarkus Native Base image with ubi-minimal (same as in `Dockerfile.native`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public static boolean hasDependency(JavaProject jkubeProject, String groupId, St
return getDependency(jkubeProject, groupId, artifactId) != null;
}

public static boolean hasDependencyWithGroupId(JavaProject project, String groupId) {
return Optional.ofNullable(project).map(JavaProject::getDependencies)
.map(deps -> deps.stream().anyMatch(dep -> Objects.equals(dep.getGroupId(), groupId)))
.orElse(false);
}

public static Dependency getDependency(JavaProject jkubeProject, String groupId, String artifactId) {
List<Dependency> dependencyList = jkubeProject.getDependencies();
if (dependencyList != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common.util;


import mockit.Expectations;
import mockit.Mocked;
import org.eclipse.jkube.kit.common.Dependency;
import org.eclipse.jkube.kit.common.JavaProject;
import org.junit.Test;

import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("ResultOfMethodCallIgnored")
public class JKubeProjectUtilTest {

@Test
public void hasDependencyWithGroupIdWithNulls() {
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(null, null);
// Then
assertThat(result).isFalse();
}

@Test
public void hasDependencyWithGroupIdWithDependency(@Mocked JavaProject project) {
// Given
// @formatter:off
new Expectations() {{
project.getDependencies(); result = Arrays.asList(
Dependency.builder().groupId("io.dep").build(),
Dependency.builder().groupId("io.dep").artifactId("artifact").version("1.3.37").build(),
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()
);
}};
// @formatter:on
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(project, "io.dep");
// Then
assertThat(result).isTrue();
}

@Test
public void hasDependencyWithGroupIdWithNoDependency(@Mocked JavaProject project) {
// Given
// @formatter:off
new Expectations() {{
project.getDependencies(); result = Arrays.asList(
Dependency.builder().groupId("io.dep").build(),
Dependency.builder().groupId("io.dep").artifactId("artifact").version("1.3.37").build(),
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()
);
}};
// @formatter:on
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(project, "io.nothere");
// Then
assertThat(result).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Properties;

import org.eclipse.jkube.kit.common.Dependency;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.config.resource.GroupArtifactVersion;
import org.eclipse.jkube.kit.enricher.api.model.Configuration;
Expand Down Expand Up @@ -125,4 +126,6 @@ default Optional<String> getDependencyVersion(String groupId, String artifactId)
* @return value of property if set.
*/
String getProperty(String key);

JavaProject getProject();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.Optional;
import java.util.function.Function;

import static org.eclipse.jkube.kit.common.util.JKubeProjectUtil.hasDependencyWithGroupId;


/**
* Configures the health checks for a Vert.x project. Unlike other enricher this enricher extract the configuration from
Expand Down Expand Up @@ -101,7 +103,7 @@ protected Probe getLivenessProbe() {

private boolean isApplicable() {
return getContext().hasPlugin(VERTX_MAVEN_PLUGIN_GROUP, VERTX_MAVEN_PLUGIN_ARTIFACT)
|| getContext().hasDependency(VERTX_GROUPID, null);
|| hasDependencyWithGroupId(getContext().getProject(), VERTX_GROUPID);
}

private String getSpecificPropertyName(boolean readiness, Config config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public VertxGenerator(GeneratorContext context) {
public boolean isApplicable(List<ImageConfiguration> configs) {
return shouldAddGeneratedImageConfiguration(configs)
&& (JKubeProjectUtil.hasPlugin(getProject(), Constants.VERTX_MAVEN_PLUGIN_GROUP, Constants.VERTX_MAVEN_PLUGIN_ARTIFACT)
|| JKubeProjectUtil.hasDependency(getProject(), Constants.VERTX_GROUPID, null));
|| JKubeProjectUtil.hasDependencyWithGroupId(getProject(), Constants.VERTX_GROUPID));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand All @@ -26,6 +27,7 @@
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import org.eclipse.jkube.kit.common.Plugin;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -37,6 +39,7 @@
/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public class VertxGeneratorTest {

@Injectable
Expand All @@ -45,6 +48,10 @@ public class VertxGeneratorTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Mocked
private JavaProject project;

private GeneratorContext context;
private Dependency dropwizard;
private Dependency core;
private Dependency infinispan;
Expand All @@ -57,67 +64,102 @@ public void init() throws IOException {
.scope("compile").file(folder.newFile("vertx-core.jar")).build();
infinispan = Dependency.builder().groupId("io.vertx").artifactId("vertx-infinispan").version("3.4.2")
.type("jar").scope("compile").file(folder.newFile("vertx-infinispan.jar")).build();
context = GeneratorContext.builder()
.logger(logger)
.project(project)
.build();
}

@Test
public void testDefaultOptions(@Mocked final JavaProject project) {
new Expectations() {{
project.getBuildDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
}};

GeneratorContext context = GeneratorContext.builder()
.logger(logger)
.project(project)
.build();
VertxGenerator generator = new VertxGenerator(context);
List<String> list = generator.getExtraJavaOptions();

assertThat(list).containsOnly("-Dvertx.cacheDirBase=/tmp", "-Dvertx.disableDnsResolver=true");
}
@Test
public void isApplicableEmptyProject() {
// When
final boolean result = new VertxGenerator(context).isApplicable(Collections.emptyList());
// Then
assertThat(result).isFalse();
}

@Test
public void testWithMetrics(@Mocked final JavaProject project) {

new Expectations() {{
project.getBuildDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
project.getDependencies(); result = Arrays.asList(dropwizard, core);
}};

GeneratorContext context = GeneratorContext.builder()
.logger(logger)
.project(project)
.build();
VertxGenerator generator = new VertxGenerator(context);
List<String> list = generator.getExtraJavaOptions();

assertThat(list).containsOnly(
// Default entries
"-Dvertx.cacheDirBase=/tmp", "-Dvertx.disableDnsResolver=true",
// Metrics entries
"-Dvertx.metrics.options.enabled=true", "-Dvertx.metrics.options.jmxEnabled=true", "-Dvertx.metrics.options.jmxDomain=vertx");
}
@Test
public void isApplicableWithPlugin() {
// Given
// @formatter:off
new Expectations() {{
project.getPlugins(); result = Collections.singletonList(
Plugin.builder().groupId("io.reactiverse").artifactId("vertx-maven-plugin").build());
}};
// @formatter:on
// When
final boolean result = new VertxGenerator(context).isApplicable(Collections.emptyList());
// Then
assertThat(result).isTrue();
}

@Test
public void isApplicableWithDependency() {
// Given
// @formatter:off
new Expectations() {{
project.getDependencies(); result = Collections.singletonList(
Dependency.builder().groupId("io.vertx").build());
}};
// @formatter:on
// When
final boolean result = new VertxGenerator(context).isApplicable(Collections.emptyList());
// Then
assertThat(result).isTrue();
}

@Test
public void testDefaultOptions() {
// Given
// @formatter:off
new Expectations() {{
project.getBuildDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
}};
// @formatter:on
// When
List<String> list = new VertxGenerator(context).getExtraJavaOptions();
// Then
assertThat(list).containsOnly("-Dvertx.cacheDirBase=/tmp", "-Dvertx.disableDnsResolver=true");
}

@Test
public void testWithMetrics() {
// Given
// @formatter:off
new Expectations() {{
project.getBuildDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
project.getDependencies(); result = Arrays.asList(dropwizard, core);
}};
// @formatter:on
// When
List<String> list = new VertxGenerator(context).getExtraJavaOptions();
// Then
assertThat(list).containsOnly(
// Default entries
"-Dvertx.cacheDirBase=/tmp", "-Dvertx.disableDnsResolver=true",
// Metrics entries
"-Dvertx.metrics.options.enabled=true", "-Dvertx.metrics.options.jmxEnabled=true", "-Dvertx.metrics.options.jmxDomain=vertx");
}

@Test
public void testWithInfinispanClusterManager(@Mocked final JavaProject project) {
new Expectations() {{
project.getBuildDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
project.getDependencies(); result = Arrays.asList(infinispan, core);
}};

GeneratorContext context = GeneratorContext.builder()
.logger(logger)
.project(project)
.build();
VertxGenerator generator = new VertxGenerator(context);
Map<String, String> env = generator.getEnv(true);

assertThat(env).contains(entry("JAVA_OPTIONS", "-Dvertx.cacheDirBase=/tmp -Dvertx.disableDnsResolver=true " +
// Force IPv4
"-Djava.net.preferIPv4Stack=true"));
assertThat(env).contains(entry("JAVA_ARGS", "-cluster"));
public void testWithInfinispanClusterManager() {
// Given
// @formatter:off
new Expectations() {{
project.getBuildDirectory(); result = new File("target/tmp").getAbsolutePath();
project.getOutputDirectory(); result = new File("target/tmp/target").getAbsolutePath();
project.getDependencies(); result = Arrays.asList(infinispan, core);
}};
// @formatter:on
// When
Map<String, String> env = new VertxGenerator(context).getEnv(true);
// Then
assertThat(env).contains(entry("JAVA_OPTIONS", "-Dvertx.cacheDirBase=/tmp -Dvertx.disableDnsResolver=true " +
// Force IPv4
"-Djava.net.preferIPv4Stack=true"));
assertThat(env).contains(entry("JAVA_ARGS", "-cluster"));
}


Expand Down

0 comments on commit 015b624

Please sign in to comment.