diff --git a/crd-generator/collector/pom.xml b/crd-generator/collector/pom.xml index 81b4f1a9cc3..16f86bd78a3 100644 --- a/crd-generator/collector/pom.xml +++ b/crd-generator/collector/pom.xml @@ -45,5 +45,22 @@ jandex ${jandex.version} + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.slf4j + slf4j-simple + test + diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/CustomResourceCollectorTest.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/CustomResourceCollectorTest.java new file mode 100644 index 00000000000..dd7e1c2e682 --- /dev/null +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/CustomResourceCollectorTest.java @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 + * + * http://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.fabric8.crd.generator.collector; + +import io.fabric8.crd.generator.collector.examples.MyCustomResource; +import io.fabric8.crdv2.generator.CustomResourceInfo; +import org.jboss.jandex.Index; +import org.jboss.jandex.IndexView; +import org.jboss.jandex.IndexWriter; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CustomResourceCollectorTest { + + // must be adjusted if new test custom resources are added + private static final int CR_COUNT_ALL = 7; + private static final int CR_COUNT_V1_PKG = 2; + private static final int CR_COUNT_V1_VERSION = 2; + private static final int CR_COUNT_OTHER_GROUP = 1; + + @Test + void explicitClass_thenNoScanAndFind() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/classes")); + collector.withCustomResourceClass(MyCustomResource.class.getName()); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(1, infos.length); + } + + @Test + void scanClassDirWithNoCRs_thenFindZero() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/classes")); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(0, infos.length); + } + + @Test + void scanClassDirWithCRs_thenFindAll() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/test-classes")); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(CR_COUNT_ALL, infos.length); + } + + @Test + void scanClassDirWithCRsAndFilterByPackageIncludes_thenFind() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/test-classes")); + collector.withIncludePackages( + Collections.singletonList("io.fabric8.crd.generator.collector.examples.v1")); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(CR_COUNT_V1_PKG, infos.length); + } + + @Test + void scanClassDirWithCRsAndFilterByPackageExcludes_thenFind() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/test-classes")); + collector.withExcludePackages( + Collections.singletonList("io.fabric8.crd.generator.collector.examples.v1")); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(CR_COUNT_ALL - CR_COUNT_V1_PKG, infos.length); + } + + @Test + void scanClassDirWithCRsAndFilterByVersionIncludes_thenFind() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/test-classes")); + collector.withIncludeVersions(Collections.singletonList("v1")); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(CR_COUNT_V1_VERSION, infos.length); + } + + @Test + void scanClassDirWithCRsAndFilterByVersionExcludes_thenFind() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/test-classes")); + collector.withExcludeVersions(Collections.singletonList("v1")); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(CR_COUNT_ALL - CR_COUNT_V1_VERSION, infos.length); + } + + @Test + void scanClassDirWithCRsAndFilterByGroupIncludes_thenFind() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/test-classes")); + collector.withIncludeGroups(Collections.singletonList("other.samples.fabric8.io")); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(CR_COUNT_OTHER_GROUP, infos.length); + } + + @Test + void scanClassDirWithCRsAndFilterByGroupExcludes_thenFind() { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(new File("target/test-classes")); + collector.withExcludeGroups(Collections.singletonList("other.samples.fabric8.io")); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(CR_COUNT_ALL - CR_COUNT_OTHER_GROUP, infos.length); + } + + @Test + void indexWithCR_thenFindCRFromIndex() throws IOException { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withIndex(Index.of(MyCustomResource.class)); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(1, infos.length); + } + + @Test + void classDirWithCRsAndIndex_thenFindOnlyCRFromIndex(@TempDir File tempDir) throws IOException { + File sourceCustomResourceClassFile1 = new File( + "target/test-classes/io/fabric8/crd/generator/collector/examples/MyCustomResource.class"); + File targetCustomResourceClassFile1 = new File(tempDir.getAbsolutePath(), + "io/fabric8/crd/generator/collector/examples/MyCustomResource.class"); + File sourceCustomResourceClassFile2 = new File( + "target/test-classes/io/fabric8/crd/generator/collector/examples/MyOtherCustomResource.class"); + File targetCustomResourceClassFile2 = new File(tempDir.getAbsolutePath(), + "io/fabric8/crd/generator/collector/examples/MyOtherCustomResource.class"); + + targetCustomResourceClassFile1.getParentFile().mkdirs(); + Files.copy(sourceCustomResourceClassFile1.toPath(), targetCustomResourceClassFile1.toPath()); + Files.copy(sourceCustomResourceClassFile2.toPath(), targetCustomResourceClassFile2.toPath()); + + File jandexIndexFile = new File(tempDir.getAbsolutePath(), "META-INF/jandex.idx"); + jandexIndexFile.getParentFile().mkdirs(); + try (OutputStream out = Files.newOutputStream(jandexIndexFile.toPath())) { + // index contains only one custom resource + new IndexWriter(out).write(Index.of(sourceCustomResourceClassFile1)); + } + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(tempDir); + + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(1, infos.length); + } + + @Test + void classDirWithCRsAndIndexAndForceScan_thenFindAll(@TempDir File tempDir) throws IOException { + File sourceCustomResourceClassFile1 = new File( + "target/test-classes/io/fabric8/crd/generator/collector/examples/MyCustomResource.class"); + File targetCustomResourceClassFile1 = new File(tempDir.getAbsolutePath(), + "io/fabric8/crd/generator/collector/examples/MyCustomResource.class"); + File sourceCustomResourceClassFile2 = new File( + "target/test-classes/io/fabric8/crd/generator/collector/examples/MyOtherCustomResource.class"); + File targetCustomResourceClassFile2 = new File(tempDir.getAbsolutePath(), + "io/fabric8/crd/generator/collector/examples/MyOtherCustomResource.class"); + + targetCustomResourceClassFile1.getParentFile().mkdirs(); + Files.copy(sourceCustomResourceClassFile1.toPath(), targetCustomResourceClassFile1.toPath()); + Files.copy(sourceCustomResourceClassFile2.toPath(), targetCustomResourceClassFile2.toPath()); + + File jandexIndexFile = new File(tempDir.getAbsolutePath(), "META-INF/jandex.idx"); + jandexIndexFile.getParentFile().mkdirs(); + try (OutputStream out = Files.newOutputStream(jandexIndexFile.toPath())) { + // index contains only one custom resource + new IndexWriter(out).write(Index.of(sourceCustomResourceClassFile1)); + } + + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withFileToIndex(tempDir); + collector.withForceIndex(true); + + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(2, infos.length); + } + + @Test + void provideIndexWithCRAndForceScan_thenFindOnlyCRFromIndex() throws IOException { + CustomResourceCollector collector = new CustomResourceCollector(); + collector.withIndex(Index.of(MyCustomResource.class)); + collector.withForceIndex(true); + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(1, infos.length); + } + + @Test + void checkNullsafe() { + CustomResourceCollector collector = new CustomResourceCollector(); + + collector.withParentClassLoader(null); + collector.withClasspath((String) null); + collector.withClasspaths(null); + + collector.withIndex((IndexView) null); + collector.withIndices(null); + + collector.withFileToIndex((File) null); + collector.withFilesToIndex(null); + + collector.withIncludePackages(null); + collector.withExcludePackages(null); + collector.withIncludeGroups(null); + collector.withExcludeGroups(null); + collector.withIncludeVersions(null); + collector.withExcludeVersions(null); + + CustomResourceInfo[] infos = collector.findCustomResources(); + assertEquals(0, infos.length); + } + +} diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyCustomResource.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyCustomResource.java new file mode 100644 index 00000000000..22655cec1cf --- /dev/null +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyCustomResource.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 + * + * http://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.fabric8.crd.generator.collector.examples; + +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Group("samples.fabric8.io") +@Version("v1alpha1") +public class MyCustomResource extends CustomResource { +} diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyOtherCustomResource.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyOtherCustomResource.java new file mode 100644 index 00000000000..3d090416978 --- /dev/null +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyOtherCustomResource.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 + * + * http://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.fabric8.crd.generator.collector.examples; + +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Group("samples.fabric8.io") +@Version("v1alpha1") +public class MyOtherCustomResource extends CustomResource { +} diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyOtherGroupCustomResource.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyOtherGroupCustomResource.java new file mode 100644 index 00000000000..62e9d11931b --- /dev/null +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/MyOtherGroupCustomResource.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 + * + * http://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.fabric8.crd.generator.collector.examples; + +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Group("other.samples.fabric8.io") +@Version("v1alpha1") +public class MyOtherGroupCustomResource extends CustomResource { +} diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v1/MyCustomResourceInV1Pkg.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v1/MyCustomResourceInV1Pkg.java new file mode 100644 index 00000000000..a455fe405f0 --- /dev/null +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v1/MyCustomResourceInV1Pkg.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 + * + * http://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.fabric8.crd.generator.collector.examples.v1; + +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Group("samples.fabric8.io") +@Version("v1") +public class MyCustomResourceInV1Pkg extends CustomResource { +} diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v1/MyOtherCustomResourceInV1Pkg.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v1/MyOtherCustomResourceInV1Pkg.java new file mode 100644 index 00000000000..e125c7f7489 --- /dev/null +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v1/MyOtherCustomResourceInV1Pkg.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 + * + * http://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.fabric8.crd.generator.collector.examples.v1; + +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Group("samples.fabric8.io") +@Version("v1") +public class MyOtherCustomResourceInV1Pkg extends CustomResource { +} diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v2/MyCustomResourceInV2Pkg.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v2/MyCustomResourceInV2Pkg.java new file mode 100644 index 00000000000..b3d5ccd9d78 --- /dev/null +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v2/MyCustomResourceInV2Pkg.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 + * + * http://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.fabric8.crd.generator.collector.examples.v2; + +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Group("samples.fabric8.io") +@Version("v2") +public class MyCustomResourceInV2Pkg extends CustomResource { +} diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v2/MyOtherCustomResourceInV2Pkg.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v2/MyOtherCustomResourceInV2Pkg.java new file mode 100644 index 00000000000..b0d22bcb39c --- /dev/null +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/examples/v2/MyOtherCustomResourceInV2Pkg.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * 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 + * + * http://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.fabric8.crd.generator.collector.examples.v2; + +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Version; + +@Group("samples.fabric8.io") +@Version("v2") +public class MyOtherCustomResourceInV2Pkg extends CustomResource { +} diff --git a/crd-generator/collector/src/test/resources/simplelogger.properties b/crd-generator/collector/src/test/resources/simplelogger.properties new file mode 100644 index 00000000000..88095757cf8 --- /dev/null +++ b/crd-generator/collector/src/test/resources/simplelogger.properties @@ -0,0 +1,43 @@ +# +# Copyright (C) 2015 Red Hat, Inc. +# +# 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 +# +# http://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. +# + +# SLF4J's SimpleLogger configuration file +# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err. +# Default logging detail level for all instances of SimpleLogger. +# Must be one of ("trace", "debug", "info", "warn", or "error"). +# If not specified, defaults to "info". +org.slf4j.simpleLogger.defaultLogLevel=debug +# Logging detail level for a SimpleLogger instance named "xxxxx". +# Must be one of ("trace", "debug", "info", "warn", or "error"). +# If not specified, the default logging detail level is used. +#org.slf4j.simpleLogger.log.xxxxx= +# Set to true if you want the current date and time to be included in output messages. +# Default is false, and will output the number of milliseconds elapsed since startup. +#org.slf4j.simpleLogger.showDateTime=false +# The date and time format to be used in the output messages. +# The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat. +# If the format is not specified or is invalid, the default format is used. +# The default format is yyyy-MM-dd HH:mm:ss:SSS Z. +#org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z +# Set to true if you want to output the current thread name. +# Defaults to true. +#org.slf4j.simpleLogger.showThreadName=true +# Set to true if you want the Logger instance name to be included in output messages. +# Defaults to true. +org.slf4j.simpleLogger.showLogName=false +# Set to true if you want the last component of the name to be included in output messages. +# Defaults to false. +#org.slf4j.simpleLogger.showShortLogName=false