@Override protected Properties useOverridePropertiesWithPropertiesComponent() { try { ProjectGeneratorConfiguration configuration = new ProjectGeneratorConfiguration(); ProjectGenerator projectGenerator = new ProjectGenerator(configuration, new ResourceManager()); return projectGenerator.generateApplicationProperties(newIntegration()); } catch (IOException e) { Assertions.fail("Unable to generate integration properties", e); } return null; }
private Path generate(Integration integration, ProjectGeneratorConfiguration generatorConfiguration, TestResourceManager resourceManager) throws IOException { Path destination = testFolder.newFolder("integration-project").toPath(); generate(destination, integration, generatorConfiguration, resourceManager); return destination; }
@Parameterized.Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { { "", Collections.emptyList() }, { "redhat", Arrays.asList( new ProjectGeneratorConfiguration.Templates.Resource("deployment.yml", "src/main/fabric8/deployment.yml"), new ProjectGeneratorConfiguration.Templates.Resource("settings.xml", "configuration/settings.xml") ) } }); }
@Test public void testGenerateProject() throws Exception { TestResourceManager resourceManager = new TestResourceManager(); Integration integration = resourceManager.newIntegration( new Step.Builder() .stepKind(StepKind.endpoint) ); ProjectGeneratorConfiguration configuration = new ProjectGeneratorConfiguration(); configuration.getTemplates().setOverridePath(this.basePath); configuration.getTemplates().getAdditionalResources().addAll(this.additionalResources); configuration.setSecretMaskingEnabled(true); Path runtimeDir = generate(integration, configuration, resourceManager); assertFileContents(configuration, runtimeDir.resolve("pom.xml"), "pom.xml"); assertFileContentsJson(configuration, runtimeDir.resolve("src/main/resources/syndesis/integration/integration.json"), "integration.json"); assertFileContents(configuration, runtimeDir.resolve("src/main/resources/application.properties"), "application.properties"); assertFileContents(configuration, runtimeDir.resolve("src/main/resources/loader.properties"), "loader.properties"); assertFileContents(configuration, runtimeDir.resolve(".s2i/bin/assemble"), "assemble"); assertFileContents(configuration, runtimeDir.resolve("prometheus-config.yml"), "prometheus-config.yml");
@Test public void testGenerateApplicationWithRestDSL() throws Exception { TestResourceManager resourceManager = new TestResourceManager(); byte[] content = Files.readAllBytes(Paths.get(location.toURI())); resourceManager.put("petstore", new OpenApi.Builder().document(content).id("petstore").build()); .build(); ProjectGeneratorConfiguration configuration = new ProjectGeneratorConfiguration(); configuration.getTemplates().setOverridePath(this.basePath); configuration.getTemplates().getAdditionalResources().addAll(this.additionalResources); configuration.setSecretMaskingEnabled(true); Path runtimeDir = generate(integration, configuration, resourceManager); assertThat(runtimeDir.resolve("src/main/java/io/syndesis/example/RestRouteConfiguration.java")).exists(); assertFileContents(configuration, runtimeDir.resolve("src/main/java/io/syndesis/example/RestRoute.java"), "RestRoute.java"); assertFileContents(configuration, runtimeDir.resolve("src/main/java/io/syndesis/example/RestRouteConfiguration.java"), "RestRouteConfiguration.java");
@SuppressWarnings("PMD.ExcessiveMethodLength") @Test public void testGenerate() throws Exception { IntegrationDeployment deployment = newIntegration( new SimpleStep.Builder() .stepKind("endpoint") ); ProjectGeneratorConfiguration configuration = new ProjectGeneratorConfiguration(); configuration.getMavenProperties().addRepository("maven.central", "https://repo1.maven.org/maven2"); configuration.getMavenProperties().addRepository("redhat.ga", "https://maven.repository.redhat.com/ga"); configuration.getMavenProperties().addRepository("jboss.ea", "https://repository.jboss.org/nexus/content/groups/ea"); configuration.getTemplates().setOverridePath(this.basePath); configuration.getTemplates().getAdditionalResources().addAll(this.additionalResources); configuration.setSecretMaskingEnabled(true); Path runtimeDir = generate(deployment, configuration); assertFileContents(configuration, runtimeDir.resolve("src/main/resources/application.properties"), "application.properties"); assertFileContents(configuration, runtimeDir.resolve("src/main/resources/loader.properties"), "loader.properties"); assertFileContents(configuration, runtimeDir.resolve("src/main/resources/syndesis/integration/integration.json"), "integration.json"); assertFileContents(configuration, runtimeDir.resolve("pom.xml"), "pom.xml"); assertFileContents(configuration, runtimeDir.resolve(".s2i/bin/assemble"), "assemble");
.build(); TestResourceManager resourceManager = new TestResourceManager(); ProjectGeneratorConfiguration configuration = new ProjectGeneratorConfiguration(); ProjectGenerator generator = new ProjectGenerator(configuration, resourceManager, TestConstants.MAVEN_PROPERTIES); Integration integration = new Integration.Builder() .createFrom(resourceManager.newIntegration(s1, s2)) .putConfiguredProperty("integration", "property") .build(); Properties properties = generator.generateApplicationProperties(integration);
private void assertFileContentsJson(ProjectGeneratorConfiguration generatorConfiguration, Path actualFilePath, String expectedFileName) throws URISyntaxException, IOException, JSONException { URL resource = null; String overridePath = generatorConfiguration.getTemplates().getOverridePath(); String methodName = testName.getMethodName(); int index = methodName.indexOf('['); if (index != -1) { methodName = methodName.substring(0, index); } if (!StringUtils.isEmpty(overridePath)) { resource = ProjectGeneratorTest.class.getResource(methodName + "/" + overridePath + "/" + expectedFileName); } if (resource == null) { resource = ProjectGeneratorTest.class.getResource(methodName + "/" + expectedFileName); } if (resource == null) { throw new IllegalArgumentException("Unable to find te required resource (" + expectedFileName + ")"); } final String actual = new String(Files.readAllBytes(actualFilePath), StandardCharsets.UTF_8).trim(); final String expected = new String(Files.readAllBytes(Paths.get(resource.toURI())), StandardCharsets.UTF_8).trim(); JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT); }
protected Path generate(IntegrationDeployment deployment, ProjectGeneratorConfiguration generatorConfiguration) throws IOException { final IntegrationResourceManager resourceManager = new TestResourceManager(); final IntegrationProjectGenerator generator = new ProjectGenerator(generatorConfiguration, resourceManager); try (InputStream is = generator.generate(deployment)) { Path ret = testFolder.newFolder("integration-project").toPath(); try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) { TarArchiveEntry tarEntry = tis.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) { // create a file with the same name as the tarEntry File destPath = new File(ret.toFile(), tarEntry.getName()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.getParentFile().mkdirs(); destPath.createNewFile(); try(BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) { IOUtils.copy(tis, bout); } } tarEntry = tis.getNextTarEntry(); } } return ret; } }
@Test public void testSanitizeConnectors() { TestResourceManager resourceManager = new TestResourceManager(); resourceManager.put(TestConstants.TIMER_CONNECTOR.getId().get(), TestConstants.TIMER_CONNECTOR); Integration source = resourceManager.newIntegration( new Step.Builder() .stepKind(StepKind.endpoint) .connection(new Connection.Builder() .id("timer-connection") .connectorId(TestConstants.TIMER_CONNECTOR.getId().get()) .build()) .putConfiguredProperty("period", "5000") .action(TestConstants.PERIODIC_TIMER_ACTION) .build() ); final Optional<Connection> unsanitizedConnection = source.getFlows().get(0).getSteps().get(0).getConnection(); assertThat(unsanitizedConnection.isPresent()).isTrue(); assertThat(unsanitizedConnection.get().getConnector().isPresent()).isFalse(); Integration sanitized = ProjectGeneratorHelper.sanitize(source, resourceManager); final Optional<Connection> sanitizedConnection = sanitized.getFlows().get(0).getSteps().get(0).getConnection(); assertThat(sanitizedConnection.isPresent()).isTrue(); assertThat(sanitizedConnection.get().getConnector().isPresent()).isTrue(); assertThat(sanitizedConnection.get().getConnector().get()).isEqualTo(TestConstants.TIMER_CONNECTOR); }
protected static void generate(Path destination, Integration integration, ProjectGeneratorConfiguration generatorConfiguration, TestResourceManager resourceManager) throws IOException { final IntegrationProjectGenerator generator = new ProjectGenerator(generatorConfiguration, resourceManager,TestConstants.MAVEN_PROPERTIES); try (InputStream is = generator.generate(integration)) { try (TarArchiveInputStream tis = new TarArchiveInputStream(is)) { TarArchiveEntry tarEntry = tis.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) { // create a file with the same name as the tarEntry File destPath = new File(destination.toFile(), tarEntry.getName()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.getParentFile().mkdirs(); destPath.createNewFile(); try(BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath))) { IOUtils.copy(tis, bout); } } tarEntry = tis.getNextTarEntry(); } } }; } }
@Test public void testSanitizeDefaultScheduler() { TestResourceManager resourceManager = new TestResourceManager(); Integration source = resourceManager.newIntegration( new Step.Builder() .stepKind(StepKind.endpoint) .connection(new Connection.Builder() .id("timer-connection") .connector(TestConstants.HTTP_CONNECTOR) .build()) .putConfiguredProperty("schedulerExpression", "1s") .action(TestConstants.HTTP_GET_ACTION) .build() ); assertThat(source.getFlows().get(0).getScheduler().isPresent()).isFalse(); Integration sanitized = ProjectGeneratorHelper.sanitize(source, resourceManager); final Flow sanitizedFlow = sanitized.getFlows().get(0); assertThat(sanitizedFlow.getScheduler().isPresent()).isTrue(); assertThat(sanitizedFlow.getScheduler().get()).hasFieldOrPropertyWithValue("type", Scheduler.Type.timer); assertThat(sanitizedFlow.getScheduler().get()).hasFieldOrPropertyWithValue("expression", "1s"); assertThat(sanitizedFlow.getSteps().get(0).getStepKind()).isEqualTo(StepKind.endpoint); assertThat(sanitizedFlow.getSteps().get(0).getConfiguredProperties()).doesNotContainKey("scheduler-type"); assertThat(sanitizedFlow.getSteps().get(0).getConfiguredProperties()).doesNotContainKey("scheduler-expression"); }
@Test public void shouldNormalizeSwaggerBasePaths() { final Swagger swagger = new Swagger().path("/path", new Path().get(new Operation())); assertThat(ProjectGeneratorHelper.normalizePaths(swagger).getPaths()).containsOnlyKeys("/path"); assertThat(ProjectGeneratorHelper.normalizePaths(swagger.basePath("/api")).getPaths()).containsOnlyKeys("/api/path"); } }
Integration newIntegration(Step... steps) { for (int i = 0; i < steps.length; i++) { steps[i].getConnection().filter(r -> r.getId().isPresent()).ifPresent( resource -> this.put(resource.getId().get(), resource) ); steps[i].getAction().filter(ConnectorAction.class::isInstance).map(ConnectorAction.class::cast).filter(r -> r.getId().isPresent()).ifPresent( resource -> this.put(resource.getId().get(), resource) ); steps[i].getExtension().filter(r -> r.getId().isPresent()).ifPresent( resource -> this.put(resource.getId().get(), resource) ); steps[i] = new Step.Builder().createFrom(steps[i]).build(); } return new Integration.Builder() .id("test-integration") .name("Test Integration") .description("This is a test integration!") .addFlow(new Flow.Builder() .steps(Arrays.asList(steps)) .build()) .build(); } }
protected IntegrationDeployment newIntegration(Step... steps) { for (int i = 0; i < steps.length; i++) { steps[i].getConnection().filter(r -> r.getId().isPresent()).ifPresent( resource -> resourceManager.put(resource.getId().get(), resource) ); steps[i].getAction().filter(ConnectorAction.class::isInstance).map(ConnectorAction.class::cast).filter(r -> r.getId().isPresent()).ifPresent( resource -> resourceManager.put(resource.getId().get(), resource) ); steps[i].getExtension().filter(r -> r.getId().isPresent()).ifPresent( resource -> resourceManager.put(resource.getId().get(), resource) ); if (steps[i] instanceof SimpleStep) { steps[i] = new SimpleStep.Builder().createFrom(steps[i]).putMetadata(Step.METADATA_STEP_INDEX, Integer.toString(i + 1)).build(); } else if (steps[i] instanceof ExpressionFilterStep) { steps[i] = new ExpressionFilterStep.Builder().createFrom(steps[i]).putMetadata(Step.METADATA_STEP_INDEX, Integer.toString(i + 1)).build(); } else if (steps[i] instanceof RuleFilterStep) { steps[i] = new RuleFilterStep.Builder().createFrom(steps[i]).putMetadata(Step.METADATA_STEP_INDEX, Integer.toString(i + 1)).build(); } } return new IntegrationDeployment.Builder() .integrationId("test-integration") .name("Test Integration") .spec(new IntegrationDeploymentSpec.Builder() .description("This is a test integration!") .steps(Arrays.asList(steps)) .build()) .lastUpdated(new Date(0)) .lastUpdated(new Date(0)) .build(); }
@Override protected Properties useOverridePropertiesWithPropertiesComponent() { try { ProjectGeneratorConfiguration configuration = new ProjectGeneratorConfiguration(); ProjectGenerator projectGenerator = new ProjectGenerator(configuration, new ResourceManager(), new MavenProperties()); return projectGenerator.generateApplicationProperties(newIntegration()); } catch (IOException e) { Assertions.fail("Unable to generate integration properties", e); } return null; }
private void assertFileContents(ProjectGeneratorConfiguration generatorConfiguration, Path actualFilePath, String expectedFileName) throws URISyntaxException, IOException { URL resource = null; String overridePath = generatorConfiguration.getTemplates().getOverridePath(); String methodName = testName.getMethodName(); int index = methodName.indexOf('['); if (index != -1) { methodName = methodName.substring(0, index); } if (!StringUtils.isEmpty(overridePath)) { resource = ProjectGeneratorTest.class.getResource(methodName + "/" + overridePath + "/" + expectedFileName); } if (resource == null) { resource = ProjectGeneratorTest.class.getResource(methodName + "/" + expectedFileName); } if (resource == null) { throw new IllegalArgumentException("Unable to find te required resource (" + expectedFileName + ")"); } final String actual = new String(Files.readAllBytes(actualFilePath), StandardCharsets.UTF_8).trim(); final String expected = new String(Files.readAllBytes(Paths.get(resource.toURI())), StandardCharsets.UTF_8).trim(); assertThat(actual).isEqualTo(expected); }
@Test public void testSanitizeScheduler() { TestResourceManager resourceManager = new TestResourceManager(); Integration source = resourceManager.newIntegration( new Step.Builder() .stepKind(StepKind.endpoint) .connection(new Connection.Builder() .id("timer-connection") .connector(TestConstants.HTTP_CONNECTOR) .build()) .putConfiguredProperty("schedulerType", "timer") .putConfiguredProperty("schedulerExpression", "1s") .action(TestConstants.HTTP_GET_ACTION) .build() ); assertThat(source.getFlows().get(0).getScheduler().isPresent()).isFalse(); Integration sanitized = ProjectGeneratorHelper.sanitize(source, resourceManager); final Flow sanitizedFlow = sanitized.getFlows().get(0); assertThat(sanitizedFlow.getScheduler().isPresent()).isTrue(); assertThat(sanitizedFlow.getScheduler().get()).hasFieldOrPropertyWithValue("type", Scheduler.Type.timer); assertThat(sanitizedFlow.getScheduler().get()).hasFieldOrPropertyWithValue("expression", "1s"); assertThat(sanitizedFlow.getSteps().get(0).getStepKind()).isEqualTo(StepKind.endpoint); assertThat(sanitizedFlow.getSteps().get(0).getConfiguredProperties()).doesNotContainKey("scheduler-type"); assertThat(sanitizedFlow.getSteps().get(0).getConfiguredProperties()).doesNotContainKey("scheduler-expression"); }
@Parameterized.Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { { "", Collections.emptyList() }, { "redhat", Arrays.asList( new ProjectGeneratorConfiguration.Templates.Resource("deployment.yml", "src/main/fabric8/deployment.yml"), new ProjectGeneratorConfiguration.Templates.Resource("settings.xml", "configuration/settings.xml") ) } }); }
protected void assertFileContents(ProjectGeneratorConfiguration generatorConfiguration, Path actualFilePath, String expectedFileName) throws URISyntaxException, IOException { URL resource = null; String overridePath = generatorConfiguration.getTemplates().getOverridePath(); String methodName = testName.getMethodName(); int index = methodName.indexOf('['); if (index != -1) { methodName = methodName.substring(0, index); } if (!StringUtils.isEmpty(overridePath)) { resource = ProjectGeneratorTest.class.getResource(methodName + "/" + overridePath + "/" + expectedFileName); } if (resource == null) { resource = ProjectGeneratorTest.class.getResource(methodName + "/" + expectedFileName); } if (resource == null) { throw new IllegalArgumentException("Unable to find te required resource (" + expectedFileName + ")"); } final String actual = new String(Files.readAllBytes(actualFilePath), StandardCharsets.UTF_8).trim(); final String expected = new String(Files.readAllBytes(Paths.get(resource.toURI())), StandardCharsets.UTF_8).trim(); assertThat(actual).isEqualTo(expected); }