Skip to content

Commit

Permalink
Make devtools-testing test independent from platform
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Apr 22, 2021
1 parent c881233 commit 660da8a
Show file tree
Hide file tree
Showing 18 changed files with 494 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,23 @@ public static void deepMerge(Map left, Map right) {
c.addAll((Collection) leftValue);
c.addAll((Collection) rightValue);
left.put(key, c);
} else {
} else if (rightValue instanceof Map) {
final Map map = new HashMap();
deepMerge(map, (Map) rightValue);
left.put(key, map);
} else if (rightValue instanceof Collection) {
left.put(key, new LinkedHashSet((Collection) rightValue));
} else if (rightValue.getClass().isPrimitive()
|| rightValue instanceof Integer
|| rightValue instanceof Boolean
|| rightValue instanceof Float
|| rightValue instanceof Long
|| rightValue instanceof Double
|| rightValue instanceof String) {
// Override
left.put(key, rightValue);
}
// else ignore
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ class NestedMapsTest {
private static final Map<String, Object> NESTED_MAP_1 = readTestYamlMap("/nested-map-1.yml");
private static final Map<String, Object> NESTED_MAP_2 = readTestYamlMap("/nested-map-2.yml");

@Test
void testGetValue() {
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.baz")).hasValue("baz");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.foo")).hasValue("bar");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.bar.baz")).isEmpty();
assertThat(NestedMaps.getValue(NESTED_MAP_1, "baa")).isEmpty();
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.bar.foo")).hasValue("bar");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.bar.bar")).hasValue("foo");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "bar.foo.bar.foo")).hasValue("baz");
assertThat((Collection) NestedMaps.getValue(NESTED_MAP_1, "list").get()).containsExactly("foo", "bar");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "bar.foo.bar")).hasValueSatisfying(v -> {
assertThat(v).isInstanceOf(Map.class);
assertThat((Map) v).hasFieldOrPropertyWithValue("foo", "baz");
});
}

@Test
void testDeepMerge() {
final HashMap<String, Object> target = new HashMap<>();
Expand All @@ -54,10 +38,30 @@ void testDeepMergeStream() {
checkTargetMap(target);
}

@Test
void testGetValue() {
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.baz")).hasValue("baz");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.int")).hasValue(1);
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.bool")).hasValue(false);
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.foo")).hasValue("bar");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.bar.baz")).isEmpty();
assertThat(NestedMaps.getValue(NESTED_MAP_1, "baa")).isEmpty();
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.bar.foo")).hasValue("bar");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "foo.bar.bar")).hasValue("foo");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "bar.foo.bar.foo")).hasValue("baz");
assertThat((Collection) NestedMaps.getValue(NESTED_MAP_1, "list").get()).containsExactly("foo", "bar");
assertThat(NestedMaps.getValue(NESTED_MAP_1, "bar.foo.bar")).hasValueSatisfying(v -> {
assertThat(v).isInstanceOf(Map.class);
assertThat((Map) v).hasFieldOrPropertyWithValue("foo", "baz");
});
}

private void checkTargetMap(Map<String, Object> target) {
assertThat(NestedMaps.getValue(target, "foo.baz")).hasValue("baz");
assertThat(NestedMaps.getValue(target, "foo.foo")).hasValue("bar");
assertThat(NestedMaps.getValue(target, "foo.bar")).hasValue("foo");
assertThat(NestedMaps.getValue(target, "foo.int")).hasValue(1);
assertThat(NestedMaps.getValue(target, "foo.bool")).hasValue(false);
assertThat(NestedMaps.getValue(target, "foo.bar.baz")).isEmpty();
assertThat(NestedMaps.getValue(target, "baz")).hasValue("bar");

Expand All @@ -73,6 +77,8 @@ void testUnflatten() {
data.put("foo.baz", "baz");
data.put("foo.foo", "bar");
data.put("foo.bar", "foo");
data.put("foo.int", 1);
data.put("foo.bool", false);
data.put("baz", "bar");
data.put("bar.foo.bar.foo", "bar");
data.put("bar.foo.bar.baz", "foo");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
foo:
baz: baz
foo: bar
int: 1
bool: false
bar:
foo: bar
bar: foo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.devtools.codestarts.jbang;

import static io.quarkus.devtools.codestarts.CodestartResourceLoader.loadCodestartsFromResources;
import static io.quarkus.devtools.project.QuarkusProjectHelper.getBaseCodestartResourceLoaders;

import io.quarkus.devtools.codestarts.Codestart;
import io.quarkus.devtools.codestarts.DataKey;
Expand Down Expand Up @@ -46,6 +47,13 @@ private QuarkusJBangCodestartCatalog(Collection<Codestart> codestarts) {
super(codestarts);
}

public static QuarkusJBangCodestartCatalog fromBaseCodestartsResources()
throws IOException {
final Map<String, Codestart> codestarts = loadCodestartsFromResources(getBaseCodestartResourceLoaders(),
QUARKUS_JBANG_CODESTARTS_DIR);
return new QuarkusJBangCodestartCatalog(codestarts.values());
}

public static QuarkusJBangCodestartCatalog fromResourceLoaders(List<ResourceLoader> resourceLoaders)
throws IOException {
final Map<String, Codestart> codestarts = loadCodestartsFromResources(resourceLoaders, QUARKUS_JBANG_CODESTARTS_DIR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.quarkus.devtools.codestarts.CodestartResourceLoader.loadCodestartsFromResources;
import static io.quarkus.devtools.codestarts.core.CodestartCatalogs.findLanguageName;
import static io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog.AppContent.CODE;
import static io.quarkus.devtools.project.QuarkusProjectHelper.getBaseCodestartResourceLoaders;
import static io.quarkus.devtools.project.QuarkusProjectHelper.getCodestartResourceLoaders;
import static io.quarkus.platform.catalog.processor.ExtensionProcessor.getCodestartName;
import static io.quarkus.platform.catalog.processor.ExtensionProcessor.getGuide;
Expand All @@ -23,6 +24,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -76,7 +78,19 @@ private QuarkusCodestartCatalog(Collection<Codestart> codestarts,
this.extensionsMapping = extensionsMapping;
}

public static QuarkusCodestartCatalog fromQuarkusPlatformDescriptorAndDirectories(
public static QuarkusCodestartCatalog fromBaseCodestartsResources(Map<String, Extension> extensionsMapping)
throws IOException {
final Map<String, Codestart> codestarts = loadCodestartsFromResources(getBaseCodestartResourceLoaders(),
QUARKUS_CODESTARTS_DIR);
return new QuarkusCodestartCatalog(codestarts.values(), extensionsMapping);
}

public static QuarkusCodestartCatalog fromBaseCodestartsResources()
throws IOException {
return fromBaseCodestartsResources(Collections.emptyMap());
}

public static QuarkusCodestartCatalog fromExtensionsCatalogAndDirectories(
ExtensionCatalog catalog, Collection<Path> directories)
throws IOException {
final Map<String, Codestart> codestarts = loadCodestartsFromResources(getCodestartResourceLoaders(catalog),
Expand Down Expand Up @@ -220,7 +234,7 @@ public static boolean isExample(Codestart codestart) {
return codestart.getType() == CodestartType.CODE && codestart.containsTag(Tag.EXAMPLE.key());
}

private static Map<String, Extension> buildExtensionsMapping(
public static Map<String, Extension> buildExtensionsMapping(
Collection<Extension> extensions) {
final Map<String, Extension> map = new HashMap<>(extensions.size());
extensions.forEach(e -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,16 @@
import io.quarkus.registry.catalog.ExtensionCatalog;
import java.util.List;
import java.util.Properties;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.RegisterExtension;

public class PlatformAwareTestBase {

@RegisterExtension
static final RegistryClientTest registryClientTest = new RegistryClientTest();

private ExtensionCatalog catalog;
private Properties quarkusProps;

@BeforeAll
static void enableDevToolsTestConfig() {
RegistryClientTestHelper.enableRegistryClientTestConfig();
}

@AfterAll
static void disableDevToolsTestConfig() {
RegistryClientTestHelper.disableRegistryClientTestConfig();
}

protected List<ResourceLoader> getCodestartsResourceLoaders() {
return QuarkusProjectHelper.getCodestartResourceLoaders(getExtensionsCatalog());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.devtools.testing;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class RegistryClientTest implements BeforeAllCallback, AfterAllCallback {

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
RegistryClientTestHelper.enableRegistryClientTestConfig();
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
RegistryClientTestHelper.disableRegistryClientTestConfig();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class QuarkusCodestartTest implements BeforeAllCallback, AfterAllCallback
private final BuildTool buildTool;
private final Set<Language> hasGeneratedProjectsWithMockedData = new HashSet<>();
private final Set<Language> hasGeneratedProjectsWithRealData = new HashSet<>();
private final boolean enableRegistryClient;
private Path targetDir;
private ExtensionCatalog extensionCatalog;
private QuarkusCodestartCatalog quarkusCodestartCatalog;
Expand All @@ -55,6 +56,9 @@ public class QuarkusCodestartTest implements BeforeAllCallback, AfterAllCallback
this.codestarts = builder.codestarts;
this.languages = builder.languages;
this.buildTool = builder.buildTool;
this.quarkusCodestartCatalog = builder.quarkusCodestartCatalog;
this.extensionCatalog = builder.extensionCatalog;
this.enableRegistryClient = builder.extensionCatalog == null;
this.data = builder.data;
}

Expand All @@ -64,7 +68,9 @@ public static QuarkusCodestartTestBuilder builder() {

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
enableRegistryClientTestConfig();
if (enableRegistryClient) {
enableRegistryClientTestConfig();
}
targetDir = Paths.get("target/quarkus-codestart-test/" + getTestId());
SnapshotTesting.deleteTestDirectory(targetDir.toFile());
}
Expand Down Expand Up @@ -200,7 +206,9 @@ private Path getProjectWithMockedDataDir(Language language) throws IOException {

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
disableRegistryClientTestConfig();
if (enableRegistryClient) {
disableRegistryClientTestConfig();
}
}

protected List<ResourceLoader> getCodestartsResourceLoaders() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.quarkus.devtools.testing.codestarts;

import io.quarkus.devtools.codestarts.DataKey;
import io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog;
import io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog.Language;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.registry.catalog.ExtensionCatalog;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -14,6 +16,8 @@ public class QuarkusCodestartTestBuilder {
BuildTool buildTool;
Set<String> codestarts;
Set<Language> languages;
QuarkusCodestartCatalog quarkusCodestartCatalog;
ExtensionCatalog extensionCatalog;

public QuarkusCodestartTestBuilder codestarts(String... codestarts) {
this.codestarts = new HashSet<>(Arrays.asList(codestarts));
Expand All @@ -40,6 +44,16 @@ public QuarkusCodestartTestBuilder putData(DataKey key, Object value) {
return this;
}

public QuarkusCodestartTestBuilder quarkusCodestartCatalog(QuarkusCodestartCatalog quarkusCodestartCatalog) {
this.quarkusCodestartCatalog = quarkusCodestartCatalog;
return this;
}

public QuarkusCodestartTestBuilder extensionCatalog(ExtensionCatalog extensionCatalog) {
this.extensionCatalog = extensionCatalog;
return this;
}

public QuarkusCodestartTest build() {
return new QuarkusCodestartTest(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static io.quarkus.devtools.testing.SnapshotTesting.assertThatDirectoryTreeMatchSnapshots;

import io.quarkus.devtools.codestarts.extension.QuarkusExtensionCodestartCatalog.QuarkusExtensionData;
import io.quarkus.devtools.testing.PlatformAwareTestBase;
import io.quarkus.devtools.testing.SnapshotTesting;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -12,7 +11,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

class QuarkusExtensionCodestartGenerationTest extends PlatformAwareTestBase {
class QuarkusExtensionCodestartGenerationTest {

private static final Path testDirPath = Paths.get("target/extension-codestart-gen-test");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static io.quarkus.devtools.testing.SnapshotTesting.assertThatDirectoryTreeMatchSnapshots;
import static io.quarkus.devtools.testing.SnapshotTesting.assertThatMatchSnapshot;

import io.quarkus.devtools.testing.PlatformAwareTestBase;
import io.quarkus.devtools.testing.SnapshotTesting;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -15,7 +14,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

class QuarkusJBangCodestartGenerationTest extends PlatformAwareTestBase {
class QuarkusJBangCodestartGenerationTest {

private static final Path testDirPath = Paths.get("target/jbang-codestart-gen-test");

Expand Down Expand Up @@ -52,7 +51,7 @@ void generatePicocliProject(TestInfo testInfo) throws Throwable {
}

private QuarkusJBangCodestartCatalog getCatalog() throws IOException {
return QuarkusJBangCodestartCatalog.fromResourceLoaders(getCodestartsResourceLoaders());
return QuarkusJBangCodestartCatalog.fromBaseCodestartsResources();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkus.devtools.codestarts.quarkus;

import io.quarkus.registry.catalog.ExtensionCatalog;
import io.quarkus.registry.catalog.json.JsonCatalogMapperHelper;
import io.quarkus.registry.catalog.json.JsonExtensionCatalog;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;

public final class FakeExtensionCatalog {

private static final String FAKE_EXTENSION_CATALOG_PATH = "/fake-catalog.json";
public static final ExtensionCatalog FAKE_EXTENSION_CATALOG = getFakeExtensionCatalog();
public static final QuarkusCodestartCatalog FAKE_QUARKUS_CODESTART_CATALOG = getQuarkusCodestartCatalog();

private FakeExtensionCatalog() {
}

private static QuarkusCodestartCatalog getQuarkusCodestartCatalog() {
try {
return QuarkusCodestartCatalog.fromBaseCodestartsResources(
QuarkusCodestartCatalog.buildExtensionsMapping(FAKE_EXTENSION_CATALOG.getExtensions()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static ExtensionCatalog getFakeExtensionCatalog() {
InputStream inputString = FakeExtensionCatalog.class.getResourceAsStream(FAKE_EXTENSION_CATALOG_PATH);
if (inputString == null) {
throw new IllegalStateException("Failed to locate " + FAKE_EXTENSION_CATALOG_PATH + " on the classpath");
}
try {
return JsonCatalogMapperHelper.deserialize(inputString, JsonExtensionCatalog.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Loading

0 comments on commit 660da8a

Please sign in to comment.