Search in sources :

Example 1 with SimpleFileWriter

use of io.dekorate.processor.SimpleFileWriter in project dekorate by dekorateio.

the class KubernetesConfigGeneratorTest method shouldGenerateKubernetesWithoutWritingToFileSystem.

@Test
public void shouldGenerateKubernetesWithoutWritingToFileSystem() throws IOException {
    Path tempDir = Files.createTempDirectory("dekorate");
    WithProject withProject = new WithProject() {
    };
    withProject.setProject(FileProjectFactory.create(new File(".")).withDekorateOutputDir(tempDir.toAbsolutePath().toString()).withDekorateMetaDir(tempDir.toAbsolutePath().toString()));
    SessionWriter writer = new SimpleFileWriter(withProject.getProject(), false);
    Session session = Session.getSession();
    session.setWriter(writer);
    KubernetesConfigGenerator generator = new DefaultKubernetesConfigGenerator(session.getConfigurationRegistry());
    Map<String, Object> map = new HashMap<String, Object>() {

        {
            put(KubernetesConfig.class.getName(), new HashMap<String, Object>() {

                {
                    put("name", "generator-test");
                    put("partOf", "generator-test-group");
                    put("version", "latest");
                    put("replicas", 2);
                    put("ports", new Map[] { new HashMap<String, Object>() {

                        {
                            put("containerPort", 8080);
                            put("name", "HTTP");
                        }
                    } });
                    put("livenessProbe", new HashMap<String, Object>() {

                        {
                            put("httpActionPath", "/health");
                        }
                    });
                }
            });
        }
    };
    generator.addPropertyConfiguration(map);
    final Map<String, String> result = session.close();
    KubernetesList list = session.getGeneratedResources().get("kubernetes");
    assertThat(list).isNotNull();
    assertThat(list.getItems()).filteredOn(i -> "Deployment".equals(i.getKind())).hasOnlyOneElementSatisfying(item -> {
        assertThat(item).isInstanceOfSatisfying(Deployment.class, dep -> {
            assertThat(dep.getSpec()).satisfies(spec -> {
                assertThat(spec.getReplicas()).isEqualTo(2);
                assertThat(spec.getTemplate().getSpec()).satisfies(podSpec -> {
                    assertThat(podSpec.getContainers()).hasOnlyOneElementSatisfying(container -> {
                        assertThat(container.getPorts()).hasOnlyOneElementSatisfying(port -> {
                            assertThat(port.getContainerPort()).isEqualTo(8080);
                            assertThat(port.getName()).isEqualTo("HTTP");
                        });
                        assertThat(container.getLivenessProbe().getHttpGet()).satisfies(httpGetAction -> {
                            assertThat(httpGetAction.getPath()).isEqualTo("/health");
                            assertThat(httpGetAction.getPort().getIntVal()).isEqualTo(8080);
                        });
                    });
                });
            });
        });
    });
    assertThat(tempDir.resolve("kubernetes.json")).doesNotExist();
    assertThat(tempDir.resolve("kubernetes.yml")).doesNotExist();
    assertThat(result).hasSize(5);
}
Also used : Path(java.nio.file.Path) WithProject(io.dekorate.WithProject) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Files(java.nio.file.Files) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) Test(org.junit.jupiter.api.Test) FileProjectFactory(io.dekorate.project.FileProjectFactory) Session(io.dekorate.Session) Map(java.util.Map) SessionWriter(io.dekorate.SessionWriter) Path(java.nio.file.Path) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) HashMap(java.util.HashMap) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) WithProject(io.dekorate.WithProject) SessionWriter(io.dekorate.SessionWriter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) Session(io.dekorate.Session) Test(org.junit.jupiter.api.Test)

Example 2 with SimpleFileWriter

use of io.dekorate.processor.SimpleFileWriter in project dekorate by dekorateio.

the class GeneratorConfigGenerator method configurePaths.

default void configurePaths(String defaultInputPath, String defaultOutputPath) {
    final String inputPath = System.getProperty(INPUT_DIR, defaultInputPath);
    final String outputPath = Optional.ofNullable(System.getProperty(OUTPUT_DIR)).map(path -> {
        resolve(path).mkdirs();
        return path;
    }).orElse(defaultOutputPath);
    if (isInputPathValid(inputPath)) {
        applyToProject(p -> p.withDekorateInputDir(inputPath));
        getSession().getConfigurationRegistry().add(new ConfigurationSupplier<>(new GeneratorConfigBuilder()));
    }
    if (isOutputPathValid(outputPath)) {
        applyToProject(p -> p.withDekorateOutputDir(outputPath));
        getSession().setWriter(new SimpleFileWriter(getProject().getBuildInfo().getClassOutputDir().resolve(getProject().getDekorateMetaDir()), resolve(outputPath).toPath()));
    }
}
Also used : WithProject(io.dekorate.WithProject) Strings(io.dekorate.utils.Strings) PropertyConfiguration(io.dekorate.config.PropertyConfiguration) Configuration(io.dekorate.kubernetes.config.Configuration) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) AnnotationConfiguration(io.dekorate.config.AnnotationConfiguration) Map(java.util.Map) Optional(java.util.Optional) ConfigurationSupplier(io.dekorate.config.ConfigurationSupplier) ConfigurationGenerator(io.dekorate.ConfigurationGenerator) File(java.io.File) GeneratorConfigAdapter(io.dekorate.option.adapter.GeneratorConfigAdapter) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter)

Example 3 with SimpleFileWriter

use of io.dekorate.processor.SimpleFileWriter in project dekorate by dekorateio.

the class ServiceBindingConfigGeneratorTest method shouldGenerateServiceBindingWithoutWritingToFileSystem.

@Test
public void shouldGenerateServiceBindingWithoutWritingToFileSystem() throws IOException {
    Path tempDir = Files.createTempDirectory("dekorate");
    WithProject withProject = new WithProject() {
    };
    withProject.setProject(FileProjectFactory.create(new File(".")).withDekorateOutputDir(tempDir.toAbsolutePath().toString()).withDekorateMetaDir(tempDir.toAbsolutePath().toString()));
    SessionWriter writer = new SimpleFileWriter(withProject.getProject(), false);
    Session session = Session.getSession();
    session.setWriter(writer);
    ServiceBindingConfigGenerator generator = new DefaultServiceBindingConfigGenerator(session.getConfigurationRegistry());
    final HashMap<String, Object>[] services = new HashMap[1];
    services[0] = new HashMap<String, Object>() {

        {
            put("group", "postgresql.dev");
            put("kind", "Database");
            put("name", "demo-database");
            put("id", "postgresDB");
        }
    };
    final HashMap<String, Object>[] customEnvVar = new HashMap[1];
    customEnvVar[0] = new HashMap<String, Object>() {

        {
            put("name", "foo");
            put("value", "bar");
            put("configmap", "baz");
        }
    };
    final HashMap<String, Object> bindingPath = new HashMap<String, Object>();
    bindingPath.put("secretPath", "/var");
    bindingPath.put("containerPath", ".spec");
    final HashMap<String, Object> application = new HashMap<String, Object>();
    application.put("group", "apps");
    application.put("kind", "Deployment");
    application.put("version", "v1");
    application.put("name", "servicebinding-test");
    Map<String, Object> map = new HashMap<String, Object>() {

        {
            put(ServiceBindingConfig.class.getName(), new HashMap<String, Object>() {

                {
                    put("name", "servicebinding-test-binding");
                    put("application", application);
                    put("services", services);
                    put("envVarPrefix", "postgres");
                    put("bindingPath", bindingPath);
                    put("customEnvVar", customEnvVar);
                }
            });
        }
    };
    generator.addPropertyConfiguration(map);
    final Map<String, String> result = session.close();
    KubernetesList list = session.getGeneratedResources().get("kubernetes");
    assertThat(list).isNotNull();
    assertThat(list.getItems()).filteredOn(i -> "ServiceBinding".equals(i.getKind())).hasOnlyOneElementSatisfying(item -> {
        assertThat(item).isInstanceOfSatisfying(ServiceBinding.class, s -> {
            assertThat(s.getSpec()).satisfies(spec -> {
                assertThat(spec.getEnvVarPrefix()).isEqualTo("postgres");
                assertThat(spec.getApplication().getKind()).isEqualTo("Deployment");
                assertThat(spec.getApplication().getResource()).isEqualTo("deployments");
                assertThat(spec.getApplication().getVersion()).isEqualTo("v1");
                assertThat(spec.getApplication().getName()).isEqualTo("servicebinding-test");
                assertThat(spec.getApplication().getGroup()).isEqualTo("apps");
                assertThat(spec.getApplication().getBindingPath().getContainerPath()).isEqualTo(".spec");
                assertThat(spec.getApplication().getBindingPath().getSecretPath()).isEqualTo("/var");
                assertThat(spec.getServices()[0].getGroup()).isEqualTo("postgresql.dev");
                assertThat(spec.getServices()[0].getName()).isEqualTo("demo-database");
                assertThat(spec.getServices()[0].getId()).isEqualTo("postgresDB");
                assertThat(spec.getServices()[0].getKind()).isEqualTo("Database");
                assertThat(spec.getCustomEnvVar()[0].getName()).isEqualTo("foo");
                assertThat(spec.getCustomEnvVar()[0].getValueFrom().getConfigMapKeyRef().getKey()).isEqualTo("bar");
                assertThat(spec.getCustomEnvVar()[0].getValueFrom().getConfigMapKeyRef().getName()).isEqualTo("baz");
            });
        });
    });
    assertThat(tempDir.resolve("kubernetes.json")).doesNotExist();
    assertThat(tempDir.resolve("kubernetes.yml")).doesNotExist();
    assertThat(result).hasSize(6);
}
Also used : Path(java.nio.file.Path) WithProject(io.dekorate.WithProject) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Files(java.nio.file.Files) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) Test(org.junit.jupiter.api.Test) FileProjectFactory(io.dekorate.project.FileProjectFactory) ServiceBinding(io.dekorate.servicebinding.model.ServiceBinding) Session(io.dekorate.Session) Map(java.util.Map) SessionWriter(io.dekorate.SessionWriter) Path(java.nio.file.Path) HashMap(java.util.HashMap) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) WithProject(io.dekorate.WithProject) SessionWriter(io.dekorate.SessionWriter) File(java.io.File) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) Session(io.dekorate.Session) Test(org.junit.jupiter.api.Test)

Example 4 with SimpleFileWriter

use of io.dekorate.processor.SimpleFileWriter in project dekorate by dekorateio.

the class KnativeConfigGeneratorTest method shouldGenerateKnativeAndWriteToTheFilesystem.

@Test
public void shouldGenerateKnativeAndWriteToTheFilesystem() {
    WithProject withProject = new WithProject() {
    };
    withProject.setProject(FileProjectFactory.create(new File(".")).withDekorateOutputDir(tempDir.toAbsolutePath().toString()).withDekorateMetaDir(tempDir.toAbsolutePath().toString()));
    SessionWriter writer = new SimpleFileWriter(withProject.getProject());
    Session session = Session.getSession();
    session.setWriter(writer);
    DefaultKnativeConfigGenerator generator = new DefaultKnativeConfigGenerator(session.getConfigurationRegistry());
    generator.setProject(FileProjectFactory.create(new File(".")));
    Map<String, Object> map = new HashMap<String, Object>() {

        {
            put(KnativeConfig.class.getName(), new HashMap<String, Object>() {

                {
                    put("name", "generator-test");
                    put("version", "latest");
                }
            });
        }
    };
    generator.addPropertyConfiguration(map);
    final Map<String, String> result = session.close();
    KubernetesList list = session.getGeneratedResources().get("knative");
    assertThat(list).isNotNull();
    assertThat(list.getItems()).filteredOn(i -> "Service".equals(i.getKind())).isNotEmpty();
    assertThat(tempDir.resolve("knative.json")).exists();
    assertThat(tempDir.resolve("knative.yml")).exists();
    assertThat(result).hasSize(4);
}
Also used : WithProject(io.dekorate.WithProject) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Files(java.nio.file.Files) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) Test(org.junit.jupiter.api.Test) FileProjectFactory(io.dekorate.project.FileProjectFactory) Session(io.dekorate.Session) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) SessionWriter(io.dekorate.SessionWriter) Path(java.nio.file.Path) HashMap(java.util.HashMap) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) WithProject(io.dekorate.WithProject) SessionWriter(io.dekorate.SessionWriter) File(java.io.File) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) Session(io.dekorate.Session) Test(org.junit.jupiter.api.Test)

Example 5 with SimpleFileWriter

use of io.dekorate.processor.SimpleFileWriter in project dekorate by dekorateio.

the class OpenshiftConfigGeneratorTest method shouldGenerateOpenshiftAndWriteToTheFilesystem.

@Test
public void shouldGenerateOpenshiftAndWriteToTheFilesystem() {
    WithProject withProject = new WithProject() {
    };
    withProject.setProject(FileProjectFactory.create(new File(".")).withDekorateOutputDir(tempDir.toAbsolutePath().toString()).withDekorateMetaDir(tempDir.toAbsolutePath().toString()));
    SessionWriter writer = new SimpleFileWriter(withProject.getProject());
    Session session = Session.getSession();
    session.setWriter(writer);
    OpenshiftConfigGenerator generator = new DefaultOpenshiftConfigGenerator(session.getConfigurationRegistry());
    Map<String, Object> openshiftConfig = new HashMap<String, Object>() {

        {
            put(OpenshiftConfig.class.getName(), new HashMap<String, Object>() {

                {
                    put("name", "generator-test");
                    put("version", "latest");
                    put("replicas", 2);
                }
            });
        }
    };
    generator.addPropertyConfiguration(openshiftConfig);
    final Map<String, String> result = session.close();
    KubernetesList list = session.getGeneratedResources().get("openshift");
    assertThat(list).isNotNull();
    assertThat(list.getItems()).filteredOn(i -> "DeploymentConfig".equals(i.getKind())).filteredOn(i -> ((DeploymentConfig) i).getSpec().getReplicas() == 2).isNotEmpty();
    assertThat(tempDir.resolve("openshift.json")).exists();
    assertThat(tempDir.resolve("openshift.yml")).exists();
    assertThat(result).hasSize(5);
}
Also used : WithProject(io.dekorate.WithProject) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Files(java.nio.file.Files) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) Test(org.junit.jupiter.api.Test) DefaultOpenshiftConfigGenerator(io.dekorate.openshift.config.DefaultOpenshiftConfigGenerator) FileProjectFactory(io.dekorate.project.FileProjectFactory) Session(io.dekorate.Session) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) OpenshiftConfig(io.dekorate.openshift.config.OpenshiftConfig) OpenshiftConfigGenerator(io.dekorate.openshift.config.OpenshiftConfigGenerator) SessionWriter(io.dekorate.SessionWriter) Path(java.nio.file.Path) HashMap(java.util.HashMap) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) OpenshiftConfig(io.dekorate.openshift.config.OpenshiftConfig) WithProject(io.dekorate.WithProject) SessionWriter(io.dekorate.SessionWriter) DefaultOpenshiftConfigGenerator(io.dekorate.openshift.config.DefaultOpenshiftConfigGenerator) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) File(java.io.File) DefaultOpenshiftConfigGenerator(io.dekorate.openshift.config.DefaultOpenshiftConfigGenerator) OpenshiftConfigGenerator(io.dekorate.openshift.config.OpenshiftConfigGenerator) SimpleFileWriter(io.dekorate.processor.SimpleFileWriter) Session(io.dekorate.Session) Test(org.junit.jupiter.api.Test)

Aggregations

WithProject (io.dekorate.WithProject)6 SimpleFileWriter (io.dekorate.processor.SimpleFileWriter)6 File (java.io.File)6 Session (io.dekorate.Session)5 SessionWriter (io.dekorate.SessionWriter)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Test (org.junit.jupiter.api.Test)5 FileProjectFactory (io.dekorate.project.FileProjectFactory)4 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)4 IOException (java.io.IOException)4 Files (java.nio.file.Files)4 Path (java.nio.file.Path)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 BeforeAll (org.junit.jupiter.api.BeforeAll)2 ConfigurationGenerator (io.dekorate.ConfigurationGenerator)1 AnnotationConfiguration (io.dekorate.config.AnnotationConfiguration)1 ConfigurationSupplier (io.dekorate.config.ConfigurationSupplier)1 PropertyConfiguration (io.dekorate.config.PropertyConfiguration)1 Configuration (io.dekorate.kubernetes.config.Configuration)1