From 2efd70ceb6703bffb671444b0c9bd57135164793 Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 28 May 2020 20:28:27 +0200 Subject: [PATCH 01/26] refactor end to end --- .../integrations/jpa/service/ApiService.java | 67 +++ .../jpa/service/FlagAssessmentService.java | 13 +- .../route/RouteBuilderExceptionHandler.java | 3 +- src/main/resources/application.properties | 2 + src/main/resources/spring/camel-context.xml | 7 +- .../xavier/integrations/EndToEndTest.java | 522 +++++++----------- ...ingBootEndToEndTestContextInitializer.java | 41 ++ .../TestContainersInfrastructure.java | 322 +++++++++++ .../route/XmlRoutes_RestMappingTest.java | 5 +- 9 files changed, 648 insertions(+), 334 deletions(-) create mode 100644 src/main/java/org/jboss/xavier/integrations/jpa/service/ApiService.java create mode 100644 src/test/java/org/jboss/xavier/integrations/SpringBootEndToEndTestContextInitializer.java create mode 100644 src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java diff --git a/src/main/java/org/jboss/xavier/integrations/jpa/service/ApiService.java b/src/main/java/org/jboss/xavier/integrations/jpa/service/ApiService.java new file mode 100644 index 00000000..9cf68261 --- /dev/null +++ b/src/main/java/org/jboss/xavier/integrations/jpa/service/ApiService.java @@ -0,0 +1,67 @@ +package org.jboss.xavier.integrations.jpa.service; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import javax.inject.Named; + +import org.apache.camel.Exchange; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.Page; +import org.springframework.stereotype.Component; + +@Component +public class ApiService { + @Value("${camel.component.servlet.mapping.context-path}") + String contextPath; + + /* + {"data":[ + {"flag":"flag","osName":"osName","flagLabel":"flaglabel1","assessment":"assessment1"}, + {"flag":"flag2","osName":"osName2","flagLabel":"flaglabel2","assessment":"assessment2"}, + {"flag":"flag3","osName":"osName3","flagLabel":"flaglabel3","assessment":"assessment3"}, + {"flag":"flag4","osName":"osName4","flagLabel":"flaglabel4","assessment":"assessment4"}], + "meta":{"offset":0,"count":0,"limit":10}, + "links":{"last":"/api/xavier//api/xavier/mappings/flag-assessment","first":"/api/xavier//api/xavier/mappings/flag-assessment"}} + */ + + public Map mapToCustomPage(Exchange exchange) { + Page page = (Page) exchange.getIn().getBody(); + Map result = new HashMap<>(); + + Map meta = new HashMap<>(); + meta.put("count", page.getTotalElements()); + meta.put("limit", page.getSize()); + meta.put("offset", page.getNumber()); + result.put("meta", meta); + + result.put("data", page.getContent()); + + Map links = new HashMap<>(); + links.put("first", getURLLink(exchange.getIn().getHeader(Exchange.HTTP_URI, String.class), 0, page.getSize())); + links.put("last", getURLLink(exchange.getIn().getHeader(Exchange.HTTP_URI, String.class), page.getTotalPages() -1, page.getSize())); + result.put("links", links); + + return result; + } + + private String getURLLink(String url, int offset, int limit) { + int indexOfQuestion = url.indexOf("?"); + + if (indexOfQuestion >= 0) { + String query = url.substring(indexOfQuestion); + String urlNoParams = url.substring(0, indexOfQuestion); + String params = Arrays.asList(query.split("&")).stream().filter(e -> !e.startsWith("limit") && !e.startsWith("offset")).collect(Collectors.joining()); + url = urlNoParams + ((params.isEmpty()) ? "?" : "?" + params + "&"); + } else { + url = url + "?"; + } + url = url + "limit=" + limit; + url = url + "&offset=" + offset; + + return url; + } +} \ No newline at end of file diff --git a/src/main/java/org/jboss/xavier/integrations/jpa/service/FlagAssessmentService.java b/src/main/java/org/jboss/xavier/integrations/jpa/service/FlagAssessmentService.java index 83ebf019..c7c6af31 100644 --- a/src/main/java/org/jboss/xavier/integrations/jpa/service/FlagAssessmentService.java +++ b/src/main/java/org/jboss/xavier/integrations/jpa/service/FlagAssessmentService.java @@ -1,21 +1,24 @@ package org.jboss.xavier.integrations.jpa.service; +import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation; import org.jboss.xavier.analytics.pojo.output.workload.summary.FlagAssessmentModel; import org.jboss.xavier.integrations.jpa.repository.FlagAssessmentRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; -import java.util.List; - @Component public class FlagAssessmentService { @Autowired FlagAssessmentRepository flagAssessmentRepository; - public List findAll() - { - return flagAssessmentRepository.findAll(); + public Page findAll(int page, int size) { + Pageable pageable = new PageRequest(page, size); + Page result = flagAssessmentRepository.findAll(pageable); + return result; } } diff --git a/src/main/java/org/jboss/xavier/integrations/route/RouteBuilderExceptionHandler.java b/src/main/java/org/jboss/xavier/integrations/route/RouteBuilderExceptionHandler.java index e6bfd294..34c3d006 100644 --- a/src/main/java/org/jboss/xavier/integrations/route/RouteBuilderExceptionHandler.java +++ b/src/main/java/org/jboss/xavier/integrations/route/RouteBuilderExceptionHandler.java @@ -36,10 +36,11 @@ public void configure() throws Exception { } public void markAnalysisAsFailed(Exchange e) { + System.out.println("++++++++++++++ EXCEPTION : " + e); String analysisId = ""; try { analysisId = e.getIn().getHeader(ANALYSIS_ID, "", String.class); - if (analysisId.isEmpty()) { + if (analysisId.isEmpty() && e.getIn().getHeader(MA_METADATA, Map.class) != null) { analysisId = (String) e.getIn().getHeader(MA_METADATA, Map.class).get(ANALYSIS_ID); } if (StringUtils.isNotEmpty(analysisId)) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4c60edd8..3bd466cf 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -62,6 +62,8 @@ insights.upload.accountnumber=123454 # tracing enabled? camel.springboot.tracing=false +pagination.defaults.flag-assessment.limit=100 + # REST Endpoints configuration api.version = v1.0 camel.component.servlet.mapping.context-path=/api/xavier/${api.version}/* diff --git a/src/main/resources/spring/camel-context.xml b/src/main/resources/spring/camel-context.xml index 862eb5ff..f03b0df4 100644 --- a/src/main/resources/spring/camel-context.xml +++ b/src/main/resources/spring/camel-context.xml @@ -68,6 +68,8 @@ Mapping REST API + + mappings @@ -75,7 +77,10 @@ - + + + + diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index fcdd1896..83d1c2b6 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -1,11 +1,31 @@ package org.jboss.xavier.integrations; +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import javax.inject.Inject; +import javax.inject.Named; + import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.ListObjectsV2Request; import com.amazonaws.util.Base64; import com.fasterxml.jackson.databind.ObjectMapper; + import org.apache.camel.CamelContext; import org.apache.camel.Exchange; +import org.apache.camel.ServiceStatus; import org.apache.camel.builder.AdviceWithRouteBuilder; import org.apache.camel.test.spring.CamelSpringBootRunner; import org.apache.camel.test.spring.UseAdviceWith; @@ -15,9 +35,12 @@ import org.assertj.core.api.SoftAssertions; import org.awaitility.Duration; import org.jboss.xavier.Application; +import org.jboss.xavier.analytics.pojo.output.AnalysisModel; import org.jboss.xavier.analytics.pojo.output.InitialSavingsEstimationReportModel; import org.jboss.xavier.analytics.pojo.output.workload.inventory.WorkloadInventoryReportModel; import org.jboss.xavier.analytics.pojo.output.workload.summary.AppIdentifierModel; +import org.jboss.xavier.analytics.pojo.output.workload.summary.FlagAssessmentIdentityModel; +import org.jboss.xavier.analytics.pojo.output.workload.summary.FlagAssessmentModel; import org.jboss.xavier.analytics.pojo.output.workload.summary.OSInformationModel; import org.jboss.xavier.analytics.pojo.output.workload.summary.ScanRunModel; import org.jboss.xavier.analytics.pojo.output.workload.summary.SummaryModel; @@ -27,6 +50,7 @@ import org.jboss.xavier.analytics.pojo.output.workload.summary.WorkloadsJavaRuntimeDetectedModel; import org.jboss.xavier.integrations.jpa.repository.InitialSavingsEstimationReportRepository; import org.jboss.xavier.integrations.jpa.repository.AppIdentifierRepository; +import org.jboss.xavier.integrations.jpa.repository.FlagAssessmentRepository; import org.jboss.xavier.integrations.jpa.service.InitialSavingsEstimationReportService; import org.jboss.xavier.integrations.route.model.notification.FilePersistedNotification; import org.jboss.xavier.integrations.route.model.user.User; @@ -34,6 +58,9 @@ import org.junit.After; import org.junit.Before; import org.junit.ClassRule; +import org.junit.Ignore; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; @@ -41,9 +68,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.util.EnvironmentTestUtils; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Import; import org.springframework.core.ParameterizedTypeReference; import org.springframework.hateoas.PagedResources; @@ -52,98 +76,20 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.KafkaContainer; -import org.testcontainers.containers.Network; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.containers.localstack.LocalStackContainer; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.images.builder.ImageFromDockerfile; -import org.testcontainers.utility.MountableFile; - -import javax.inject.Inject; -import javax.inject.Named; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Comparator; -import java.util.Enumeration; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; -import java.util.UUID; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.awaitility.Awaitility.await; -import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3; @RunWith(CamelSpringBootRunner.class) -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) @UseAdviceWith // Disables automatic start of Camel context @SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -@ContextConfiguration(initializers = EndToEndTest.Initializer.class) +@ContextConfiguration(initializers = SpringBootEndToEndTestContextInitializer.class) @Import(TestConfigurationS3.class) @ActiveProfiles("test") public class EndToEndTest { - private static Logger logger = LoggerFactory.getLogger(EndToEndTest.class); - - @ClassRule - public static GenericContainer activemq = new GenericContainer<>("vromero/activemq-artemis") - .withExposedPorts(61616, 8161) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AMQ-LOG")) - .withEnv("DISABLE_SECURITY", "true") - .withEnv("BROKER_CONFIG_GLOBAL_MAX_SIZE", "50000") - .withEnv("BROKER_CONFIG_MAX_SIZE_BYTES", "50000") - .withEnv("BROKER_CONFIG_MAX_DISK_USAGE", "100"); - - @ClassRule - public static GenericContainer kie_server = new GenericContainer<>("jboss/kie-server-showcase:7.18.0.Final") - .withNetworkAliases("kie-server") - .withExposedPorts(8080) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("KIE-LOG")) - .withEnv("KIE_SERVER_ID", "analytics-kieserver") - .withEnv("KIE_ADMIN_USER", "kieserver") - .withEnv("KIE_ADMIN_PWD", "kieserver1!") - .withEnv("KIE_SERVER_MODE", "DEVELOPMENT") - .withEnv("KIE_MAVEN_REPO", "https://oss.sonatype.org/content/repositories/snapshots") - .withEnv("KIE_REPOSITORY","https://repository.jboss.org/nexus/content/groups/public-jboss") - .withEnv("KIE_SERVER_CONTROLLER_PWD","admin") - .withEnv("KIE_SERVER_CONTROLLER_USER","admin") - .withEnv("KIE_SERVER_LOCATION","http://kie-server:8080/kie-server/services/rest/server") - .withEnv("KIE_SERVER_PWD","kieserver1!") - .withEnv("KIE_SERVER_USER","kieserver"); - - @ClassRule - public static PostgreSQLContainer postgreSQL = new PostgreSQLContainer() - .withDatabaseName("sampledb") - .withUsername("admin") - .withPassword("redhat"); - - @ClassRule - public static LocalStackContainer localstack = new LocalStackContainer() - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AWS-LOG")) - .withServices(S3); - - private static String ingressCommitHash = "3ea33a8d793c2154f7cfa12057ca005c5f6031fa"; // 2019-11-11 - private static String insightsRbacCommitHash = "a55b610a1385f0f6d3188b08710ec6a5890a97f6"; // 2020-02-05 + private Logger logger = LoggerFactory.getLogger(EndToEndTest.class); @Inject private InitialSavingsEstimationReportService initialSavingsEstimationReportService; @@ -154,6 +100,9 @@ public class EndToEndTest { @Inject private AppIdentifierRepository appIdentifierRepository; + @Inject + private FlagAssessmentRepository flagAssessmentRepository; + @Value("${S3_BUCKET}") private String bucket; @@ -182,211 +131,15 @@ public class EndToEndTest { @Value("${test.bigfile.vms_expected:5254}") private int numberVMsExpected_InBigFile; - public static class Initializer implements ApplicationContextInitializer { - - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - try { - cloneIngressRepoAndUnzip(); - cloneInsightsRbacRepo_UnzipAndConfigure(); - - Network network = Network.newNetwork(); - - GenericContainer minio = new GenericContainer<>("minio/minio") - .withCommand("server /data") - .withExposedPorts(9000) - .withNetworkAliases("minio") - .withNetwork(network) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("MINIO-LOG")) - .withEnv("MINIO_ACCESS_KEY", "BQA2GEXO711FVBVXDWKM") - .withEnv("MINIO_SECRET_KEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC"); - minio.start(); - - Thread.sleep(5000); - GenericContainer createbuckets = new GenericContainer<>("minio/mc") - .dependsOn(minio) - .withNetwork(network) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("MINIO-MC-LOG")) - .withCopyFileToContainer(MountableFile.forClasspathResource("minio-bucket-creation-commands.sh"), "/") - .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd.withEntrypoint("sh", "/minio-bucket-creation-commands.sh", "minio:9000")); - createbuckets.start(); - - KafkaContainer kafka = new KafkaContainer() - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("KAFKA-LOG")) - .withNetworkAliases("kafka") - .withNetwork(network); - kafka.start(); - - GenericContainer ingress = new GenericContainer(new ImageFromDockerfile() - .withDockerfile(Paths.get("src/test/resources/insights-ingress-go/Dockerfile"))) - .withExposedPorts(3000) - .withNetwork(network) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("INGRESS-LOG")) - .withEnv("AWS_ACCESS_KEY_ID","BQA2GEXO711FVBVXDWKM") - .withEnv("AWS_SECRET_ACCESS_KEY","uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") - .withEnv("AWS_REGION","us-east-1") - .withEnv("INGRESS_STAGEBUCKET","insights-upload-perma") - .withEnv("INGRESS_REJECTBUCKET","insights-upload-rejected") - .withEnv("INGRESS_INVENTORYURL","http://inventory:8080/api/inventory/v1/hosts") - .withEnv("INGRESS_VALIDTOPICS","xavier,testareno,advisortestareno,advisor") - .withEnv("OPENSHIFT_BUILD_COMMIT","woopwoop") - .withEnv("INGRESS_MINIODEV","true") - .withEnv("INGRESS_MINIOACCESSKEY","BQA2GEXO711FVBVXDWKM") - .withEnv("INGRESS_MINIOSECRETKEY","uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") - .withEnv("INGRESS_MINIOENDPOINT", "minio:9000") - .withEnv("INGRESS_KAFKABROKERS", "kafka:9092"); - ingress.start(); - - Network rbacNetwork = Network.newNetwork(); - GenericContainer rbacPostgreSQL = new PostgreSQLContainer() - .withDatabaseName("rb_database") - .withUsername("rbac_username") - .withPassword("rbac_password") - .withNetwork(rbacNetwork) - .withNetworkAliases("rbac_db"); - rbacPostgreSQL.start(); - GenericContainer rbacServer = new GenericContainer<>(new ImageFromDockerfile() - .withDockerfile(Paths.get("src/test/resources/insights-rbac/insightsRbac_Dockerfile"))) - .withNetwork(rbacNetwork) - .withNetworkAliases("rbac") - .withExposedPorts(8000) - .withEnv("DATABASE_SERVICE_NAME", "POSTGRES_SQL") - .withEnv("DATABASE_ENGINE", "postgresql") - .withEnv("DATABASE_NAME", "rb_database") - .withEnv("DATABASE_USER", "rbac_username") - .withEnv("DATABASE_PASSWORD", "rbac_password") - .withEnv("POSTGRES_SQL_SERVICE_HOST", "rbac_db") - .withEnv("POSTGRES_SQL_SERVICE_PORT", "5432"); - rbacServer.start(); - - importProjectIntoKIE(); - - EnvironmentTestUtils.addEnvironment("environment", configurableApplicationContext.getEnvironment(), - "amq.server=" + activemq.getContainerIpAddress(), - "amq.port=" + activemq.getMappedPort(61616), - "minio.host=" + getContainerHost(minio, 9000), - "insights.upload.host=" + getContainerHost(ingress), - "insights.properties=yearOverYearGrowthRatePercentage,percentageOfHypervisorsMigratedOnYear1,percentageOfHypervisorsMigratedOnYear2,percentageOfHypervisorsMigratedOnYear3,reportName,reportDescription", - "camel.component.servlet.mapping.context-path=/*", - "insights.kafka.host=" + kafka.getBootstrapServers(), - "postgresql.service.name=" + postgreSQL.getContainerIpAddress(), - "postgresql.service.port=" + postgreSQL.getFirstMappedPort(), - "spring.datasource.username=" + postgreSQL.getUsername(), - "spring.datasource.password=" + postgreSQL.getPassword(), - "S3_HOST=" + localstack.getEndpointConfiguration(S3).getServiceEndpoint(), - "S3_REGION="+ localstack.getEndpointConfiguration(S3).getSigningRegion(), - "kieserver.devel-service=" + getHostForKie() + "/kie-server", - "spring.datasource.url = jdbc:postgresql://" + getContainerHost(postgreSQL) + "/sampledb" , - "spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect" , - "thread.concurrentConsumers=3", - "insights.rbac.path=/api/v1/access/", - "insights.rbac.host=" + "http://" + getContainerHost(rbacServer, 8000)); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @NotNull - private String getContainerHost(GenericContainer container, Integer port) { - return container.getContainerIpAddress() + ":" + container.getMappedPort(port); - } - - @NotNull - private static String getContainerHost(GenericContainer container) { - return container.getContainerIpAddress() + ":" + container.getFirstMappedPort(); - } - } - @Inject CamelContext camelContext; @Inject AmazonS3 amazonS3; - @Value("${camel.component.servlet.mapping.context-path}") private String basePath; - private static void cloneIngressRepoAndUnzip() throws IOException { - // downloading, unzipping, renaming - String ingressRepoZipURL = "https://github.com/RedHatInsights/insights-ingress-go/archive/" + ingressCommitHash + ".zip"; - File compressedFile = new File("src/test/resources/ingressRepo.zip"); - FileUtils.copyURLToFile(new URL(ingressRepoZipURL), compressedFile, 1000, 10000); - unzipFile(compressedFile, "src/test/resources"); - - // we rename the directory because we had issues with Docker and the long folder - FileUtils.moveDirectory(new File("src/test/resources/insights-ingress-go-" + ingressCommitHash), new File("src/test/resources/insights-ingress-go")); - } - - private static void cloneInsightsRbacRepo_UnzipAndConfigure() throws IOException { - // downloading, unzipping, renaming - String insightsRbacRepoZipURL = "https://github.com/RedHatInsights/insights-rbac/archive/" + insightsRbacCommitHash + ".zip"; - File compressedFile = new File("src/test/resources/insightsRbacRepo.zip"); - FileUtils.copyURLToFile(new URL(insightsRbacRepoZipURL), compressedFile, 1000, 10000); - unzipFile(compressedFile, "src/test/resources"); - - // we rename the directory because we had issues with Docker and the long folder - FileUtils.moveDirectory(new File("src/test/resources/insights-rbac-" + insightsRbacCommitHash), new File("src/test/resources/insights-rbac")); - - // Use custom Dockerfile - FileUtils.copyFile( - new File("src/test/resources/insightsRbac_Dockerfile"), - new File("src/test/resources/insights-rbac/insightsRbac_Dockerfile") - ); - - // Configure default system roles for application=migration-analytics - FileUtils.copyFile( - new File("src/test/resources/insightsRbac_roleDefinitions.json"), - new File("src/test/resources/insights-rbac/rbac/management/role/definitions/migration-analytics.json") - ); - } - - private static void unzipFile(File file, String outputDir) throws IOException { - java.util.zip.ZipFile zipFile = new ZipFile(file); - try { - Enumeration entries = zipFile.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - File entryDestination = new File(outputDir, entry.getName()); - if (entry.isDirectory()) { - entryDestination.mkdirs(); - } else { - entryDestination.getParentFile().mkdirs(); - InputStream in = zipFile.getInputStream(entry); - OutputStream out = new FileOutputStream(entryDestination); - IOUtils.copy(in, out); - in.close(); - out.close(); - } - } - } finally { - zipFile.close(); - } - } - - private static String getHostForKie() { - return kie_server.getContainerIpAddress() + ":" + kie_server.getFirstMappedPort(); - } - - private static void importProjectIntoKIE() throws InterruptedException, IOException { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - headers.setCacheControl("no-cache"); - headers.set("Authorization", "Basic YWRtaW46YWRtaW4="); // admin:admin - - String kieRestURL = "http://" + getHostForKie() + "/kie-server/services/rest/"; - - // KIE Container Creation - HttpHeaders kieheaders = new HttpHeaders(); - kieheaders.setContentType(MediaType.APPLICATION_JSON); - kieheaders.set("Authorization", "Basic a2llc2VydmVyOmtpZXNlcnZlcjEh"); - kieheaders.setCacheControl("no-cache"); - String kieContainerBody = "{\"container-id\" : \"xavier-analytics_0.0.1-SNAPSHOT\",\"release-id\" : {\"group-id\" : \"org.jboss.xavier\",\"artifact-id\" : \"xavier-analytics\",\"version\" : \"0.0.1-SNAPSHOT\" } }"; - try { - new RestTemplate().exchange(kieRestURL + "server/containers/xavier-analytics_0.0.1-SNAPSHOT", HttpMethod.PUT, new HttpEntity<>(kieContainerBody, kieheaders), String.class); - } catch (RestClientException e) { - e.printStackTrace(); - } - } + private static int analysisNum; @Before public void setDefaults() { @@ -477,10 +230,53 @@ public void setDefaults() { .withIdentifier("IBM Websphere App Server") // Workload name .build(); appIdentifierRepository.save(Arrays.asList(applicationPlatform1, applicationPlatform2, applicationPlatform3, applicationPlatform4)); + + FlagAssessmentModel flagAssessmentModel1 = new FlagAssessmentModel(); + flagAssessmentModel1.setAssessment("assessment1"); + flagAssessmentModel1.setFlag("flag1"); + flagAssessmentModel1.setFlagLabel("flaglabel1"); + flagAssessmentModel1.setOsName("osname1"); + FlagAssessmentIdentityModel fgId1 = new FlagAssessmentIdentityModel(); + fgId1.setFlag("flag"); + fgId1.setOsName("osName"); + flagAssessmentModel1.setId(fgId1); + + FlagAssessmentModel flagAssessmentModel2 = new FlagAssessmentModel(); + flagAssessmentModel2.setAssessment("assessment2"); + flagAssessmentModel2.setFlag("flag2"); + flagAssessmentModel2.setFlagLabel("flaglabel2"); + flagAssessmentModel2.setOsName("osname2"); + FlagAssessmentIdentityModel fgId2 = new FlagAssessmentIdentityModel(); + fgId2.setFlag("flag2"); + fgId2.setOsName("osName2"); + flagAssessmentModel2.setId(fgId2); + + FlagAssessmentModel flagAssessmentModel3 = new FlagAssessmentModel(); + flagAssessmentModel3.setAssessment("assessment3"); + flagAssessmentModel3.setFlag("flag3"); + flagAssessmentModel3.setFlagLabel("flaglabel3"); + flagAssessmentModel3.setOsName("osname3"); + FlagAssessmentIdentityModel fgId3 = new FlagAssessmentIdentityModel(); + fgId3.setFlag("flag3"); + fgId3.setOsName("osName3"); + flagAssessmentModel3.setId(fgId3); + + FlagAssessmentModel flagAssessmentModel4 = new FlagAssessmentModel(); + flagAssessmentModel4.setAssessment("assessment4"); + flagAssessmentModel4.setFlag("flag4"); + flagAssessmentModel4.setFlagLabel("flaglabel4"); + flagAssessmentModel4.setOsName("osname4"); + FlagAssessmentIdentityModel fgId4 = new FlagAssessmentIdentityModel(); + fgId4.setFlag("flag4"); + fgId4.setOsName("osName4"); + flagAssessmentModel4.setId(fgId4); + + flagAssessmentRepository.save(Arrays.asList(flagAssessmentModel1, flagAssessmentModel2, flagAssessmentModel3, flagAssessmentModel4)); } - @After - public void cleanUp() throws IOException { + @BeforeClass + @AfterClass + public static void cleanUp() throws IOException { // cleaning downloadable files/directories FileUtils.deleteDirectory(new File("src/test/resources/insights-ingress-go")); FileUtils.deleteQuietly(new File("src/test/resources/ingressRepo.zip")); @@ -495,39 +291,51 @@ private int getStorageObjectsSize() { return s3Size; } - @Test - public void end2endTest() throws Exception { - Thread.sleep(2000); - - // given - camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "5000"); - camelContext.start(); - - camelContext.getRouteDefinition("download-file").adviceWith(camelContext, new AdviceWithRouteBuilder() { - @Override - public void configure() { - weaveById("setHttpUri") - .replace() - .process(e -> { - String url = e.getIn().getBody(FilePersistedNotification.class).getUrl(); - url = url.replace("minio:9000", minio_host); - e.getIn().setHeader("httpUriReplaced", url); - }) - .setHeader("Exchange.HTTP_URI", header("httpUriReplaced")) - .setHeader("Host", constant("minio:9000")); - - weaveById("toOldHost") - .replace() - .to("http4:oldhost?preserveHostHeader=true"); - } - }); + @Before + public void initCamel() throws Exception { + if (camelContext.getStatus() != ServiceStatus.Started ) { + Thread.sleep(2000); + + // given + camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "5000"); + camelContext.start(); + + camelContext.getRouteDefinition("download-file").adviceWith(camelContext, new AdviceWithRouteBuilder() { + @Override + public void configure() { + weaveById("setHttpUri") + .replace() + .process(e -> { + String url = e.getIn().getBody(FilePersistedNotification.class).getUrl(); + url = url.replace("minio:9000", minio_host); + e.getIn().setHeader("httpUriReplaced", url); + }) + .setHeader("Exchange.HTTP_URI", header("httpUriReplaced")) + .setHeader("Host", constant("minio:9000")); + + weaveById("toOldHost") + .replace() + .to("http4:oldhost?preserveHostHeader=true"); + } + }); // 1. Check user has firstTime ResponseEntity userEntity = new RestTemplate().exchange(getBaseURLAPIPath() + "/user", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); assertThat(userEntity.getBody().isFirstTimeCreatingReports()).isTrue(); + logger.info("****** Checking First Time User **********"); + } + } + + @After + public void finishTest() throws Exception { + //camelContext.stop(); + } + + @Test + @Ignore + public void whenRegularTestShouldAnswerInTime() throws Exception { // Start the camel route as if the UI was sending the file to the Camel Rest Upload route - int analysisNum = 0; assertThat(getStorageObjectsSize()).isEqualTo(0); logger.info("+++++++ Regular Test ++++++"); @@ -581,7 +389,7 @@ public void configure() { softly.assertThat(workloadInventoryReport.getBody().getContent().stream().flatMap(e -> e.getRecommendedTargetsIMS().stream()).distinct().count()).isEqualTo(5); softly.assertThat(workloadInventoryReport.getBody().getContent().stream().filter(e -> e.getRecommendedTargetsIMS().contains("OSP")).count()).isEqualTo(11); softly.assertThat(workloadInventoryReport.getBody().getContent().stream().filter(e -> e.getRecommendedTargetsIMS().contains("RHEL")).count()).isEqualTo(4); - softly.assertThat(workloadInventoryReport.getBody().getContent().stream().filter(e -> e.getRecommendedTargetsIMS().contains("None")).count()).isEqualTo(1); + softly.assertThat(workloadInventoryReport.getBody().getContent().stream().filter(e -> e.getRecommendedTargetsIMS().contains("None")).count()).isEqualTo(0); softly.assertThat(workloadInventoryReport.getBody().getContent().stream().flatMap(e -> e.getFlagsIMS().stream()).distinct().count()).isEqualTo(2); softly.assertThat(workloadInventoryReport.getBody().getContent().stream().filter(e -> e.getFlagsIMS().contains("Shared Disk")).count()).isEqualTo(2); softly.assertThat(workloadInventoryReport.getBody().getContent().stream().filter(e -> e.getOsName().contains("ServerNT") && e.getWorkloads().contains("Microsoft SQL Server")).count()).isEqualTo(1); @@ -613,27 +421,47 @@ public void configure() { softly.assertThat(wks_scanrunmodel_actual).isEqualTo(wks_scanrunmodel_expected); softly.assertThat(wks_ostypemodel_actual).isEqualTo(wks_ostypemodel_expected); }); + } + + @Test + @Ignore + + public void whenPerformanceTestShouldAnswerInTime() throws Exception { + analysisNum++; // Performance test logger.info("+++++++ Performance Test ++++++"); new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20190829-16128-uq17dx.tar.gz", "application/zip"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_PerformaceTest)).isEqualTo(142); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_PerformaceTest)).isEqualTo(142); + } + + @Test + @Ignore + public void whenFileWithVMWithoutHostShouldAddVMToWorkloadInventory() throws Exception { + analysisNum++; // Test with a file with VM without Host logger.info("+++++++ Test with a file with VM without Host ++++++"); new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_without_host.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum),timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); ResponseEntity> workloadInventoryReport_file_vm_without_host = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?size=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); assertThat(workloadInventoryReport_file_vm_without_host.getBody().getContent().size()).isEqualTo(8); assertThat(workloadInventoryReport_file_vm_without_host.getBody().getContent().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(2); assertThat(workloadInventoryReport_file_vm_without_host.getBody().getContent().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(6); + } + + @Test + @Ignore + + public void whenFileWithHostWithoutClusterShouldAddVMToWorkloadInventory() throws Exception { + analysisNum++; // Test with a file with Host without Cluster logger.info("+++++++ Test with a file with Host without Cluster ++++++"); new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-host_without_cluster.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); ResponseEntity> workloadInventoryReport_file_host_without_cluster = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?size=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); // Total VMs @@ -642,11 +470,18 @@ public void configure() { assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getContent().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(3); // Right VMs assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getContent().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(5); + } + + @Test + @Ignore + + public void whenFileWithWrongCPUCoresPerSocketShouldNotFailAndFallbackTheValue() throws Exception { + analysisNum++; // Test with a file with Wrong CPU cores per socket logger.info("+++++++ Test with a file with Wrong CPU cores per socket ++++++"); new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-wrong_cpu_cores_per_socket.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(5); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(5); ResponseEntity initialCostSavingsReport_wrong_cpu_cores = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); assertThat(initialCostSavingsReport_wrong_cpu_cores.getBody().getEnvironmentModel().getHypervisors()).isEqualTo(2); @@ -655,11 +490,16 @@ public void configure() { assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getContent().stream().filter(e -> e.getCpuCores() == null).count()).isEqualTo(0); assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getContent().stream().filter(e -> e.getCpuCores() != null).count()).isEqualTo(5); assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getContent().size()).isEqualTo(5); + } + @Test + @Ignore + public void whenFileWith0CPUCoresPerSocketShouldNotFailAndFallbackTheValue() throws Exception { + analysisNum++; // Test with a file with 0 CPU cores per socket logger.info("+++++++ Test with a file with 0 CPU cores per socket ++++++"); new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_0_cores.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); ResponseEntity initialCostSavingsReport_zero_cpu_cores = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); assertThat(initialCostSavingsReport_zero_cpu_cores.getBody().getEnvironmentModel().getHypervisors()).isEqualTo(2); @@ -668,11 +508,17 @@ public void configure() { assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getContent().stream().filter(e -> e.getCpuCores() == null).count()).isEqualTo(0); assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getContent().stream().filter(e -> e.getCpuCores() != null).count()).isEqualTo(8); assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getContent().size()).isEqualTo(8); + } + @Test + @Ignore + + public void whenFileWithUsedDiskStorageShouldNotFailAndFallbackTheValue() throws Exception { + analysisNum++; // Test with a file with VM.used_disk_storage logger.info("+++++++ Test with a file with VM.used_disk_storage ++++++"); new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); ResponseEntity initialCostSavingsReport_vm_with_used_disk = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); assertThat(initialCostSavingsReport_vm_with_used_disk.getBody().getEnvironmentModel().getHypervisors()).isEqualTo(4); @@ -688,18 +534,19 @@ public void configure() { .filter(e -> e.getFlagsIMS().contains(">4 vNICs")).count()).isEqualTo(0); // Test Insights Enabled + analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20200318-Insights.tar.gz", "application/zip"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(14); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(14); ResponseEntity> workloadInventoryReport_with_insights_enabled = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?size=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); assertThat(workloadInventoryReport_with_insights_enabled.getBody().getContent().size()).isEqualTo(14); assertThat(workloadInventoryReport_with_insights_enabled.getBody().getContent().stream().filter(e -> e.getInsightsEnabled()).count()).isEqualTo(2); // Test OSInformation, JavaRuntimes, and ApplicationPlatforms in WMS + analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20200304-Linux_JDK.tar.gz", "application/zip"), String.class); - - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(14); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(14); ResponseEntity workloadSummaryReportJavaRuntimes = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-summary", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); WorkloadSummaryReportModel workloadSummaryReport_JavaRuntimesExpected = new ObjectMapper().readValue(IOUtils.resourceToString("cfme_inventory-20200304-Linux_JDK-summary-report.json", StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), WorkloadSummaryReportModel.class); @@ -708,12 +555,21 @@ public void configure() { .usingRecursiveComparison() .ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*", ".*report.*", ".*workloadsDetectedOSTypeModels.*", ".*scanRunModels.*") .isEqualTo(workloadSummaryReport_JavaRuntimesExpected); + } + @Test + @Ignore + public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { // Ultra Performance test logger.info("+++++++ Ultra Performance Test ++++++"); + analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", ++analysisNum), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); + } + @Test + @Ignore + public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exception { // Stress test // We load 3 times a BIG file ( 8 Mb ) and 2 times a small file ( 316 Kb ) // More or less 7 minutes each bunch of threads of Big Files @@ -734,18 +590,32 @@ public void configure() { int timeoutMilliseconds_secondSmallFile = timeoutMilliseconds_UltraPerformaceTest + timeoutMilliseconds_SmallFileSummaryReport; assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum + 4), timeoutMilliseconds_secondSmallFile)).isEqualTo( 8); + } + + @Test + public void testCustomPage() { + ResponseEntity response = new RestTemplate().exchange(getBaseURLAPIPath() + "/mappings/flag-assessment?limit=10&offset=0", HttpMethod.GET, getRequestEntity(), String.class); + assertThat(response.getBody()).contains("\"first\":\"/api/xavier/v1.0/mappings/flag-assessment?limit=10&offset=0\""); + assertThat(response.getBody()).contains("\"last\":\"/api/xavier/v1.0/mappings/flag-assessment?limit=10&offset=0\""); + logger.info("RESPONSE PAGINATION : " + response.getBody()); + } + + @Test + @Ignore + public void whenDeleteReportShouldRemoveFileInS3() throws Exception { // Testing the deletion of a file in S3 logger.info("++++++++ Delete report test +++++"); int s3ObjectsBefore = getStorageObjectsSize(); - - ResponseEntity stringEntity = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d", 3), HttpMethod.DELETE, getRequestEntity(), new ParameterizedTypeReference() {}); + + // we upload a file to be sure there's one report to delete, as it could be that this test is executed the first + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); + analysisNum++; + Thread.sleep(5000); + ResponseEntity stringEntity = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d", analysisNum), HttpMethod.DELETE, getRequestEntity(), new ParameterizedTypeReference() {}); assertThat(stringEntity.getStatusCodeValue()).isEqualTo(HttpStatus.SC_NO_CONTENT); - assertThat(initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", 3L)).isNull(); - assertThat(getStorageObjectsSize()).isEqualTo(s3ObjectsBefore - 1); - - camelContext.stop(); + assertThat(getStorageObjectsSize()).isEqualTo(s3ObjectsBefore); } private String getBaseURLAPIPath() { diff --git a/src/test/java/org/jboss/xavier/integrations/SpringBootEndToEndTestContextInitializer.java b/src/test/java/org/jboss/xavier/integrations/SpringBootEndToEndTestContextInitializer.java new file mode 100644 index 00000000..c97ec733 --- /dev/null +++ b/src/test/java/org/jboss/xavier/integrations/SpringBootEndToEndTestContextInitializer.java @@ -0,0 +1,41 @@ +package org.jboss.xavier.integrations; + +import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3; + +import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; + +public class SpringBootEndToEndTestContextInitializer implements ApplicationContextInitializer { + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + try { + TestContainersInfrastructure containersInfrastructure = new TestContainersInfrastructure(); + containersInfrastructure.createAndStartDockerContainers(); + Thread.sleep(10000); + + EnvironmentTestUtils.addEnvironment("test", configurableApplicationContext.getEnvironment(), + "amq.server=" + containersInfrastructure.getActivemq().getContainerIpAddress(), + "amq.port=" + containersInfrastructure.getActivemq().getMappedPort(61616), + "minio.host=" + containersInfrastructure.getContainerHost(containersInfrastructure.getMinio(), 9000), + "insights.upload.host=" + containersInfrastructure.getContainerHost(containersInfrastructure.getIngress()), + "insights.properties=yearOverYearGrowthRatePercentage,percentageOfHypervisorsMigratedOnYear1,percentageOfHypervisorsMigratedOnYear2,percentageOfHypervisorsMigratedOnYear3,reportName,reportDescription", +// "camel.component.servlet.mapping.context-path=/api/xavier/*", + "insights.kafka.host=" + containersInfrastructure.getKafka().getBootstrapServers(), + "postgresql.service.name=" + containersInfrastructure.getPostgreSQL().getContainerIpAddress(), + "postgresql.service.port=" + containersInfrastructure.getPostgreSQL().getFirstMappedPort(), + "spring.datasource.username=" + containersInfrastructure.getPostgreSQL().getUsername(), + "spring.datasource.password=" + containersInfrastructure.getPostgreSQL().getPassword(), + "S3_HOST=" + containersInfrastructure.getLocalstack().getEndpointConfiguration(S3).getServiceEndpoint(), + "S3_REGION="+ containersInfrastructure.getLocalstack().getEndpointConfiguration(S3).getSigningRegion(), + "kieserver.devel-service=" + containersInfrastructure.getHostForKie() + "/kie-server", + "spring.datasource.url = jdbc:postgresql://" + containersInfrastructure.getContainerHost(containersInfrastructure.getPostgreSQL()) + "/sampledb" , + "spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect" , + "thread.concurrentConsumers=3", + "insights.rbac.path=/api/v1/access/", + "insights.rbac.host=" + "http://" + containersInfrastructure.getContainerHost(containersInfrastructure.getRbacServer(), 8000)); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java new file mode 100644 index 00000000..6a68b013 --- /dev/null +++ b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java @@ -0,0 +1,322 @@ +package org.jboss.xavier.integrations; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.nio.file.Paths; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import javax.validation.constraints.NotNull; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.junit.ClassRule; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.KafkaContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.containers.localstack.LocalStackContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.images.builder.ImageFromDockerfile; +import org.testcontainers.utility.MountableFile; + +import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3; + +public class TestContainersInfrastructure { + private Logger logger = LoggerFactory.getLogger(TestContainersInfrastructure.class); + + private String ingressCommitHash = "3ea33a8d793c2154f7cfa12057ca005c5f6031fa"; // 2019-11-11 + private String insightsRbacCommitHash = "a55b610a1385f0f6d3188b08710ec6a5890a97f6"; // 2020-02-05 + + private GenericContainer kie_server; + + private PostgreSQLContainer postgreSQL; + + private LocalStackContainer localstack; + + private GenericContainer activemq; + + private GenericContainer minio; + + private GenericContainer createbuckets; + + private KafkaContainer kafka; + + private GenericContainer ingress; + + private GenericContainer rbacPostgreSQL; + + private GenericContainer rbacServer; + + private GenericContainer getRBACServerContainer(Network rbacNetwork) { + return new GenericContainer<>(new ImageFromDockerfile() + .withDockerfile(Paths.get("src/test/resources/insights-rbac/insightsRbac_Dockerfile"))) + .withNetwork(rbacNetwork).withNetworkAliases("rbac").withExposedPorts(8000) + .withEnv("DATABASE_SERVICE_NAME", "POSTGRES_SQL").withEnv("DATABASE_ENGINE", "postgresql") + .withEnv("DATABASE_NAME", "rb_database").withEnv("DATABASE_USER", "rbac_username") + .withEnv("DATABASE_PASSWORD", "rbac_password").withEnv("POSTGRES_SQL_SERVICE_HOST", "rbac_db") + .withEnv("POSTGRES_SQL_SERVICE_PORT", "5432"); + } + + private GenericContainer getRBACPostgreSQLContainer(Network rbacNetwork) { + return new PostgreSQLContainer().withDatabaseName("rb_database").withUsername("rbac_username") + .withPassword("rbac_password").withNetwork(rbacNetwork).withNetworkAliases("rbac_db"); + } + + private GenericContainer getIngressContainer(Network network) { + return new GenericContainer(new ImageFromDockerfile() + .withDockerfile(Paths.get("src/test/resources/insights-ingress-go/Dockerfile"))).withExposedPorts(3000) + .withNetwork(network).withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("INGRESS-LOG")) + .withEnv("AWS_ACCESS_KEY_ID", "BQA2GEXO711FVBVXDWKM") + .withEnv("AWS_SECRET_ACCESS_KEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") + .withEnv("AWS_REGION", "us-east-1").withEnv("INGRESS_STAGEBUCKET", "insights-upload-perma") + .withEnv("INGRESS_REJECTBUCKET", "insights-upload-rejected") + .withEnv("INGRESS_INVENTORYURL", "http://inventory:8080/api/inventory/v1/hosts") + .withEnv("INGRESS_VALIDTOPICS", "xavier,testareno,advisortestareno,advisor") + .withEnv("OPENSHIFT_BUILD_COMMIT", "woopwoop").withEnv("INGRESS_MINIODEV", "true") + .withEnv("INGRESS_MINIOACCESSKEY", "BQA2GEXO711FVBVXDWKM") + .withEnv("INGRESS_MINIOSECRETKEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") + .withEnv("INGRESS_MINIOENDPOINT", "minio:9000").withEnv("INGRESS_KAFKABROKERS", "kafka:9092"); + } + + private KafkaContainer getKafkaContainer(Network network) { + return new KafkaContainer().withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("KAFKA-LOG")) + .withNetworkAliases("kafka").withNetwork(network); + } + + private GenericContainer getMinioCreateBucketsContainer(Network network, GenericContainer minio) { + return new GenericContainer<>("minio/mc").dependsOn(minio).withNetwork(network) + .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("MINIO-MC-LOG")) + .withCopyFileToContainer(MountableFile.forClasspathResource("minio-bucket-creation-commands.sh"), "/") + .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd.withEntrypoint("sh", + "/minio-bucket-creation-commands.sh", "minio:9000")); + } + + private GenericContainer getMinioContainer(Network network) { + return new GenericContainer<>("minio/minio").withCommand("server /data").withExposedPorts(9000) + .withNetworkAliases("minio").withNetwork(network) + .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("MINIO-LOG")) + .withEnv("MINIO_ACCESS_KEY", "BQA2GEXO711FVBVXDWKM") + .withEnv("MINIO_SECRET_KEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC"); + } + + private LocalStackContainer getLocalstackContainer() { + return new LocalStackContainer().withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AWS-LOG")) + .withServices(S3); + } + + private PostgreSQLContainer getPostgreSQLContainer() { + return new PostgreSQLContainer().withDatabaseName("sampledb").withUsername("admin").withPassword("redhat"); + } + + private GenericContainer getKIEContainer() { + return new GenericContainer<>("jboss/kie-server-showcase:7.18.0.Final").withNetworkAliases("kie-server") + .withExposedPorts(8080).withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("KIE-LOG")) + .withEnv("KIE_SERVER_ID", "analytics-kieserver").withEnv("KIE_ADMIN_USER", "kieserver") + .withEnv("KIE_ADMIN_PWD", "kieserver1!").withEnv("KIE_SERVER_MODE", "DEVELOPMENT") + .withEnv("KIE_MAVEN_REPO", "https://oss.sonatype.org/content/repositories/snapshots") + .withEnv("KIE_REPOSITORY", "https://repository.jboss.org/nexus/content/groups/public-jboss") + .withEnv("KIE_SERVER_CONTROLLER_PWD", "admin").withEnv("KIE_SERVER_CONTROLLER_USER", "admin") + .withEnv("KIE_SERVER_LOCATION", "http://kie-server:8080/kie-server/services/rest/server") + .withEnv("KIE_SERVER_PWD", "kieserver1!").withEnv("KIE_SERVER_USER", "kieserver"); + } + + private GenericContainer getActiveMQContainer() { + return (GenericContainer) new GenericContainer<>("vromero/activemq-artemis").withExposedPorts(61616, 8161) + .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AMQ-LOG")).withEnv("DISABLE_SECURITY", "true") + .withEnv("BROKER_CONFIG_GLOBAL_MAX_SIZE", "50000").withEnv("BROKER_CONFIG_MAX_SIZE_BYTES", "50000") + .withEnv("BROKER_CONFIG_MAX_DISK_USAGE", "100"); + } + + @NotNull + public String getContainerHost(GenericContainer container, Integer port) { + return container.getContainerIpAddress() + ":" + container.getMappedPort(port); + } + + @NotNull + public String getContainerHost(GenericContainer container) { + return container.getContainerIpAddress() + ":" + container.getFirstMappedPort(); + } + + public void createAndStartDockerContainers() throws IOException, InterruptedException { + cloneIngressRepoAndUnzip(); + cloneInsightsRbacRepo_UnzipAndConfigure(); + + activemq = getActiveMQContainer(); + activemq.start(); + + kie_server = getKIEContainer(); + kie_server.start(); + + postgreSQL = getPostgreSQLContainer(); + postgreSQL.start(); + + localstack = getLocalstackContainer(); + localstack.start(); + + Network network = Network.newNetwork(); + + minio = getMinioContainer(network); + minio.start(); + + Thread.sleep(5000); + createbuckets = getMinioCreateBucketsContainer(network, minio); + createbuckets.start(); + + kafka = getKafkaContainer(network); + kafka.start(); + + ingress = getIngressContainer(network); + ingress.start(); + + Network rbacNetwork = Network.newNetwork(); + rbacPostgreSQL = getRBACPostgreSQLContainer(rbacNetwork); + rbacPostgreSQL.start(); + + rbacServer = getRBACServerContainer(rbacNetwork); + rbacServer.start(); + + Thread.sleep(5000); + importProjectIntoKIE(); + } + + private void cloneIngressRepoAndUnzip() throws IOException { + // downloading, unzipping, renaming + String ingressRepoZipURL = "https://github.com/RedHatInsights/insights-ingress-go/archive/" + ingressCommitHash + + ".zip"; + File compressedFile = new File("src/test/resources/ingressRepo.zip"); + FileUtils.copyURLToFile(new URL(ingressRepoZipURL), compressedFile, 1000, 10000); + unzipFile(compressedFile, "src/test/resources"); + + // we rename the directory because we had issues with Docker and the long folder + FileUtils.moveDirectory(new File("src/test/resources/insights-ingress-go-" + ingressCommitHash), + new File("src/test/resources/insights-ingress-go")); + } + + private void cloneInsightsRbacRepo_UnzipAndConfigure() throws IOException { + // downloading, unzipping, renaming + String insightsRbacRepoZipURL = "https://github.com/RedHatInsights/insights-rbac/archive/" + + insightsRbacCommitHash + ".zip"; + File compressedFile = new File("src/test/resources/insightsRbacRepo.zip"); + FileUtils.copyURLToFile(new URL(insightsRbacRepoZipURL), compressedFile, 1000, 10000); + unzipFile(compressedFile, "src/test/resources"); + + // we rename the directory because we had issues with Docker and the long folder + FileUtils.moveDirectory(new File("src/test/resources/insights-rbac-" + insightsRbacCommitHash), + new File("src/test/resources/insights-rbac")); + + // Use custom Dockerfile + FileUtils.copyFile(new File("src/test/resources/insightsRbac_Dockerfile"), + new File("src/test/resources/insights-rbac/insightsRbac_Dockerfile")); + + // Configure default system roles for application=migration-analytics + FileUtils.copyFile(new File("src/test/resources/insightsRbac_roleDefinitions.json"), + new File("src/test/resources/insights-rbac/rbac/management/role/definitions/migration-analytics.json")); + } + + private void unzipFile(File file, String outputDir) throws IOException { + java.util.zip.ZipFile zipFile = new ZipFile(file); + try { + Enumeration entries = zipFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); + File entryDestination = new File(outputDir, entry.getName()); + if (entry.isDirectory()) { + entryDestination.mkdirs(); + } else { + entryDestination.getParentFile().mkdirs(); + InputStream in = zipFile.getInputStream(entry); + OutputStream out = new FileOutputStream(entryDestination); + IOUtils.copy(in, out); + in.close(); + out.close(); + } + } + } finally { + zipFile.close(); + } + } + + public String getHostForKie() { + return kie_server.getContainerIpAddress() + ":" + kie_server.getFirstMappedPort(); + } + + private void importProjectIntoKIE() throws InterruptedException, IOException { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.setCacheControl("no-cache"); + headers.set("Authorization", "Basic YWRtaW46YWRtaW4="); // admin:admin + + String kieRestURL = "http://" + getHostForKie() + "/kie-server/services/rest/"; + + // KIE Container Creation + HttpHeaders kieheaders = new HttpHeaders(); + kieheaders.setContentType(MediaType.APPLICATION_JSON); + kieheaders.set("Authorization", "Basic a2llc2VydmVyOmtpZXNlcnZlcjEh"); + kieheaders.setCacheControl("no-cache"); + String kieContainerBody = "{\"container-id\" : \"xavier-analytics_0.0.1-SNAPSHOT\",\"release-id\" : {\"group-id\" : \"org.jboss.xavier\",\"artifact-id\" : \"xavier-analytics\",\"version\" : \"0.0.1-SNAPSHOT\" } }"; + try { + new RestTemplate().exchange(kieRestURL + "server/containers/xavier-analytics_0.0.1-SNAPSHOT", + HttpMethod.PUT, new HttpEntity<>(kieContainerBody, kieheaders), String.class); + } catch (RestClientException e) { + e.printStackTrace(); + } + } + + public GenericContainer getKie_server() { + return kie_server; + } + + public PostgreSQLContainer getPostgreSQL() { + return postgreSQL; + } + + public LocalStackContainer getLocalstack() { + return localstack; + } + + public GenericContainer getActivemq() { + return activemq; + } + + public GenericContainer getMinio() { + return minio; + } + + public GenericContainer getCreatebuckets() { + return createbuckets; + } + + public KafkaContainer getKafka() { + return kafka; + } + + public GenericContainer getIngress() { + return ingress; + } + + public GenericContainer getRbacPostgreSQL() { + return rbacPostgreSQL; + } + + public GenericContainer getRbacServer() { + return rbacServer; + } + +} \ No newline at end of file diff --git a/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestMappingTest.java b/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestMappingTest.java index 38535ead..035396fd 100644 --- a/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestMappingTest.java +++ b/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestMappingTest.java @@ -15,6 +15,9 @@ import org.springframework.http.HttpMethod; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.any; + + @SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -51,7 +54,7 @@ public void xmlRouteBuilder_RestMapping_ShouldCallFindFlagAssessment_FindAll() t restTemplate.exchange(camel_context + "mappings/flag-assessment", HttpMethod.GET, entity, String.class); //Then - verify(flagAssessmentService).findAll(); + verify(flagAssessmentService).findAll(any(), any()); camelContext.stop(); } From 7d56e6c611a84bea54bb508293b72df768568a15 Mon Sep 17 00:00:00 2001 From: jonathan Date: Fri, 29 May 2020 18:50:43 +0200 Subject: [PATCH 02/26] cleaned changes from other branch --- .../integrations/jpa/service/ApiService.java | 67 ------------------- .../jpa/service/FlagAssessmentService.java | 13 ++-- .../route/RouteBuilderExceptionHandler.java | 3 +- src/main/resources/application.properties | 2 - src/main/resources/spring/camel-context.xml | 7 +- .../route/XmlRoutes_RestMappingTest.java | 5 +- 6 files changed, 8 insertions(+), 89 deletions(-) delete mode 100644 src/main/java/org/jboss/xavier/integrations/jpa/service/ApiService.java diff --git a/src/main/java/org/jboss/xavier/integrations/jpa/service/ApiService.java b/src/main/java/org/jboss/xavier/integrations/jpa/service/ApiService.java deleted file mode 100644 index 9cf68261..00000000 --- a/src/main/java/org/jboss/xavier/integrations/jpa/service/ApiService.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.jboss.xavier.integrations.jpa.service; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import javax.inject.Named; - -import org.apache.camel.Exchange; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.data.domain.Page; -import org.springframework.stereotype.Component; - -@Component -public class ApiService { - @Value("${camel.component.servlet.mapping.context-path}") - String contextPath; - - /* - {"data":[ - {"flag":"flag","osName":"osName","flagLabel":"flaglabel1","assessment":"assessment1"}, - {"flag":"flag2","osName":"osName2","flagLabel":"flaglabel2","assessment":"assessment2"}, - {"flag":"flag3","osName":"osName3","flagLabel":"flaglabel3","assessment":"assessment3"}, - {"flag":"flag4","osName":"osName4","flagLabel":"flaglabel4","assessment":"assessment4"}], - "meta":{"offset":0,"count":0,"limit":10}, - "links":{"last":"/api/xavier//api/xavier/mappings/flag-assessment","first":"/api/xavier//api/xavier/mappings/flag-assessment"}} - */ - - public Map mapToCustomPage(Exchange exchange) { - Page page = (Page) exchange.getIn().getBody(); - Map result = new HashMap<>(); - - Map meta = new HashMap<>(); - meta.put("count", page.getTotalElements()); - meta.put("limit", page.getSize()); - meta.put("offset", page.getNumber()); - result.put("meta", meta); - - result.put("data", page.getContent()); - - Map links = new HashMap<>(); - links.put("first", getURLLink(exchange.getIn().getHeader(Exchange.HTTP_URI, String.class), 0, page.getSize())); - links.put("last", getURLLink(exchange.getIn().getHeader(Exchange.HTTP_URI, String.class), page.getTotalPages() -1, page.getSize())); - result.put("links", links); - - return result; - } - - private String getURLLink(String url, int offset, int limit) { - int indexOfQuestion = url.indexOf("?"); - - if (indexOfQuestion >= 0) { - String query = url.substring(indexOfQuestion); - String urlNoParams = url.substring(0, indexOfQuestion); - String params = Arrays.asList(query.split("&")).stream().filter(e -> !e.startsWith("limit") && !e.startsWith("offset")).collect(Collectors.joining()); - url = urlNoParams + ((params.isEmpty()) ? "?" : "?" + params + "&"); - } else { - url = url + "?"; - } - url = url + "limit=" + limit; - url = url + "&offset=" + offset; - - return url; - } -} \ No newline at end of file diff --git a/src/main/java/org/jboss/xavier/integrations/jpa/service/FlagAssessmentService.java b/src/main/java/org/jboss/xavier/integrations/jpa/service/FlagAssessmentService.java index c7c6af31..83ebf019 100644 --- a/src/main/java/org/jboss/xavier/integrations/jpa/service/FlagAssessmentService.java +++ b/src/main/java/org/jboss/xavier/integrations/jpa/service/FlagAssessmentService.java @@ -1,24 +1,21 @@ package org.jboss.xavier.integrations.jpa.service; -import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation; import org.jboss.xavier.analytics.pojo.output.workload.summary.FlagAssessmentModel; import org.jboss.xavier.integrations.jpa.repository.FlagAssessmentRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; +import java.util.List; + @Component public class FlagAssessmentService { @Autowired FlagAssessmentRepository flagAssessmentRepository; - public Page findAll(int page, int size) { - Pageable pageable = new PageRequest(page, size); - Page result = flagAssessmentRepository.findAll(pageable); - return result; + public List findAll() + { + return flagAssessmentRepository.findAll(); } } diff --git a/src/main/java/org/jboss/xavier/integrations/route/RouteBuilderExceptionHandler.java b/src/main/java/org/jboss/xavier/integrations/route/RouteBuilderExceptionHandler.java index 34c3d006..e6bfd294 100644 --- a/src/main/java/org/jboss/xavier/integrations/route/RouteBuilderExceptionHandler.java +++ b/src/main/java/org/jboss/xavier/integrations/route/RouteBuilderExceptionHandler.java @@ -36,11 +36,10 @@ public void configure() throws Exception { } public void markAnalysisAsFailed(Exchange e) { - System.out.println("++++++++++++++ EXCEPTION : " + e); String analysisId = ""; try { analysisId = e.getIn().getHeader(ANALYSIS_ID, "", String.class); - if (analysisId.isEmpty() && e.getIn().getHeader(MA_METADATA, Map.class) != null) { + if (analysisId.isEmpty()) { analysisId = (String) e.getIn().getHeader(MA_METADATA, Map.class).get(ANALYSIS_ID); } if (StringUtils.isNotEmpty(analysisId)) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3bd466cf..4c60edd8 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -62,8 +62,6 @@ insights.upload.accountnumber=123454 # tracing enabled? camel.springboot.tracing=false -pagination.defaults.flag-assessment.limit=100 - # REST Endpoints configuration api.version = v1.0 camel.component.servlet.mapping.context-path=/api/xavier/${api.version}/* diff --git a/src/main/resources/spring/camel-context.xml b/src/main/resources/spring/camel-context.xml index f03b0df4..862eb5ff 100644 --- a/src/main/resources/spring/camel-context.xml +++ b/src/main/resources/spring/camel-context.xml @@ -68,8 +68,6 @@ Mapping REST API - - mappings @@ -77,10 +75,7 @@ - - - - + diff --git a/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestMappingTest.java b/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestMappingTest.java index 035396fd..38535ead 100644 --- a/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestMappingTest.java +++ b/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestMappingTest.java @@ -15,9 +15,6 @@ import org.springframework.http.HttpMethod; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.any; - - @SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -54,7 +51,7 @@ public void xmlRouteBuilder_RestMapping_ShouldCallFindFlagAssessment_FindAll() t restTemplate.exchange(camel_context + "mappings/flag-assessment", HttpMethod.GET, entity, String.class); //Then - verify(flagAssessmentService).findAll(any(), any()); + verify(flagAssessmentService).findAll(); camelContext.stop(); } From 9c63a39af98be3de8566ac7b9adbebd9c4ee080b Mon Sep 17 00:00:00 2001 From: jonathan Date: Fri, 29 May 2020 18:55:04 +0200 Subject: [PATCH 03/26] refactor-end2end deleted @Ignore on endtoend tests --- .../xavier/integrations/EndToEndTest.java | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 83d1c2b6..4422a864 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -326,14 +326,7 @@ public void configure() { } } - @After - public void finishTest() throws Exception { - //camelContext.stop(); - } - @Test - @Ignore - public void whenRegularTestShouldAnswerInTime() throws Exception { // Start the camel route as if the UI was sending the file to the Camel Rest Upload route assertThat(getStorageObjectsSize()).isEqualTo(0); @@ -424,8 +417,6 @@ public void whenRegularTestShouldAnswerInTime() throws Exception { } @Test - @Ignore - public void whenPerformanceTestShouldAnswerInTime() throws Exception { analysisNum++; // Performance test @@ -436,8 +427,6 @@ public void whenPerformanceTestShouldAnswerInTime() throws Exception { } @Test - @Ignore - public void whenFileWithVMWithoutHostShouldAddVMToWorkloadInventory() throws Exception { analysisNum++; // Test with a file with VM without Host @@ -453,8 +442,6 @@ public void whenFileWithVMWithoutHostShouldAddVMToWorkloadInventory() throws Exc } @Test - @Ignore - public void whenFileWithHostWithoutClusterShouldAddVMToWorkloadInventory() throws Exception { analysisNum++; @@ -473,8 +460,6 @@ public void whenFileWithHostWithoutClusterShouldAddVMToWorkloadInventory() throw } @Test - @Ignore - public void whenFileWithWrongCPUCoresPerSocketShouldNotFailAndFallbackTheValue() throws Exception { analysisNum++; @@ -493,7 +478,6 @@ public void whenFileWithWrongCPUCoresPerSocketShouldNotFailAndFallbackTheValue() } @Test - @Ignore public void whenFileWith0CPUCoresPerSocketShouldNotFailAndFallbackTheValue() throws Exception { analysisNum++; // Test with a file with 0 CPU cores per socket @@ -511,8 +495,6 @@ public void whenFileWith0CPUCoresPerSocketShouldNotFailAndFallbackTheValue() thr } @Test - @Ignore - public void whenFileWithUsedDiskStorageShouldNotFailAndFallbackTheValue() throws Exception { analysisNum++; // Test with a file with VM.used_disk_storage @@ -558,7 +540,6 @@ public void whenFileWithUsedDiskStorageShouldNotFailAndFallbackTheValue() throws } @Test - @Ignore public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { // Ultra Performance test logger.info("+++++++ Ultra Performance Test ++++++"); @@ -568,7 +549,6 @@ public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { } @Test - @Ignore public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exception { // Stress test // We load 3 times a BIG file ( 8 Mb ) and 2 times a small file ( 316 Kb ) @@ -602,12 +582,11 @@ public void testCustomPage() { @Test - @Ignore public void whenDeleteReportShouldRemoveFileInS3() throws Exception { // Testing the deletion of a file in S3 logger.info("++++++++ Delete report test +++++"); int s3ObjectsBefore = getStorageObjectsSize(); - + // we upload a file to be sure there's one report to delete, as it could be that this test is executed the first new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); analysisNum++; From 5dcecab2b1e66178938afe9f7f91d878140d76a1 Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 4 Jun 2020 12:23:58 +0200 Subject: [PATCH 04/26] refactor-end2end organized imports --- .../xavier/integrations/TestContainersInfrastructure.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java index 6a68b013..157afbd9 100644 --- a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java +++ b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java @@ -1,5 +1,7 @@ package org.jboss.xavier.integrations; +import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -15,12 +17,8 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.junit.ClassRule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.boot.test.util.EnvironmentTestUtils; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -36,8 +34,6 @@ import org.testcontainers.images.builder.ImageFromDockerfile; import org.testcontainers.utility.MountableFile; -import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3; - public class TestContainersInfrastructure { private Logger logger = LoggerFactory.getLogger(TestContainersInfrastructure.class); From d9b2242815e78eb512c1b282c06ec8bc8bb3b7f7 Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 4 Jun 2020 15:25:27 +0200 Subject: [PATCH 05/26] refactor-end2end Fixed test --- .../xavier/integrations/EndToEndTest.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 7ab3d447..82338f97 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -571,29 +571,24 @@ public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exce // To process the first small file it should take 10 seconds // To process the second small file it should take 7 minutes of the first bunch of big files plus 10 seconds of the small file logger.info("+++++++ Stress Test ++++++"); + int firstupload = ++analysisNum; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); + analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0.json", "application/json"), String.class); + analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); + analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0.json", "application/json"), String.class); // We will check for time we retrieve the third file uploaded to see previous ones are not affecting - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum + 2), timeoutMilliseconds_SmallFileSummaryReport)).isEqualTo(8); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum + 1), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum + 3), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo( numberVMsExpected_InBigFile); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 1), timeoutMilliseconds_SmallFileSummaryReport)).isEqualTo(8); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 2), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo( numberVMsExpected_InBigFile); int timeoutMilliseconds_secondSmallFile = timeoutMilliseconds_UltraPerformaceTest + timeoutMilliseconds_SmallFileSummaryReport; - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum + 4), timeoutMilliseconds_secondSmallFile)).isEqualTo( 8); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 3), timeoutMilliseconds_secondSmallFile)).isEqualTo( 8); } - @Test - public void testCustomPage() { - ResponseEntity response = new RestTemplate().exchange(getBaseURLAPIPath() + "/mappings/flag-assessment?limit=10&offset=0", HttpMethod.GET, getRequestEntity(), String.class); - assertThat(response.getBody()).contains("\"first\":\"/api/xavier/v1.0/mappings/flag-assessment?limit=10&offset=0\""); - assertThat(response.getBody()).contains("\"last\":\"/api/xavier/v1.0/mappings/flag-assessment?limit=10&offset=0\""); - logger.info("RESPONSE PAGINATION : " + response.getBody()); - } - - @Test public void whenDeleteReportShouldRemoveFileInS3() throws Exception { // Testing the deletion of a file in S3 From b301ca71eacb403bd6f15a0485984de6d5063abd Mon Sep 17 00:00:00 2001 From: jonathan Date: Fri, 5 Jun 2020 17:33:13 +0200 Subject: [PATCH 06/26] refactor_endtoend fix tests --- .../integrations/route/XmlRoutes_RestReportTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestReportTest.java b/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestReportTest.java index 165c73b0..5d6acf4e 100644 --- a/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestReportTest.java +++ b/src/test/java/org/jboss/xavier/integrations/route/XmlRoutes_RestReportTest.java @@ -99,8 +99,8 @@ public void xmlRouteBuilder_RestReport_NoParamGiven_ShouldCallFindReports() thro //Then verify(analysisService).findAllByOwner("mrizzi@redhat.com", 0, 10); assertThat(response).isNotNull(); - assertThat(response.getBody()).contains("\"content\":[]"); - assertThat(response.getBody()).contains("\"size\":10"); + assertThat(response.getBody()).contains("\"data\":[]"); + assertThat(response.getBody()).contains("\"limit\":10"); camelContext.stop(); } @@ -148,8 +148,8 @@ public void xmlRouteBuilder_RestReport_PageAndSizeParamGiven_ShouldCallFindRepor //Then verify(analysisService).findAllByOwner("mrizzi@redhat.com", page, size); assertThat(response).isNotNull(); - assertThat(response.getBody()).contains("\"content\":[]"); - assertThat(response.getBody()).contains("\"size\":3"); + assertThat(response.getBody()).contains("\"data\":[]"); + assertThat(response.getBody()).contains("\"limit\":3"); camelContext.stop(); } @@ -180,8 +180,8 @@ public void xmlRouteBuilder_RestReport_FilterTextPageAndSizeParamGiven_ShouldCal //Then verify(analysisService).findByOwnerAndReportName("mrizzi@redhat.com", filterText, page, size); assertThat(response).isNotNull(); - assertThat(response.getBody()).contains("\"content\":[]"); - assertThat(response.getBody()).contains("\"size\":3"); + assertThat(response.getBody()).contains("\"data\":[]"); + assertThat(response.getBody()).contains("\"limit\":3"); camelContext.stop(); } From b0b38eb4255c344d2df63f71abc143687dc37ad6 Mon Sep 17 00:00:00 2001 From: jonathan Date: Mon, 15 Jun 2020 12:20:31 +0200 Subject: [PATCH 07/26] refactor Fixed asserts on S3Objects --- .../xavier/integrations/EndToEndTest.java | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 17ef2fd5..46786498 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -67,7 +67,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; import org.springframework.core.ParameterizedTypeReference; -import org.springframework.hateoas.PagedResources; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -134,17 +133,15 @@ public class EndToEndTest { @Inject AmazonS3 amazonS3; - + @Inject private ObjectMapper objectMapper; @Value("${camel.component.servlet.mapping.context-path}") private String basePath; - private static int analysisNum; - @Before public void setDefaults() { long id = 0L; @@ -275,6 +272,7 @@ public void setDefaults() { fgId4.setOsName("osName4"); flagAssessmentModel4.setId(fgId4); flagAssessmentRepository.save(Arrays.asList(flagAssessmentModel1, flagAssessmentModel2, flagAssessmentModel3, flagAssessmentModel4)); + } @BeforeClass @@ -322,6 +320,8 @@ public void configure() { } }); + setDefaults(); + // this one should give 2 pages ResponseEntity> responseFlaggAssessment = new RestTemplate().exchange(getBaseURLAPIPath() + "/mappings/flag-assessment?limit=2&offset=0", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); assertThat(responseFlaggAssessment.getBody().getData().size()).isEqualTo(2); @@ -339,7 +339,8 @@ public void configure() { @Test public void whenRegularTestShouldAnswerInTime() throws Exception { // Start the camel route as if the UI was sending the file to the Camel Rest Upload route - assertThat(getStorageObjectsSize()).isEqualTo(0); + int s3Objects = getStorageObjectsSize(); + assertThat(s3Objects).isEqualTo(analysisNum); logger.info("+++++++ Regular Test ++++++"); analysisNum++; @@ -356,7 +357,7 @@ public void whenRegularTestShouldAnswerInTime() throws Exception { }); // Check S3 - assertThat(getStorageObjectsSize()).isEqualTo(1); + assertThat(getStorageObjectsSize()).isEqualTo(s3Objects+1); // Check DB for initialCostSavingsReport with concrete values InitialSavingsEstimationReportModel initialCostSavingsReportDB = initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", 1L); @@ -434,13 +435,8 @@ public void whenRegularTestShouldAnswerInTime() throws Exception { .isThrownBy(() -> new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", 9999), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {})) .matches(e -> e.getStatusCode().equals(HttpStatus.NOT_FOUND)) .matches(e -> e.getResponseBodyAsString().equalsIgnoreCase("Report not found")); - } - - @Test - public void whenPerformanceTestShouldAnswerInTime() throws Exception { - analysisNum++; - // OpenAPI Pagination Response test + // OpenAPI Pagination Response test ResponseEntity> workloadInventoryReportPagination = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); assertThat(workloadInventoryReportPagination.getBody().getMeta().getCount()).isEqualTo(14); assertThat(workloadInventoryReportPagination.getBody().getMeta().getOffset()).isEqualTo(0); @@ -460,6 +456,11 @@ public void whenPerformanceTestShouldAnswerInTime() throws Exception { assertThat(workloadInventoryReportPagination.getBody().getLinks().getLast()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=12", analysisNum)); assertThat(workloadInventoryReportPagination.getBody().getLinks().getPrev()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=4", analysisNum)); assertThat(workloadInventoryReportPagination.getBody().getLinks().getNext()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=12", analysisNum)); + } + + @Test + public void whenPerformanceTestShouldAnswerInTime() throws Exception { + analysisNum++; // Performance test logger.info("+++++++ Performance Test ++++++"); @@ -477,20 +478,15 @@ public void whenFileWithVMWithoutHostShouldAddVMToWorkloadInventory() throws Exc new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_without_host.json", "application/json"), String.class); assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum),timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); - ResponseEntity> workloadInventoryReport_file_vm_without_host = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?size=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); - assertThat(workloadInventoryReport_file_vm_without_host.getBody().getContent().size()).isEqualTo(8); - assertThat(workloadInventoryReport_file_vm_without_host.getBody().getContent().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(2); - assertThat(workloadInventoryReport_file_vm_without_host.getBody().getContent().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(6); + ResponseEntity> workloadInventoryReport_file_vm_without_host = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?size=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().size()).isEqualTo(8); + assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(2); + assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(6); } @Test public void whenFileWithHostWithoutClusterShouldAddVMToWorkloadInventory() throws Exception { analysisNum++; - ResponseEntity> workloadInventoryReport_file_vm_without_host = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); - assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().size()).isEqualTo(8); - assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(2); - assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(6); - // Test with a file with Host without Cluster logger.info("+++++++ Test with a file with Host without Cluster ++++++"); new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-host_without_cluster.json", "application/json"), String.class); @@ -631,9 +627,10 @@ public void whenDeleteReportShouldRemoveFileInS3() throws Exception { new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); analysisNum++; Thread.sleep(5000); + assertThat(getStorageObjectsSize()).isEqualTo(s3ObjectsBefore+1); + ResponseEntity stringEntity = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d", analysisNum), HttpMethod.DELETE, getRequestEntity(), new ParameterizedTypeReference() {}); assertThat(stringEntity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); - assertThat(initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", Long.valueOf(analysisNum))).isNull(); assertThat(getStorageObjectsSize()).isEqualTo(s3ObjectsBefore); } From dbc18270984b4dd004a5924e1e0f88d17a7374c2 Mon Sep 17 00:00:00 2001 From: jonathan Date: Mon, 29 Jun 2020 18:13:52 +0200 Subject: [PATCH 08/26] refactor-end2end Added awaitility code to be sure the resources are created --- .../xavier/integrations/EndToEndTest.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 46612c93..8d919318 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -356,11 +356,11 @@ public void configure() { @Test public void whenRegularTestShouldAnswerInTime() throws Exception { + logger.info("+++++++ Regular Test ++++++"); // Start the camel route as if the UI was sending the file to the Camel Rest Upload route int s3Objects = getStorageObjectsSize(); assertThat(s3Objects).isEqualTo(analysisNum); - logger.info("+++++++ Regular Test ++++++"); analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20190912-demolab_withSSA.tar.gz", "application/zip"), String.class); @@ -371,11 +371,16 @@ public void whenRegularTestShouldAnswerInTime() throws Exception { .until( () -> { // Check database for the ICSR to be created List all = initialSavingsEstimationReportRepository.findAll(); - return all != null && !all.isEmpty(); + return all != null && all.size() == analysisNum; }); // Check S3 - assertThat(getStorageObjectsSize()).isEqualTo(s3Objects+1); + await() + .atMost(5000, TimeUnit.MILLISECONDS) + .with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) + .until( () -> { + return (getStorageObjectsSize() == s3Objects + 1); + }); // Check DB for initialCostSavingsReport with concrete values InitialSavingsEstimationReportModel initialCostSavingsReportDB = initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", 1L); @@ -477,7 +482,7 @@ public void whenRegularTestShouldAnswerInTime() throws Exception { // Testing that limit and offset params are really taken into consideration ResponseEntity> responseAnalysisModel = new RestTemplate().exchange(getBaseURLAPIPath() + "/report?limit=2&offset=0", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); - assertThat(responseAnalysisModel.getBody().getData().size()).isEqualTo(2); + assertThat(responseAnalysisModel.getBody().getData().size()).isLessThanOrEqualTo(2); } @Test @@ -629,7 +634,6 @@ public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exce new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0.json", "application/json"), String.class); - // We will check for time we retrieve the third file uploaded to see previous ones are not affecting assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 1), timeoutMilliseconds_SmallFileSummaryReport)).isEqualTo(8); assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); @@ -648,8 +652,12 @@ public void whenDeleteReportShouldRemoveFileInS3() throws Exception { // we upload a file to be sure there's one report to delete, as it could be that this test is executed the first new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); analysisNum++; - Thread.sleep(5000); - assertThat(getStorageObjectsSize()).isEqualTo(s3ObjectsBefore+1); + await() + .atMost(5000, TimeUnit.MILLISECONDS) + .with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) + .until( () -> { + return getStorageObjectsSize() == s3ObjectsBefore + 1; + }); ResponseEntity stringEntity = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d", analysisNum), HttpMethod.DELETE, getRequestEntity(), new ParameterizedTypeReference() {}); assertThat(stringEntity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); From 8c24dd445cb44914087381a2a3b7b70b5c3e98b4 Mon Sep 17 00:00:00 2001 From: jonathan Date: Mon, 29 Jun 2020 21:11:52 +0200 Subject: [PATCH 09/26] refactor-end2end starting and stopping camel context --- .../xavier/integrations/EndToEndTest.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 8d919318..6affcedf 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -147,6 +147,8 @@ public class EndToEndTest { private static int analysisNum; + private static boolean firstTime = true; + public void setDefaults() { long id = 0L; @@ -305,13 +307,14 @@ private void assertHttpClientError(String url, HttpMethod method, HttpStatus sta @Before public void initCamel() throws Exception { - if (camelContext.getStatus() != ServiceStatus.Started ) { - Thread.sleep(2000); + Thread.sleep(2000); - // given - camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "5000"); - camelContext.start(); + // given + camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "5000"); + camelContext.start(); + if (firstTime) { + firstTime = false; camelContext.getRouteDefinition("download-file").adviceWith(camelContext, new AdviceWithRouteBuilder() { @Override public void configure() { @@ -354,6 +357,11 @@ public void configure() { } } + @After + public void closeCamel() throws Exception { + camelContext.stop(); + } + @Test public void whenRegularTestShouldAnswerInTime() throws Exception { logger.info("+++++++ Regular Test ++++++"); From cc375d41e710d3252d1e6b960baff3027f0883a3 Mon Sep 17 00:00:00 2001 From: jonathan Date: Tue, 30 Jun 2020 21:11:40 +0200 Subject: [PATCH 10/26] endtoend refactor - fixed RBAC tests on the duplicated SpringBootTest annotation numAnalysis is set at the current number of analisys in the DB --- .../xavier/integrations/EndToEndTest.java | 50 +++++++++++++++---- ...uilder_DirectCheckRbacPermissionsTest.java | 1 - ...der_DirectFetchRbacAndProcessDataTest.java | 1 - ...Builder_DirectFetchRbacUserAccessTest.java | 3 -- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 6affcedf..d5da2a8a 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -148,6 +148,7 @@ public class EndToEndTest { private static int analysisNum; private static boolean firstTime = true; + private static int testsExecuted = 0; public void setDefaults() { long id = 0L; @@ -304,17 +305,17 @@ private void assertHttpClientError(String url, HttpMethod method, HttpStatus sta .isThrownBy(() -> new RestTemplate().exchange(getBaseURLAPIPath() + url, method, getRequestEntity(), String.class)) .matches(e -> e.getStatusCode().equals(status)); } - + @Before public void initCamel() throws Exception { - Thread.sleep(2000); - - // given - camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "5000"); - camelContext.start(); - if (firstTime) { + logger.info("STARTING CAMEL >>>>>>>>"); firstTime = false; + Thread.sleep(2000); //TODO use Awaitility to check a particular container + + // given + camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "5000"); + camelContext.start(); camelContext.getRouteDefinition("download-file").adviceWith(camelContext, new AdviceWithRouteBuilder() { @Override public void configure() { @@ -336,6 +337,10 @@ public void configure() { setDefaults(); + ResponseEntity> responseAnalysisModel = new RestTemplate().exchange(getBaseURLAPIPath() + "/report?limit=1000&offset=0", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + analysisNum = responseAnalysisModel.getBody().getData().size(); + logger.info("***** Number of reports upload until now : " + analysisNum); + // Checking errors are correctly treated assertHttpClientError("/report/99999", HttpMethod.GET,HttpStatus.NOT_FOUND); assertHttpClientError("/report/99999", HttpMethod.DELETE, HttpStatus.NOT_FOUND); @@ -351,15 +356,21 @@ public void configure() { assertThat(responseFlaggAssessmentHighLimit.getBody().getData().size()).isEqualTo(4); // 1. Check user has firstTime + logger.info("****** Checking First Time User **********"); ResponseEntity userEntity = new RestTemplate().exchange(getBaseURLAPIPath() + "/user", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); assertThat(userEntity.getBody().isFirstTimeCreatingReports()).isTrue(); - logger.info("****** Checking First Time User **********"); } } @After public void closeCamel() throws Exception { - camelContext.stop(); + testsExecuted++; + logger.info("After test method ....................."); + + if (testsExecuted == 10) { + camelContext.stop(); + logger.info("...................CLOSING CAMEL CONTEXT ............."); + } } @Test @@ -491,6 +502,8 @@ public void whenRegularTestShouldAnswerInTime() throws Exception { // Testing that limit and offset params are really taken into consideration ResponseEntity> responseAnalysisModel = new RestTemplate().exchange(getBaseURLAPIPath() + "/report?limit=2&offset=0", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); assertThat(responseAnalysisModel.getBody().getData().size()).isLessThanOrEqualTo(2); + logger.info("------- End Regular Test -------"); + } @Test @@ -502,6 +515,8 @@ public void whenPerformanceTestShouldAnswerInTime() throws Exception { new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20190829-16128-uq17dx.tar.gz", "application/zip"), String.class); assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_PerformaceTest)).isEqualTo(142); + logger.info("------- End Performance Test ------"); + } @Test @@ -517,6 +532,8 @@ public void whenFileWithVMWithoutHostShouldAddVMToWorkloadInventory() throws Exc assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().size()).isEqualTo(8); assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(2); assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(6); + logger.info("------- End file with VM without Host Test ------"); + } @Test @@ -534,6 +551,8 @@ public void whenFileWithHostWithoutClusterShouldAddVMToWorkloadInventory() throw assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getData().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(3); // Right VMs assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getData().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(5); + logger.info("------- End file with Host without Cluster Test ------"); + } @Test @@ -551,6 +570,8 @@ public void whenFileWithWrongCPUCoresPerSocketShouldNotFailAndFallbackTheValue() assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getData().stream().filter(e -> e.getCpuCores() == null).count()).isEqualTo(0); assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getData().stream().filter(e -> e.getCpuCores() != null).count()).isEqualTo(5); assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getData().size()).isEqualTo(5); + logger.info("------- End Wrong CPU cores per socket Test ------"); + } @Test @@ -558,7 +579,7 @@ public void whenFileWith0CPUCoresPerSocketShouldNotFailAndFallbackTheValue() thr analysisNum++; // Test with a file with 0 CPU cores per socket logger.info("+++++++ Test with a file with 0 CPU cores per socket ++++++"); - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_0_cores.json", "application/json"), String.class); + ResponseEntity response = new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_0_cores.json", "application/json"), String.class); assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); ResponseEntity initialCostSavingsReport_zero_cpu_cores = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); @@ -568,6 +589,8 @@ public void whenFileWith0CPUCoresPerSocketShouldNotFailAndFallbackTheValue() thr assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getData().stream().filter(e -> e.getCpuCores() == null).count()).isEqualTo(0); assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getData().stream().filter(e -> e.getCpuCores() != null).count()).isEqualTo(8); assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getData().size()).isEqualTo(8); + logger.info("------- End 0 CPU cores per socket Test ------"); + } @Test @@ -613,6 +636,8 @@ public void whenFileWithUsedDiskStorageShouldNotFailAndFallbackTheValue() throws .usingRecursiveComparison() .ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*", ".*report.*", ".*workloadsDetectedOSTypeModels.*", ".*scanRunModels.*") .isEqualTo(workloadSummaryReport_JavaRuntimesExpected); + logger.info("------- End file with VM.used_disk_storage Test ------"); + } @Test @@ -622,6 +647,8 @@ public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { analysisNum++; new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); + logger.info("------- End Ultra Performance Test ------"); + } @Test @@ -649,6 +676,7 @@ public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exce int timeoutMilliseconds_secondSmallFile = timeoutMilliseconds_UltraPerformaceTest + timeoutMilliseconds_SmallFileSummaryReport; assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 3), timeoutMilliseconds_secondSmallFile)).isEqualTo( 8); + logger.info("------- End Stress Test ------"); } @Test @@ -671,6 +699,8 @@ public void whenDeleteReportShouldRemoveFileInS3() throws Exception { assertThat(stringEntity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); assertThat(initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", Long.valueOf(analysisNum))).isNull(); assertThat(getStorageObjectsSize()).isEqualTo(s3ObjectsBefore); + logger.info("--------- End Delete report test -------"); + } private String getBaseURLAPIPath() { diff --git a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java index 947fdd2a..83ed0fc8 100644 --- a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java +++ b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java @@ -13,7 +13,6 @@ import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RBACRouteBuilder_DirectCheckRbacPermissionsTest extends XavierCamelTest { @Value("${camel.component.servlet.mapping.context-path}") diff --git a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacAndProcessDataTest.java b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacAndProcessDataTest.java index bf423481..2fdbe6e0 100644 --- a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacAndProcessDataTest.java +++ b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacAndProcessDataTest.java @@ -21,7 +21,6 @@ import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RBACRouteBuilder_DirectFetchRbacAndProcessDataTest extends XavierCamelTest { @Value("${camel.component.servlet.mapping.context-path}") diff --git a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacUserAccessTest.java b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacUserAccessTest.java index fde78611..41ffad84 100644 --- a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacUserAccessTest.java +++ b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacUserAccessTest.java @@ -7,13 +7,11 @@ import org.apache.camel.component.http4.HttpMethods; import org.apache.http.HttpHeaders; import org.apache.http.entity.ContentType; -import org.jboss.xavier.Application; import org.jboss.xavier.integrations.route.XavierCamelTest; import org.jboss.xavier.integrations.util.TestUtil; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.Arrays; @@ -22,7 +20,6 @@ import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RBACRouteBuilder_DirectFetchRbacUserAccessTest extends XavierCamelTest { @Value("${camel.component.servlet.mapping.context-path}") From 75a0d636c942c529e271034fe61f6a7ad9983135 Mon Sep 17 00:00:00 2001 From: jonathan Date: Fri, 10 Jul 2020 11:45:12 +0200 Subject: [PATCH 11/26] refactor-endtoend upgraded testcontainers version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 34cc2dd7..12eda621 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ info - 1.12.4 + 1.14.3 From 29d43cbf9a8c1120b2eeec229d28a98b3b69f2c9 Mon Sep 17 00:00:00 2001 From: jonathan Date: Fri, 10 Jul 2020 11:50:18 +0200 Subject: [PATCH 12/26] refactor-end2end restructure of EndToEndTest and creation of a separate class with Docker infrastructure --- .../xavier/integrations/EndToEndTest.java | 717 +++++++++++------- ...ingBootEndToEndTestContextInitializer.java | 41 - .../TestContainersInfrastructure.java | 328 ++++---- 3 files changed, 614 insertions(+), 472 deletions(-) delete mode 100644 src/test/java/org/jboss/xavier/integrations/SpringBootEndToEndTestContextInitializer.java diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index d5da2a8a..c3a398ad 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.awaitility.Awaitility.await; - import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -26,7 +25,6 @@ import org.apache.camel.CamelContext; import org.apache.camel.Exchange; -import org.apache.camel.ServiceStatus; import org.apache.camel.builder.AdviceWithRouteBuilder; import org.apache.camel.test.spring.CamelSpringBootRunner; import org.apache.camel.test.spring.UseAdviceWith; @@ -60,8 +58,6 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; @@ -71,7 +67,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; import org.springframework.core.ParameterizedTypeReference; -import org.springframework.hateoas.PagedResources; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -86,11 +81,11 @@ @RunWith(CamelSpringBootRunner.class) @UseAdviceWith // Disables automatic start of Camel context -@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -@ContextConfiguration(initializers = SpringBootEndToEndTestContextInitializer.class) +@SpringBootTest(classes = { Application.class }, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@ContextConfiguration(initializers = TestContainersInfrastructure.SpringBootInitializerTestContainers.class) @Import(TestConfigurationS3.class) @ActiveProfiles("test") -public class EndToEndTest { +public class EndToEndTest extends TestContainersInfrastructure { private Logger logger = LoggerFactory.getLogger(EndToEndTest.class); @Inject @@ -154,91 +149,80 @@ public void setDefaults() { long id = 0L; // OSInformation - AppIdentifierModel osFamily1 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("RHEL") - .withIdentifier("RHEL") // osFamily field - .withPriority(100) - .build(); - AppIdentifierModel osFamily2 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("Windows Server") + AppIdentifierModel osFamily1 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("RHEL").withIdentifier("RHEL") // osFamily + // field + .withPriority(100).build(); + AppIdentifierModel osFamily2 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("Windows Server") .withIdentifier("Windows Server") // osFamily field - .withPriority(90) - .build(); - AppIdentifierModel osFamily3 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("Windows Other") + .withPriority(90).build(); + AppIdentifierModel osFamily3 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("Windows Other") .withIdentifier("Windows Other") // osFamily field - .withPriority(80) - .build(); - AppIdentifierModel osFamily4 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("SUSE") - .withIdentifier("SUSE") // osFamily field - .withPriority(70) - .build(); - AppIdentifierModel osFamily5 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("CentOS") - .withIdentifier("CentOS") // osFamily field - .withPriority(60) - .build(); - AppIdentifierModel osFamily6 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("Oracle Linux") + .withPriority(80).build(); + AppIdentifierModel osFamily4 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("SUSE").withIdentifier("SUSE") // osFamily + // field + .withPriority(70).build(); + AppIdentifierModel osFamily5 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("CentOS").withIdentifier("CentOS") // osFamily + // field + .withPriority(60).build(); + AppIdentifierModel osFamily6 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("Oracle Linux") .withIdentifier("Oracle Linux") // osFamily field - .withPriority(50) - .build(); - AppIdentifierModel osFamily7 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("Ubuntu") - .withIdentifier("Ubuntu") // osFamily field - .withPriority(40) - .build(); - AppIdentifierModel osFamily8 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("Debian") - .withIdentifier("Debian") // osFamily field - .withPriority(30) - .build(); - AppIdentifierModel osFamily9 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(OSInformationModel.APP_IDENTIFIER) - .withName("Other") - .withIdentifier("Other") // osFamily field - .withPriority(20) - .build(); - appIdentifierRepository.save(Arrays.asList(osFamily1, osFamily2, osFamily3, osFamily4, osFamily5, osFamily6, osFamily7, osFamily8, osFamily9)); + .withPriority(50).build(); + AppIdentifierModel osFamily7 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("Ubuntu").withIdentifier("Ubuntu") // osFamily + // field + .withPriority(40).build(); + AppIdentifierModel osFamily8 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("Debian").withIdentifier("Debian") // osFamily + // field + .withPriority(30).build(); + AppIdentifierModel osFamily9 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(OSInformationModel.APP_IDENTIFIER).withName("Other").withIdentifier("Other") // osFamily + // field + .withPriority(20).build(); + appIdentifierRepository.save(Arrays.asList(osFamily1, osFamily2, osFamily3, osFamily4, osFamily5, osFamily6, + osFamily7, osFamily8, osFamily9)); // JDK Runtimes String oracleVendorName = "Oracle"; - AppIdentifierModel jdkRuntime1 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(WorkloadsJavaRuntimeDetectedModel.APP_IDENTIFIER) - .withName(oracleVendorName) - .withVersion("8") - .withIdentifier("Oracle JDK 8") // Workload name + AppIdentifierModel jdkRuntime1 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(WorkloadsJavaRuntimeDetectedModel.APP_IDENTIFIER).withName(oracleVendorName) + .withVersion("8").withIdentifier("Oracle JDK 8") // Workload name .build(); - AppIdentifierModel jdkRuntime2 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(WorkloadsJavaRuntimeDetectedModel.APP_IDENTIFIER) - .withName(oracleVendorName) - .withVersion("11") - .withIdentifier("Oracle JDK 11") // Workload name + AppIdentifierModel jdkRuntime2 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(WorkloadsJavaRuntimeDetectedModel.APP_IDENTIFIER).withName(oracleVendorName) + .withVersion("11").withIdentifier("Oracle JDK 11") // Workload name .build(); - AppIdentifierModel jdkRuntime3 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(WorkloadsJavaRuntimeDetectedModel.APP_IDENTIFIER) - .withName(oracleVendorName) - .withVersion("13") - .withIdentifier("Oracle JDK 13") // Workload name + AppIdentifierModel jdkRuntime3 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(WorkloadsJavaRuntimeDetectedModel.APP_IDENTIFIER).withName(oracleVendorName) + .withVersion("13").withIdentifier("Oracle JDK 13") // Workload name .build(); appIdentifierRepository.save(Arrays.asList(jdkRuntime1, jdkRuntime2, jdkRuntime3)); // ApplicationPlatforms - AppIdentifierModel applicationPlatform1 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(WorkloadsApplicationPlatformsDetectedModel.APP_IDENTIFIER) - .withName("JBoss EAP") + AppIdentifierModel applicationPlatform1 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(WorkloadsApplicationPlatformsDetectedModel.APP_IDENTIFIER).withName("JBoss EAP") .withIdentifier("Red Hat JBoss EAP") // Workload name .build(); - AppIdentifierModel applicationPlatform2 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(WorkloadsApplicationPlatformsDetectedModel.APP_IDENTIFIER) - .withName("Tomcat") + AppIdentifierModel applicationPlatform2 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(WorkloadsApplicationPlatformsDetectedModel.APP_IDENTIFIER).withName("Tomcat") .withIdentifier("Tomcat") // Workload name .build(); - AppIdentifierModel applicationPlatform3 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(WorkloadsApplicationPlatformsDetectedModel.APP_IDENTIFIER) - .withName("Oracle Weblogic") + AppIdentifierModel applicationPlatform3 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(WorkloadsApplicationPlatformsDetectedModel.APP_IDENTIFIER).withName("Oracle Weblogic") .withIdentifier("Oracle Weblogic") // Workload name .build(); - AppIdentifierModel applicationPlatform4 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id).withGroupName(WorkloadsApplicationPlatformsDetectedModel.APP_IDENTIFIER) - .withName("IBM WebSphere") + AppIdentifierModel applicationPlatform4 = AppIdentifierModel.Builder.anAppIdentifierModel().withId(++id) + .withGroupName(WorkloadsApplicationPlatformsDetectedModel.APP_IDENTIFIER).withName("IBM WebSphere") .withIdentifier("IBM Websphere App Server") // Workload name .build(); - appIdentifierRepository.save(Arrays.asList(applicationPlatform1, applicationPlatform2, applicationPlatform3, applicationPlatform4)); - + appIdentifierRepository.save( + Arrays.asList(applicationPlatform1, applicationPlatform2, applicationPlatform3, applicationPlatform4)); // FLag assessments FlagAssessmentModel flagAssessmentModel1 = new FlagAssessmentModel(); @@ -279,7 +263,8 @@ public void setDefaults() { fgId4.setFlag("flag4"); fgId4.setOsName("osName4"); flagAssessmentModel4.setId(fgId4); - flagAssessmentRepository.save(Arrays.asList(flagAssessmentModel1, flagAssessmentModel2, flagAssessmentModel3, flagAssessmentModel4)); + flagAssessmentRepository.save( + Arrays.asList(flagAssessmentModel1, flagAssessmentModel2, flagAssessmentModel3, flagAssessmentModel4)); } @@ -294,6 +279,17 @@ public static void cleanUp() throws IOException { FileUtils.deleteQuietly(new File("src/test/resources/insightsRbacRepo.zip")); } + @BeforeClass + public static void startContainers() throws IOException, InterruptedException { + createAndStartDockerContainers(); + Thread.sleep(15000); + } + + @AfterClass + public static void stopContainers() { + stopAndDestroyDockerContainers(); + } + private int getStorageObjectsSize() { int s3Size = storageClient.listObjectsV2(new ListObjectsV2Request().withBucketName(bucket)).getKeyCount(); logger.info("S3 Objects : " + s3Size); @@ -301,9 +297,9 @@ private int getStorageObjectsSize() { } private void assertHttpClientError(String url, HttpMethod method, HttpStatus status) { - assertThatExceptionOfType(org.springframework.web.client.HttpClientErrorException.class) - .isThrownBy(() -> new RestTemplate().exchange(getBaseURLAPIPath() + url, method, getRequestEntity(), String.class)) - .matches(e -> e.getStatusCode().equals(status)); + assertThatExceptionOfType(org.springframework.web.client.HttpClientErrorException.class).isThrownBy( + () -> new RestTemplate().exchange(getBaseURLAPIPath() + url, method, getRequestEntity(), String.class)) + .matches(e -> e.getStatusCode().equals(status)); } @Before @@ -311,7 +307,7 @@ public void initCamel() throws Exception { if (firstTime) { logger.info("STARTING CAMEL >>>>>>>>"); firstTime = false; - Thread.sleep(2000); //TODO use Awaitility to check a particular container + Thread.sleep(2000); // TODO use Awaitility to check a particular container // given camelContext.getGlobalOptions().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "5000"); @@ -319,45 +315,51 @@ public void initCamel() throws Exception { camelContext.getRouteDefinition("download-file").adviceWith(camelContext, new AdviceWithRouteBuilder() { @Override public void configure() { - weaveById("setHttpUri") - .replace() - .process(e -> { - String url = e.getIn().getBody(FilePersistedNotification.class).getUrl(); - url = url.replace("minio:9000", minio_host); - e.getIn().setHeader("httpUriReplaced", url); - }) - .setHeader("Exchange.HTTP_URI", header("httpUriReplaced")) - .setHeader("Host", constant("minio:9000")); - - weaveById("toOldHost") - .replace() - .to("http4:oldhost?preserveHostHeader=true"); + weaveById("setHttpUri").replace().process(e -> { + String url = e.getIn().getBody(FilePersistedNotification.class).getUrl(); + url = url.replace("minio:9000", minio_host); + e.getIn().setHeader("httpUriReplaced", url); + }).setHeader("Exchange.HTTP_URI", header("httpUriReplaced")).setHeader("Host", + constant("minio:9000")); + + weaveById("toOldHost").replace().to("http4:oldhost?preserveHostHeader=true"); } }); setDefaults(); - ResponseEntity> responseAnalysisModel = new RestTemplate().exchange(getBaseURLAPIPath() + "/report?limit=1000&offset=0", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + ResponseEntity> responseAnalysisModel = new RestTemplate().exchange( + getBaseURLAPIPath() + "/report?limit=1000&offset=0", HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); analysisNum = responseAnalysisModel.getBody().getData().size(); logger.info("***** Number of reports upload until now : " + analysisNum); // Checking errors are correctly treated - assertHttpClientError("/report/99999", HttpMethod.GET,HttpStatus.NOT_FOUND); + assertHttpClientError("/report/99999", HttpMethod.GET, HttpStatus.NOT_FOUND); assertHttpClientError("/report/99999", HttpMethod.DELETE, HttpStatus.NOT_FOUND); - assertHttpClientError("/report/99999/payload-link", HttpMethod.GET,HttpStatus.NOT_FOUND); - assertHttpClientError("/report/99999/payload", HttpMethod.GET,HttpStatus.NOT_FOUND); + assertHttpClientError("/report/99999/payload-link", HttpMethod.GET, HttpStatus.NOT_FOUND); + assertHttpClientError("/report/99999/payload", HttpMethod.GET, HttpStatus.NOT_FOUND); assertHttpClientError("/report/99999/initial-savings-estimation", HttpMethod.GET, HttpStatus.NOT_FOUND); // this one should give 2 pages - ResponseEntity> responseFlaggAssessment = new RestTemplate().exchange(getBaseURLAPIPath() + "/mappings/flag-assessment?limit=2&offset=0", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + ResponseEntity> responseFlaggAssessment = new RestTemplate().exchange( + getBaseURLAPIPath() + "/mappings/flag-assessment?limit=2&offset=0", HttpMethod.GET, + getRequestEntity(), new ParameterizedTypeReference>() { + }); assertThat(responseFlaggAssessment.getBody().getData().size()).isEqualTo(2); - ResponseEntity> responseFlaggAssessmentHighLimit = new RestTemplate().exchange(getBaseURLAPIPath() + "/mappings/flag-assessment?limit=1000&offset=0", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + ResponseEntity> responseFlaggAssessmentHighLimit = new RestTemplate() + .exchange(getBaseURLAPIPath() + "/mappings/flag-assessment?limit=1000&offset=0", HttpMethod.GET, + getRequestEntity(), new ParameterizedTypeReference>() { + }); assertThat(responseFlaggAssessmentHighLimit.getBody().getData().size()).isEqualTo(4); // 1. Check user has firstTime logger.info("****** Checking First Time User **********"); - ResponseEntity userEntity = new RestTemplate().exchange(getBaseURLAPIPath() + "/user", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); + ResponseEntity userEntity = new RestTemplate().exchange(getBaseURLAPIPath() + "/user", HttpMethod.GET, + getRequestEntity(), new ParameterizedTypeReference() { + }); assertThat(userEntity.getBody().isFirstTimeCreatingReports()).isTrue(); } } @@ -365,142 +367,216 @@ public void configure() { @After public void closeCamel() throws Exception { testsExecuted++; - logger.info("After test method ....................."); + logger.info("After test method ..."); if (testsExecuted == 10) { + logger.info("CLOSING CAMEL CONTEXT >>>>>>>>"); camelContext.stop(); - logger.info("...................CLOSING CAMEL CONTEXT ............."); } } @Test public void whenRegularTestShouldAnswerInTime() throws Exception { logger.info("+++++++ Regular Test ++++++"); - // Start the camel route as if the UI was sending the file to the Camel Rest Upload route + // Start the camel route as if the UI was sending the file to the Camel Rest + // Upload route int s3Objects = getStorageObjectsSize(); assertThat(s3Objects).isEqualTo(analysisNum); analysisNum++; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20190912-demolab_withSSA.tar.gz", "application/zip"), String.class); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", + getRequestEntityForUploadRESTCall("cfme_inventory-20190912-demolab_withSSA.tar.gz", "application/zip"), + String.class); // then - await() - .atMost(timeoutMilliseconds_InitialCostSavingsReport, TimeUnit.MILLISECONDS) - .with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) - .until( () -> { - // Check database for the ICSR to be created - List all = initialSavingsEstimationReportRepository.findAll(); - return all != null && all.size() == analysisNum; - }); + await().atMost(timeoutMilliseconds_InitialCostSavingsReport, TimeUnit.MILLISECONDS).with() + .pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> { + // Check database for the ICSR to be created + List all = initialSavingsEstimationReportRepository.findAll(); + return all != null && all.size() == analysisNum; + }); // Check S3 - await() - .atMost(5000, TimeUnit.MILLISECONDS) - .with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) - .until( () -> { - return (getStorageObjectsSize() == s3Objects + 1); - }); + await().atMost(5000, TimeUnit.MILLISECONDS).with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> { + return (getStorageObjectsSize() == s3Objects + 1); + }); // Check DB for initialCostSavingsReport with concrete values - InitialSavingsEstimationReportModel initialCostSavingsReportDB = initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", 1L); + InitialSavingsEstimationReportModel initialCostSavingsReportDB = initialSavingsEstimationReportService + .findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", 1L); assertThat(initialCostSavingsReportDB.getEnvironmentModel().getHypervisors() == 2); assertThat(initialCostSavingsReportDB.getSourceCostsModel().getYear1Server() == 42); // Call initialCostSavingsReport - ResponseEntity initialCostSavingsReport = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); + ResponseEntity initialCostSavingsReport = new RestTemplate().exchange( + getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference() { + }); // Call workloadInventoryReport - ResponseEntity> workloadInventoryReport = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + ResponseEntity> workloadInventoryReport = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); // Call workloadSummaryReport - ResponseEntity workloadSummaryReport = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-summary", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); + ResponseEntity workloadSummaryReport = new RestTemplate().exchange( + getBaseURLAPIPath() + String.format("/report/%d/workload-summary", analysisNum), HttpMethod.GET, + getRequestEntity(), new ParameterizedTypeReference() { + }); // Checks on Initial Savings Report - InitialSavingsEstimationReportModel initialSavingsEstimationReport_Expected = objectMapper.readValue(IOUtils.resourceToString("cfme_inventory-20190912-demolab-withssa-initial-cost-savings-report.json", StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), InitialSavingsEstimationReportModel.class); + InitialSavingsEstimationReportModel initialSavingsEstimationReport_Expected = objectMapper.readValue( + IOUtils.resourceToString("cfme_inventory-20190912-demolab-withssa-initial-cost-savings-report.json", + StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), + InitialSavingsEstimationReportModel.class); SoftAssertions.assertSoftly(softly -> softly.assertThat(initialCostSavingsReport.getBody()) - .usingRecursiveComparison() - .ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*", ".*report.*") + .usingRecursiveComparison().ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*", ".*report.*") .isEqualTo(initialSavingsEstimationReport_Expected)); // Checks on Workload Inventory Report SoftAssertions.assertSoftly(softly -> { softly.assertThat(workloadInventoryReport.getBody().getData().size()).isEqualTo(14); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().flatMap(e -> e.getWorkloads().stream()).distinct().count()).isEqualTo(7); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getWorkloads().contains("Red Hat JBoss EAP")).count()).isEqualTo(2); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().map(WorkloadInventoryReportModel::getOsName).distinct().count()).isEqualTo(4); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getOsName().contains("CentOS 7 (64-bit)")).count()).isEqualTo(2); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().map(WorkloadInventoryReportModel::getComplexity).distinct().count()).isEqualTo(4); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getComplexity().contains("Unknown")).count()).isEqualTo(0); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getComplexity().contains("Unsupported")).count()).isEqualTo(1); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().flatMap(e -> e.getRecommendedTargetsIMS().stream()).distinct().count()).isEqualTo(6); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getRecommendedTargetsIMS().contains("OSP")).count()).isEqualTo(11); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getRecommendedTargetsIMS().contains("RHEL")).count()).isEqualTo(4); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getRecommendedTargetsIMS().contains("None")).count()).isEqualTo(1); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().flatMap(e -> e.getFlagsIMS().stream()).distinct().count()).isEqualTo(2); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getFlagsIMS().contains("Shared Disk")).count()).isEqualTo(2); - softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter(e -> e.getOsName().contains("ServerNT") && e.getWorkloads().contains("Microsoft SQL Server")).count()).isEqualTo(1); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .flatMap(e -> e.getWorkloads().stream()).distinct().count()).isEqualTo(7); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .filter(e -> e.getWorkloads().contains("Red Hat JBoss EAP")).count()).isEqualTo(2); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .map(WorkloadInventoryReportModel::getOsName).distinct().count()).isEqualTo(4); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .filter(e -> e.getOsName().contains("CentOS 7 (64-bit)")).count()).isEqualTo(2); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .map(WorkloadInventoryReportModel::getComplexity).distinct().count()).isEqualTo(4); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .filter(e -> e.getComplexity().contains("Unknown")).count()).isEqualTo(0); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .filter(e -> e.getComplexity().contains("Unsupported")).count()).isEqualTo(1); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .flatMap(e -> e.getRecommendedTargetsIMS().stream()).distinct().count()).isEqualTo(6); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .filter(e -> e.getRecommendedTargetsIMS().contains("OSP")).count()).isEqualTo(11); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .filter(e -> e.getRecommendedTargetsIMS().contains("RHEL")).count()).isEqualTo(4); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .filter(e -> e.getRecommendedTargetsIMS().contains("None")).count()).isEqualTo(1); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .flatMap(e -> e.getFlagsIMS().stream()).distinct().count()).isEqualTo(2); + softly.assertThat(workloadInventoryReport.getBody().getData().stream() + .filter(e -> e.getFlagsIMS().contains("Shared Disk")).count()).isEqualTo(2); + softly.assertThat(workloadInventoryReport.getBody().getData().stream().filter( + e -> e.getOsName().contains("ServerNT") && e.getWorkloads().contains("Microsoft SQL Server")) + .count()).isEqualTo(1); }); - WorkloadInventoryReportModel[] workloadInventoryReportModelExpected = objectMapper.readValue(IOUtils.resourceToString("cfme_inventory-20190912-demolab-withssa-workload-inventory-report.json", StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), WorkloadInventoryReportModel[].class); - assertThat(workloadInventoryReport.getBody().getData().toArray()) - .usingRecursiveComparison() + WorkloadInventoryReportModel[] workloadInventoryReportModelExpected = objectMapper.readValue( + IOUtils.resourceToString("cfme_inventory-20190912-demolab-withssa-workload-inventory-report.json", + StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), + WorkloadInventoryReportModel[].class); + assertThat(workloadInventoryReport.getBody().getData().toArray()).usingRecursiveComparison() .ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*") .isEqualTo(workloadInventoryReportModelExpected); // Checks on Workload Summary Report - WorkloadSummaryReportModel workloadSummaryReport_Expected = objectMapper.readValue(IOUtils.resourceToString("cfme_inventory-20190912-demolab-withssa-workload-summary-report.json", StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), WorkloadSummaryReportModel.class); - - assertThat(workloadSummaryReport.getBody()) - .usingRecursiveComparison() - .ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*", ".*report.*", ".*workloadsDetectedOSTypeModels.*", ".*scanRunModels.*") + WorkloadSummaryReportModel workloadSummaryReport_Expected = objectMapper + .readValue( + IOUtils.resourceToString("cfme_inventory-20190912-demolab-withssa-workload-summary-report.json", + StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), + WorkloadSummaryReportModel.class); + + assertThat(workloadSummaryReport.getBody()).usingRecursiveComparison() + .ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*", ".*report.*", + ".*workloadsDetectedOSTypeModels.*", ".*scanRunModels.*") .isEqualTo(workloadSummaryReport_Expected); // WLSR.ScanRunModels - TreeSet wks_scanrunmodel_expected = getWks_scanrunmodel(workloadSummaryReport_Expected.getScanRunModels()); - TreeSet wks_scanrunmodel_actual = getWks_scanrunmodel(workloadSummaryReport.getBody().getScanRunModels()); + TreeSet wks_scanrunmodel_expected = getWks_scanrunmodel( + workloadSummaryReport_Expected.getScanRunModels()); + TreeSet wks_scanrunmodel_actual = getWks_scanrunmodel( + workloadSummaryReport.getBody().getScanRunModels()); // WLSR.WorkloadsDetectedOSTypeModel - TreeSet wks_ostypemodel_expected = getWks_ostypemodel(workloadSummaryReport_Expected.getWorkloadsDetectedOSTypeModels()); - TreeSet wks_ostypemodel_actual = getWks_ostypemodel(workloadSummaryReport.getBody().getWorkloadsDetectedOSTypeModels()); + TreeSet wks_ostypemodel_expected = getWks_ostypemodel( + workloadSummaryReport_Expected.getWorkloadsDetectedOSTypeModels()); + TreeSet wks_ostypemodel_actual = getWks_ostypemodel( + workloadSummaryReport.getBody().getWorkloadsDetectedOSTypeModels()); SoftAssertions.assertSoftly(softly -> { - softly.assertThat(wks_scanrunmodel_actual).isEqualTo(wks_scanrunmodel_expected); - softly.assertThat(wks_ostypemodel_actual).isEqualTo(wks_ostypemodel_expected); + softly.assertThat(wks_scanrunmodel_actual).isEqualTo(wks_scanrunmodel_expected); + softly.assertThat(wks_ostypemodel_actual).isEqualTo(wks_ostypemodel_expected); }); // Checking that the JSON file for Cost Savings Report has ISO Format Dates - ResponseEntity initialCostSavingsReportJSON = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); - assertThat(initialCostSavingsReportJSON.getBody().matches(".*creationDate\"\\:\"\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}.*")).isTrue(); + ResponseEntity initialCostSavingsReportJSON = new RestTemplate().exchange( + getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), + HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() { + }); + assertThat(initialCostSavingsReportJSON.getBody() + .matches(".*creationDate\"\\:\"\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}.*")).isTrue(); - // Checking that a NOT FOUND status code is returned when asking for a Initial Savings Report that doesnt exist + // Checking that a NOT FOUND status code is returned when asking for a Initial + // Savings Report that doesnt exist assertThatExceptionOfType(org.springframework.web.client.HttpClientErrorException.class) - .isThrownBy(() -> new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", 9999), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {})) - .matches(e -> e.getStatusCode().equals(HttpStatus.NOT_FOUND)) - .matches(e -> e.getResponseBodyAsString().equalsIgnoreCase("Report not found")); - - // OpenAPI Pagination Response test - ResponseEntity> workloadInventoryReportPagination = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + .isThrownBy(() -> new RestTemplate().exchange( + getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", 9999), + HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() { + })) + .matches(e -> e.getStatusCode().equals(HttpStatus.NOT_FOUND)) + .matches(e -> e.getResponseBodyAsString().equalsIgnoreCase("Report not found")); + + // OpenAPI Pagination Response test + ResponseEntity> workloadInventoryReportPagination = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); assertThat(workloadInventoryReportPagination.getBody().getMeta().getCount()).isEqualTo(14); assertThat(workloadInventoryReportPagination.getBody().getMeta().getOffset()).isEqualTo(0); assertThat(workloadInventoryReportPagination.getBody().getMeta().getLimit()).isEqualTo(100); - assertThat(workloadInventoryReportPagination.getBody().getLinks().getFirst()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?limit=100&offset=0", analysisNum)); - assertThat(workloadInventoryReportPagination.getBody().getLinks().getLast()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?limit=100&offset=0", analysisNum)); + assertThat(workloadInventoryReportPagination.getBody().getLinks().getFirst()) + .isEqualTo(getBaseURLAPIPathWithoutHost() + + String.format("/report/%d/workload-inventory?limit=100&offset=0", analysisNum)); + assertThat(workloadInventoryReportPagination.getBody().getLinks().getLast()) + .isEqualTo(getBaseURLAPIPathWithoutHost() + + String.format("/report/%d/workload-inventory?limit=100&offset=0", analysisNum)); assertThat(workloadInventoryReportPagination.getBody().getLinks().getPrev()).isNull(); assertThat(workloadInventoryReportPagination.getBody().getLinks().getNext()).isNull(); - workloadInventoryReportPagination = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=8", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + workloadInventoryReportPagination = new RestTemplate().exchange( + getBaseURLAPIPath() + String.format( + "/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=8", + analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); assertThat(workloadInventoryReportPagination.getBody().getMeta().getCount()).isEqualTo(14); assertThat(workloadInventoryReportPagination.getBody().getMeta().getOffset()).isEqualTo(8); assertThat(workloadInventoryReportPagination.getBody().getMeta().getLimit()).isEqualTo(4); - assertThat(workloadInventoryReportPagination.getBody().getLinks().getFirst()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=0", analysisNum)); - assertThat(workloadInventoryReportPagination.getBody().getLinks().getLast()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=12", analysisNum)); - assertThat(workloadInventoryReportPagination.getBody().getLinks().getPrev()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=4", analysisNum)); - assertThat(workloadInventoryReportPagination.getBody().getLinks().getNext()).isEqualTo(getBaseURLAPIPathWithoutHost() + String.format("/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=12", analysisNum)); + assertThat(workloadInventoryReportPagination.getBody().getLinks().getFirst()) + .isEqualTo(getBaseURLAPIPathWithoutHost() + String.format( + "/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=0", + analysisNum)); + assertThat(workloadInventoryReportPagination.getBody().getLinks().getLast()) + .isEqualTo(getBaseURLAPIPathWithoutHost() + String.format( + "/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=12", + analysisNum)); + assertThat(workloadInventoryReportPagination.getBody().getLinks().getPrev()) + .isEqualTo(getBaseURLAPIPathWithoutHost() + String.format( + "/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=4", + analysisNum)); + assertThat(workloadInventoryReportPagination.getBody().getLinks().getNext()) + .isEqualTo(getBaseURLAPIPathWithoutHost() + String.format( + "/report/%d/workload-inventory?datacenter=Datacenter&cluster=VMCluster&limit=4&offset=12", + analysisNum)); // Testing that limit and offset params are really taken into consideration - ResponseEntity> responseAnalysisModel = new RestTemplate().exchange(getBaseURLAPIPath() + "/report?limit=2&offset=0", HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + ResponseEntity> responseAnalysisModel = new RestTemplate().exchange( + getBaseURLAPIPath() + "/report?limit=2&offset=0", HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); assertThat(responseAnalysisModel.getBody().getData().size()).isLessThanOrEqualTo(2); logger.info("------- End Regular Test -------"); @@ -513,8 +589,11 @@ public void whenPerformanceTestShouldAnswerInTime() throws Exception { // Performance test logger.info("+++++++ Performance Test ++++++"); - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20190829-16128-uq17dx.tar.gz", "application/zip"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_PerformaceTest)).isEqualTo(142); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", + getRequestEntityForUploadRESTCall("cfme_inventory-20190829-16128-uq17dx.tar.gz", "application/zip"), + String.class); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_PerformaceTest)).isEqualTo(142); logger.info("------- End Performance Test ------"); } @@ -525,13 +604,26 @@ public void whenFileWithVMWithoutHostShouldAddVMToWorkloadInventory() throws Exc // Test with a file with VM without Host logger.info("+++++++ Test with a file with VM without Host ++++++"); - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_without_host.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum),timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); - - ResponseEntity> workloadInventoryReport_file_vm_without_host = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?size=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", + getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_without_host.json", "application/json"), + String.class); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); + + ResponseEntity> workloadInventoryReport_file_vm_without_host = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?size=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().size()).isEqualTo(8); - assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(2); - assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(6); + assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream() + .filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") + && e.getCluster().equalsIgnoreCase("No cluster defined")) + .count()).isEqualTo(2); + assertThat(workloadInventoryReport_file_vm_without_host.getBody().getData().stream() + .filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") + && !e.getCluster().equalsIgnoreCase("No cluster defined")) + .count()).isEqualTo(6); logger.info("------- End file with VM without Host Test ------"); } @@ -541,16 +633,28 @@ public void whenFileWithHostWithoutClusterShouldAddVMToWorkloadInventory() throw analysisNum++; // Test with a file with Host without Cluster logger.info("+++++++ Test with a file with Host without Cluster ++++++"); - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-host_without_cluster.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); - - ResponseEntity> workloadInventoryReport_file_host_without_cluster = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( + "cloudforms-export-v1_0_0-host_without_cluster.json", "application/json"), String.class); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); + + ResponseEntity> workloadInventoryReport_file_host_without_cluster = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); // Total VMs assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getData().size()).isEqualTo(8); // Wrong VMs - assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getData().stream().filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") && e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(3); + assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getData().stream() + .filter(e -> e.getDatacenter().equalsIgnoreCase("No datacenter defined") + && e.getCluster().equalsIgnoreCase("No cluster defined")) + .count()).isEqualTo(3); // Right VMs - assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getData().stream().filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") && !e.getCluster().equalsIgnoreCase("No cluster defined")).count()).isEqualTo(5); + assertThat(workloadInventoryReport_file_host_without_cluster.getBody().getData().stream() + .filter(e -> !e.getDatacenter().equalsIgnoreCase("No datacenter defined") + && !e.getCluster().equalsIgnoreCase("No cluster defined")) + .count()).isEqualTo(5); logger.info("------- End file with Host without Cluster Test ------"); } @@ -560,15 +664,28 @@ public void whenFileWithWrongCPUCoresPerSocketShouldNotFailAndFallbackTheValue() analysisNum++; // Test with a file with Wrong CPU cores per socket logger.info("+++++++ Test with a file with Wrong CPU cores per socket ++++++"); - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-wrong_cpu_cores_per_socket.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(5); - - ResponseEntity initialCostSavingsReport_wrong_cpu_cores = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); - assertThat(initialCostSavingsReport_wrong_cpu_cores.getBody().getEnvironmentModel().getHypervisors()).isEqualTo(2); - - ResponseEntity> workloadInventoryReport_file_wrong_cpu_cores = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); - assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getData().stream().filter(e -> e.getCpuCores() == null).count()).isEqualTo(0); - assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getData().stream().filter(e -> e.getCpuCores() != null).count()).isEqualTo(5); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( + "cloudforms-export-v1_0_0-wrong_cpu_cores_per_socket.json", "application/json"), String.class); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(5); + + ResponseEntity initialCostSavingsReport_wrong_cpu_cores = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference() { + }); + assertThat(initialCostSavingsReport_wrong_cpu_cores.getBody().getEnvironmentModel().getHypervisors()) + .isEqualTo(2); + + ResponseEntity> workloadInventoryReport_file_wrong_cpu_cores = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); + assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getData().stream() + .filter(e -> e.getCpuCores() == null).count()).isEqualTo(0); + assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getData().stream() + .filter(e -> e.getCpuCores() != null).count()).isEqualTo(5); assertThat(workloadInventoryReport_file_wrong_cpu_cores.getBody().getData().size()).isEqualTo(5); logger.info("------- End Wrong CPU cores per socket Test ------"); @@ -579,15 +696,29 @@ public void whenFileWith0CPUCoresPerSocketShouldNotFailAndFallbackTheValue() thr analysisNum++; // Test with a file with 0 CPU cores per socket logger.info("+++++++ Test with a file with 0 CPU cores per socket ++++++"); - ResponseEntity response = new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_0_cores.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); - - ResponseEntity initialCostSavingsReport_zero_cpu_cores = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); - assertThat(initialCostSavingsReport_zero_cpu_cores.getBody().getEnvironmentModel().getHypervisors()).isEqualTo(2); - - ResponseEntity> workloadInventoryReport_file_zero_cpu_cores = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); - assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getData().stream().filter(e -> e.getCpuCores() == null).count()).isEqualTo(0); - assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getData().stream().filter(e -> e.getCpuCores() != null).count()).isEqualTo(8); + ResponseEntity response = new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", + getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_0_cores.json", "application/json"), + String.class); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); + + ResponseEntity initialCostSavingsReport_zero_cpu_cores = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference() { + }); + assertThat(initialCostSavingsReport_zero_cpu_cores.getBody().getEnvironmentModel().getHypervisors()) + .isEqualTo(2); + + ResponseEntity> workloadInventoryReport_file_zero_cpu_cores = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); + assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getData().stream() + .filter(e -> e.getCpuCores() == null).count()).isEqualTo(0); + assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getData().stream() + .filter(e -> e.getCpuCores() != null).count()).isEqualTo(8); assertThat(workloadInventoryReport_file_zero_cpu_cores.getBody().getData().size()).isEqualTo(8); logger.info("------- End 0 CPU cores per socket Test ------"); @@ -598,43 +729,74 @@ public void whenFileWithUsedDiskStorageShouldNotFailAndFallbackTheValue() throws analysisNum++; // Test with a file with VM.used_disk_storage logger.info("+++++++ Test with a file with VM.used_disk_storage ++++++"); - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); - - ResponseEntity initialCostSavingsReport_vm_with_used_disk = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); - assertThat(initialCostSavingsReport_vm_with_used_disk.getBody().getEnvironmentModel().getHypervisors()).isEqualTo(4); - - ResponseEntity> workloadInventoryReport_vm_with_used_disk = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( + "cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(8); + + ResponseEntity initialCostSavingsReport_vm_with_used_disk = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference() { + }); + assertThat(initialCostSavingsReport_vm_with_used_disk.getBody().getEnvironmentModel().getHypervisors()) + .isEqualTo(4); + + ResponseEntity> workloadInventoryReport_vm_with_used_disk = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); assertThat(workloadInventoryReport_vm_with_used_disk.getBody().getData().size()).isEqualTo(8); assertThat(workloadInventoryReport_vm_with_used_disk.getBody().getData().stream() - .filter(e -> ("tomcat".equalsIgnoreCase(e.getVmName())) && (e.getDiskSpace() == 2159550464L)).count()).isEqualTo(1); + .filter(e -> ("tomcat".equalsIgnoreCase(e.getVmName())) && (e.getDiskSpace() == 2159550464L)).count()) + .isEqualTo(1); assertThat(workloadInventoryReport_vm_with_used_disk.getBody().getData().stream() - .filter(e -> ("lb".equalsIgnoreCase(e.getVmName())) && (e.getDiskSpace() == 2620260352L + 5000L)).count()).isEqualTo(1); - //NICs flag test + .filter(e -> ("lb".equalsIgnoreCase(e.getVmName())) && (e.getDiskSpace() == 2620260352L + 5000L)) + .count()).isEqualTo(1); + // NICs flag test assertThat(workloadInventoryReport_vm_with_used_disk.getBody().getData().stream() .filter(e -> e.getFlagsIMS().contains(">4 vNICs")).count()).isEqualTo(0); // Test Insights Enabled analysisNum++; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20200318-Insights.tar.gz", "application/zip"), String.class); - - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(14); - - ResponseEntity> workloadInventoryReport_with_insights_enabled = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference>() {}); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", + getRequestEntityForUploadRESTCall("cfme_inventory-20200318-Insights.tar.gz", "application/zip"), + String.class); + + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(14); + + ResponseEntity> workloadInventoryReport_with_insights_enabled = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); assertThat(workloadInventoryReport_with_insights_enabled.getBody().getData().size()).isEqualTo(14); - assertThat(workloadInventoryReport_with_insights_enabled.getBody().getData().stream().filter(e -> e.getInsightsEnabled()).count()).isEqualTo(2); + assertThat(workloadInventoryReport_with_insights_enabled.getBody().getData().stream() + .filter(e -> e.getInsightsEnabled()).count()).isEqualTo(2); // Test OSInformation, JavaRuntimes, and ApplicationPlatforms in WMS analysisNum++; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory-20200304-Linux_JDK.tar.gz", "application/zip"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(14); - - ResponseEntity workloadSummaryReportJavaRuntimes = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-summary", analysisNum), HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() {}); - WorkloadSummaryReportModel workloadSummaryReport_JavaRuntimesExpected = new ObjectMapper().readValue(IOUtils.resourceToString("cfme_inventory-20200304-Linux_JDK-summary-report.json", StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), WorkloadSummaryReportModel.class); - - assertThat(workloadSummaryReportJavaRuntimes.getBody()) - .usingRecursiveComparison() - .ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*", ".*report.*", ".*workloadsDetectedOSTypeModels.*", ".*scanRunModels.*") + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", + getRequestEntityForUploadRESTCall("cfme_inventory-20200304-Linux_JDK.tar.gz", "application/zip"), + String.class); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_InitialCostSavingsReport)).isEqualTo(14); + + ResponseEntity workloadSummaryReportJavaRuntimes = new RestTemplate().exchange( + getBaseURLAPIPath() + String.format("/report/%d/workload-summary", analysisNum), HttpMethod.GET, + getRequestEntity(), new ParameterizedTypeReference() { + }); + WorkloadSummaryReportModel workloadSummaryReport_JavaRuntimesExpected = new ObjectMapper() + .readValue( + IOUtils.resourceToString("cfme_inventory-20200304-Linux_JDK-summary-report.json", + StandardCharsets.UTF_8, EndToEndTest.class.getClassLoader()), + WorkloadSummaryReportModel.class); + + assertThat(workloadSummaryReportJavaRuntimes.getBody()).usingRecursiveComparison() + .ignoringFieldsMatchingRegexes(".*id.*", ".*creationDate.*", ".*report.*", + ".*workloadsDetectedOSTypeModels.*", ".*scanRunModels.*") .isEqualTo(workloadSummaryReport_JavaRuntimesExpected); logger.info("------- End file with VM.used_disk_storage Test ------"); @@ -645,8 +807,10 @@ public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { // Ultra Performance test logger.info("+++++++ Ultra Performance Test ++++++"); analysisNum++; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( + "cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); logger.info("------- End Ultra Performance Test ------"); } @@ -656,26 +820,38 @@ public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exce // Stress test // We load 2 times a BIG file ( 8 Mb ) and 2 times a small file ( 316 Kb ) // More or less 7 minutes each bunch of threads of Big Files - // 1 bunch of threads for 2 big files and 1 small file, while 1 big file and 1 small file wait in the queue + // 1 bunch of threads for 2 big files and 1 small file, while 1 big file and 1 + // small file wait in the queue // We have 3 consumers // To process the first small file it should take 10 seconds - // To process the second small file it should take 7 minutes of the first bunch of big files plus 10 seconds of the small file + // To process the second small file it should take 7 minutes of the first bunch + // of big files plus 10 seconds of the small file logger.info("+++++++ Stress Test ++++++"); int firstupload = ++analysisNum; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( + "cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); analysisNum++; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0.json", "application/json"), String.class); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", + getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0.json", "application/json"), String.class); analysisNum++; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( + "cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); analysisNum++; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0.json", "application/json"), String.class); - // We will check for time we retrieve the third file uploaded to see previous ones are not affecting - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 1), timeoutMilliseconds_SmallFileSummaryReport)).isEqualTo(8); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 2), timeoutMilliseconds_UltraPerformaceTest)).isEqualTo( numberVMsExpected_InBigFile); - - int timeoutMilliseconds_secondSmallFile = timeoutMilliseconds_UltraPerformaceTest + timeoutMilliseconds_SmallFileSummaryReport; - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 3), timeoutMilliseconds_secondSmallFile)).isEqualTo( 8); + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", + getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0.json", "application/json"), String.class); + // We will check for time we retrieve the third file uploaded to see previous + // ones are not affecting + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 1), + timeoutMilliseconds_SmallFileSummaryReport)).isEqualTo(8); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload), + timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 2), + timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); + + int timeoutMilliseconds_secondSmallFile = timeoutMilliseconds_UltraPerformaceTest + + timeoutMilliseconds_SmallFileSummaryReport; + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", firstupload + 3), + timeoutMilliseconds_secondSmallFile)).isEqualTo(8); logger.info("------- End Stress Test ------"); } @@ -685,26 +861,30 @@ public void whenDeleteReportShouldRemoveFileInS3() throws Exception { logger.info("++++++++ Delete report test +++++"); int s3ObjectsBefore = getStorageObjectsSize(); - // we upload a file to be sure there's one report to delete, as it could be that this test is executed the first - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall("cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); + // we upload a file to be sure there's one report to delete, as it could be that + // this test is executed the first + new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( + "cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); analysisNum++; - await() - .atMost(5000, TimeUnit.MILLISECONDS) - .with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS) - .until( () -> { - return getStorageObjectsSize() == s3ObjectsBefore + 1; - }); + await().atMost(5000, TimeUnit.MILLISECONDS).with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> { + return getStorageObjectsSize() == s3ObjectsBefore + 1; + }); - ResponseEntity stringEntity = new RestTemplate().exchange(getBaseURLAPIPath() + String.format("/report/%d", analysisNum), HttpMethod.DELETE, getRequestEntity(), new ParameterizedTypeReference() {}); + ResponseEntity stringEntity = new RestTemplate().exchange( + getBaseURLAPIPath() + String.format("/report/%d", analysisNum), HttpMethod.DELETE, getRequestEntity(), + new ParameterizedTypeReference() { + }); assertThat(stringEntity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); - assertThat(initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", Long.valueOf(analysisNum))).isNull(); + assertThat(initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", + Long.valueOf(analysisNum))).isNull(); assertThat(getStorageObjectsSize()).isEqualTo(s3ObjectsBefore); logger.info("--------- End Delete report test -------"); } private String getBaseURLAPIPath() { - return "http://localhost:" + serverPort + basePath.substring(0, basePath.length() - 1); // to remove the last * char + return "http://localhost:" + serverPort + basePath.substring(0, basePath.length() - 1); // to remove the last * + // char } private String getBaseURLAPIPathWithoutHost() { @@ -713,18 +893,19 @@ private String getBaseURLAPIPathWithoutHost() { private Integer callSummaryReportAndCheckVMs(final String reportUrl, int timeoutMilliseconds) { AtomicInteger numberVMsReceived = new AtomicInteger(0); - await() - .atMost(timeoutMilliseconds, TimeUnit.MILLISECONDS) - .with().pollInterval(Duration.ONE_SECOND) + await().atMost(timeoutMilliseconds, TimeUnit.MILLISECONDS).with().pollInterval(Duration.ONE_SECOND) .until(() -> { - ResponseEntity workloadSummaryReport_stress_checkVMs = new RestTemplate().exchange(getBaseURLAPIPath() + reportUrl, HttpMethod.GET, getRequestEntity(), new ParameterizedTypeReference() { - }); - boolean success = (workloadSummaryReport_stress_checkVMs != null && - workloadSummaryReport_stress_checkVMs.getStatusCodeValue() == 200 && - workloadSummaryReport_stress_checkVMs.getBody() != null && - workloadSummaryReport_stress_checkVMs.getBody().getSummaryModels() != null ); + ResponseEntity workloadSummaryReport_stress_checkVMs = new RestTemplate() + .exchange(getBaseURLAPIPath() + reportUrl, HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference() { + }); + boolean success = (workloadSummaryReport_stress_checkVMs != null + && workloadSummaryReport_stress_checkVMs.getStatusCodeValue() == 200 + && workloadSummaryReport_stress_checkVMs.getBody() != null + && workloadSummaryReport_stress_checkVMs.getBody().getSummaryModels() != null); if (success) { - numberVMsReceived.set(workloadSummaryReport_stress_checkVMs.getBody().getSummaryModels().stream().mapToInt(SummaryModel::getVms).sum()); + numberVMsReceived.set(workloadSummaryReport_stress_checkVMs.getBody().getSummaryModels() + .stream().mapToInt(SummaryModel::getVms).sum()); } return success; }); @@ -750,7 +931,8 @@ private HttpEntity getRequestEntity() { } @NotNull - private HttpEntity> getRequestEntityForUploadRESTCall(String filename, String content_type_header) throws IOException { + private HttpEntity> getRequestEntityForUploadRESTCall(String filename, + String content_type_header) throws IOException { // Headers HttpHeaders headers = getHttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); @@ -762,7 +944,8 @@ private HttpEntity> getRequestEntityForUploadRESTC LinkedMultiValueMap fileMap = new LinkedMultiValueMap<>(); fileMap.add(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=filex; filename=" + filename); fileMap.add("Content-type", content_type_header); - body.add("file", new HttpEntity<>(IOUtils.resourceToByteArray(filename, EndToEndTest.class.getClassLoader()), fileMap)); + body.add("file", + new HttpEntity<>(IOUtils.resourceToByteArray(filename, EndToEndTest.class.getClassLoader()), fileMap)); // params Body parts body.add("percentageOfHypervisorsMigratedOnYear1", "50"); @@ -782,13 +965,13 @@ private HttpHeaders getHttpHeaders() { // Headers HttpHeaders headers = new HttpHeaders(); headers.set("x-rh-insights-request-id", UUID.randomUUID().toString()); - String rhIdentityJson = "{\"entitlements\":{\"insights\":{\"is_entitled\":true},\"openshift\":{\"is_entitled\":true},\"smart_management\":{\"is_entitled\":false},\"hybrid_cloud\":{\"is_entitled\":true}}," + - "\"identity\":{\"internal\":{\"auth_time\":0,\"auth_type\":\"jwt-auth\",\"org_id\":\"6340056\", " + - //"\"filename\":\"" + filename + "\"," + + String rhIdentityJson = "{\"entitlements\":{\"insights\":{\"is_entitled\":true},\"openshift\":{\"is_entitled\":true},\"smart_management\":{\"is_entitled\":false},\"hybrid_cloud\":{\"is_entitled\":true}}," + + "\"identity\":{\"internal\":{\"auth_time\":0,\"auth_type\":\"jwt-auth\",\"org_id\":\"6340056\", " + + // "\"filename\":\"" + filename + "\"," + "\"origin\":\"xavier\",\"customerid\":\"CID888\"}," + // \"analysisId\":\"" + analysisId + "\"}," + "\"account_number\":\"1460290\", \"user\":{\"first_name\":\"User\",\"is_active\":true,\"is_internal\":true,\"last_name\":\"Dumy\",\"locale\":\"en_US\",\"is_org_admin\":false,\"username\":\"dummy@redhat.com\",\"email\":\"dummy+qa@redhat.com\"},\"type\":\"User\"}}"; - headers.set("x-rh-identity", Base64.encodeAsString(rhIdentityJson.getBytes()) ); + headers.set("x-rh-identity", Base64.encodeAsString(rhIdentityJson.getBytes())); headers.set("username", "dummy@redhat.com"); return headers; } diff --git a/src/test/java/org/jboss/xavier/integrations/SpringBootEndToEndTestContextInitializer.java b/src/test/java/org/jboss/xavier/integrations/SpringBootEndToEndTestContextInitializer.java deleted file mode 100644 index c97ec733..00000000 --- a/src/test/java/org/jboss/xavier/integrations/SpringBootEndToEndTestContextInitializer.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.jboss.xavier.integrations; - -import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3; - -import org.springframework.boot.test.util.EnvironmentTestUtils; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; - -public class SpringBootEndToEndTestContextInitializer implements ApplicationContextInitializer { - @Override - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - try { - TestContainersInfrastructure containersInfrastructure = new TestContainersInfrastructure(); - containersInfrastructure.createAndStartDockerContainers(); - Thread.sleep(10000); - - EnvironmentTestUtils.addEnvironment("test", configurableApplicationContext.getEnvironment(), - "amq.server=" + containersInfrastructure.getActivemq().getContainerIpAddress(), - "amq.port=" + containersInfrastructure.getActivemq().getMappedPort(61616), - "minio.host=" + containersInfrastructure.getContainerHost(containersInfrastructure.getMinio(), 9000), - "insights.upload.host=" + containersInfrastructure.getContainerHost(containersInfrastructure.getIngress()), - "insights.properties=yearOverYearGrowthRatePercentage,percentageOfHypervisorsMigratedOnYear1,percentageOfHypervisorsMigratedOnYear2,percentageOfHypervisorsMigratedOnYear3,reportName,reportDescription", -// "camel.component.servlet.mapping.context-path=/api/xavier/*", - "insights.kafka.host=" + containersInfrastructure.getKafka().getBootstrapServers(), - "postgresql.service.name=" + containersInfrastructure.getPostgreSQL().getContainerIpAddress(), - "postgresql.service.port=" + containersInfrastructure.getPostgreSQL().getFirstMappedPort(), - "spring.datasource.username=" + containersInfrastructure.getPostgreSQL().getUsername(), - "spring.datasource.password=" + containersInfrastructure.getPostgreSQL().getPassword(), - "S3_HOST=" + containersInfrastructure.getLocalstack().getEndpointConfiguration(S3).getServiceEndpoint(), - "S3_REGION="+ containersInfrastructure.getLocalstack().getEndpointConfiguration(S3).getSigningRegion(), - "kieserver.devel-service=" + containersInfrastructure.getHostForKie() + "/kie-server", - "spring.datasource.url = jdbc:postgresql://" + containersInfrastructure.getContainerHost(containersInfrastructure.getPostgreSQL()) + "/sampledb" , - "spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect" , - "thread.concurrentConsumers=3", - "insights.rbac.path=/api/v1/access/", - "insights.rbac.host=" + "http://" + containersInfrastructure.getContainerHost(containersInfrastructure.getRbacServer(), 8000)); - } catch (Exception e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java index 157afbd9..25e27384 100644 --- a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java +++ b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java @@ -10,6 +10,7 @@ import java.net.URL; import java.nio.file.Paths; import java.util.Enumeration; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -19,6 +20,9 @@ import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -32,171 +36,139 @@ import org.testcontainers.containers.localstack.LocalStackContainer; import org.testcontainers.containers.output.Slf4jLogConsumer; import org.testcontainers.images.builder.ImageFromDockerfile; +import org.testcontainers.lifecycle.Startables; import org.testcontainers.utility.MountableFile; public class TestContainersInfrastructure { - private Logger logger = LoggerFactory.getLogger(TestContainersInfrastructure.class); - - private String ingressCommitHash = "3ea33a8d793c2154f7cfa12057ca005c5f6031fa"; // 2019-11-11 - private String insightsRbacCommitHash = "a55b610a1385f0f6d3188b08710ec6a5890a97f6"; // 2020-02-05 - - private GenericContainer kie_server; - - private PostgreSQLContainer postgreSQL; - - private LocalStackContainer localstack; - - private GenericContainer activemq; - - private GenericContainer minio; - - private GenericContainer createbuckets; - - private KafkaContainer kafka; - - private GenericContainer ingress; - - private GenericContainer rbacPostgreSQL; - - private GenericContainer rbacServer; - - private GenericContainer getRBACServerContainer(Network rbacNetwork) { - return new GenericContainer<>(new ImageFromDockerfile() + private static Logger logger = LoggerFactory.getLogger(TestContainersInfrastructure.class); + + private static String ingressCommitHash = "3ea33a8d793c2154f7cfa12057ca005c5f6031fa"; // 2019-11-11 + private static String insightsRbacCommitHash = "a55b610a1385f0f6d3188b08710ec6a5890a97f6"; // 2020-02-05 + + protected static GenericContainer kie_serverContainer = new GenericContainer<>("jboss/kie-server-showcase:7.18.0.Final").withNetworkAliases("kie-server") + .withExposedPorts(8080).withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("KIE-LOG")) + .withEnv("KIE_SERVER_ID", "analytics-kieserver").withEnv("KIE_ADMIN_USER", "kieserver") + .withEnv("KIE_ADMIN_PWD", "kieserver1!").withEnv("KIE_SERVER_MODE", "DEVELOPMENT") + .withEnv("KIE_MAVEN_REPO", "https://oss.sonatype.org/content/repositories/snapshots") + .withEnv("KIE_REPOSITORY", "https://repository.jboss.org/nexus/content/groups/public-jboss") + .withEnv("KIE_SERVER_CONTROLLER_PWD", "admin").withEnv("KIE_SERVER_CONTROLLER_USER", "admin") + .withEnv("KIE_SERVER_LOCATION", "http://kie-server:8080/kie-server/services/rest/server") + .withEnv("KIE_SERVER_PWD", "kieserver1!").withEnv("KIE_SERVER_USER", "kieserver"); + + protected static PostgreSQLContainer postgreSQLContainer=new PostgreSQLContainer().withDatabaseName("sampledb").withUsername("admin").withPassword("redhat"); + + protected static LocalStackContainer localstackContainer = new LocalStackContainer().withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AWS-LOG")) + .withServices(S3); + + protected static GenericContainer activemqContainer=new GenericContainer<>("vromero/activemq-artemis").withExposedPorts(61616, 8161) + .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AMQ-LOG")).withEnv("DISABLE_SECURITY", "true") + .withEnv("BROKER_CONFIG_GLOBAL_MAX_SIZE", "50000").withEnv("BROKER_CONFIG_MAX_SIZE_BYTES", "50000") + .withEnv("BROKER_CONFIG_MAX_DISK_USAGE", "100"); + + + // INGRESS + private static Network ingressNetwork = Network.newNetwork(); + + protected static GenericContainer minioContainer = new GenericContainer<>("minio/minio").withCommand("server /data").withExposedPorts(9000) + .withNetworkAliases("minio").withNetwork(ingressNetwork) + .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("MINIO-LOG")) + .withEnv("MINIO_ACCESS_KEY", "BQA2GEXO711FVBVXDWKM") + .withEnv("MINIO_SECRET_KEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC"); + + protected static GenericContainer createbucketsContainer=new GenericContainer<>("minio/mc").dependsOn(minioContainer) + .withNetwork(ingressNetwork) + .withStartupTimeout(java.time.Duration.ofSeconds(60)) + .withStartupAttempts(20) + .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("MINIO-MC-LOG")) + .withCopyFileToContainer(MountableFile.forClasspathResource("minio-bucket-creation-commands.sh"), "/") + .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd.withEntrypoint("sh", + "/minio-bucket-creation-commands.sh", "minio:9000")); + + protected static KafkaContainer kafkaContainer=new KafkaContainer().withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("KAFKA-LOG")) + .withNetworkAliases("kafka").withNetwork(ingressNetwork); + + protected static GenericContainer ingressContainer=new GenericContainer(new ImageFromDockerfile() + .withDockerfile(Paths.get("src/test/resources/insights-ingress-go/Dockerfile"))) + .withExposedPorts(3000) + .withNetwork(ingressNetwork) + .withLogConsumer(new Slf4jLogConsumer(logger) + .withPrefix("INGRESS-LOG")) + .withEnv("AWS_ACCESS_KEY_ID", "BQA2GEXO711FVBVXDWKM") + .withEnv("AWS_SECRET_ACCESS_KEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") + .withEnv("AWS_REGION", "us-east-1") + .withEnv("INGRESS_STAGEBUCKET", "insights-upload-perma") + .withEnv("INGRESS_REJECTBUCKET", "insights-upload-rejected") + .withEnv("INGRESS_INVENTORYURL", "http://inventory:8080/api/inventory/v1/hosts") + .withEnv("INGRESS_VALIDTOPICS", "xavier,testareno,advisortestareno,advisor") + .withEnv("OPENSHIFT_BUILD_COMMIT", "woopwoop") + .withEnv("INGRESS_MINIODEV", "true") + .withEnv("INGRESS_MINIOACCESSKEY", "BQA2GEXO711FVBVXDWKM") + .withEnv("INGRESS_MINIOSECRETKEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") + .withEnv("INGRESS_MINIOENDPOINT", "minio:9000") + .withEnv("INGRESS_KAFKABROKERS", "kafka:9092") + .dependsOn(kafkaContainer,minioContainer,createbucketsContainer); + + + // RBAC + private static Network rbacNetwork = Network.newNetwork(); + + protected static GenericContainer rbacPostgreSQLContainer=new PostgreSQLContainer().withDatabaseName("rb_database").withUsername("rbac_username") + .withPassword("rbac_password").withNetwork(rbacNetwork).withNetworkAliases("rbac_db"); + + + protected static GenericContainer rbacServerContainer=new GenericContainer<>(new ImageFromDockerfile() .withDockerfile(Paths.get("src/test/resources/insights-rbac/insightsRbac_Dockerfile"))) - .withNetwork(rbacNetwork).withNetworkAliases("rbac").withExposedPorts(8000) - .withEnv("DATABASE_SERVICE_NAME", "POSTGRES_SQL").withEnv("DATABASE_ENGINE", "postgresql") - .withEnv("DATABASE_NAME", "rb_database").withEnv("DATABASE_USER", "rbac_username") - .withEnv("DATABASE_PASSWORD", "rbac_password").withEnv("POSTGRES_SQL_SERVICE_HOST", "rbac_db") - .withEnv("POSTGRES_SQL_SERVICE_PORT", "5432"); - } - - private GenericContainer getRBACPostgreSQLContainer(Network rbacNetwork) { - return new PostgreSQLContainer().withDatabaseName("rb_database").withUsername("rbac_username") - .withPassword("rbac_password").withNetwork(rbacNetwork).withNetworkAliases("rbac_db"); - } + .withNetwork(rbacNetwork).withNetworkAliases("rbac").withExposedPorts(8000) + .withEnv("DATABASE_SERVICE_NAME", "POSTGRES_SQL").withEnv("DATABASE_ENGINE", "postgresql") + .withEnv("DATABASE_NAME", "rb_database").withEnv("DATABASE_USER", "rbac_username") + .withEnv("DATABASE_PASSWORD", "rbac_password").withEnv("POSTGRES_SQL_SERVICE_HOST", "rbac_db") + .withEnv("POSTGRES_SQL_SERVICE_PORT", "5432") + .dependsOn(rbacPostgreSQLContainer); - private GenericContainer getIngressContainer(Network network) { - return new GenericContainer(new ImageFromDockerfile() - .withDockerfile(Paths.get("src/test/resources/insights-ingress-go/Dockerfile"))).withExposedPorts(3000) - .withNetwork(network).withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("INGRESS-LOG")) - .withEnv("AWS_ACCESS_KEY_ID", "BQA2GEXO711FVBVXDWKM") - .withEnv("AWS_SECRET_ACCESS_KEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") - .withEnv("AWS_REGION", "us-east-1").withEnv("INGRESS_STAGEBUCKET", "insights-upload-perma") - .withEnv("INGRESS_REJECTBUCKET", "insights-upload-rejected") - .withEnv("INGRESS_INVENTORYURL", "http://inventory:8080/api/inventory/v1/hosts") - .withEnv("INGRESS_VALIDTOPICS", "xavier,testareno,advisortestareno,advisor") - .withEnv("OPENSHIFT_BUILD_COMMIT", "woopwoop").withEnv("INGRESS_MINIODEV", "true") - .withEnv("INGRESS_MINIOACCESSKEY", "BQA2GEXO711FVBVXDWKM") - .withEnv("INGRESS_MINIOSECRETKEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") - .withEnv("INGRESS_MINIOENDPOINT", "minio:9000").withEnv("INGRESS_KAFKABROKERS", "kafka:9092"); - } - - private KafkaContainer getKafkaContainer(Network network) { - return new KafkaContainer().withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("KAFKA-LOG")) - .withNetworkAliases("kafka").withNetwork(network); - } - - private GenericContainer getMinioCreateBucketsContainer(Network network, GenericContainer minio) { - return new GenericContainer<>("minio/mc").dependsOn(minio).withNetwork(network) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("MINIO-MC-LOG")) - .withCopyFileToContainer(MountableFile.forClasspathResource("minio-bucket-creation-commands.sh"), "/") - .withCreateContainerCmdModifier(createContainerCmd -> createContainerCmd.withEntrypoint("sh", - "/minio-bucket-creation-commands.sh", "minio:9000")); - } - - private GenericContainer getMinioContainer(Network network) { - return new GenericContainer<>("minio/minio").withCommand("server /data").withExposedPorts(9000) - .withNetworkAliases("minio").withNetwork(network) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("MINIO-LOG")) - .withEnv("MINIO_ACCESS_KEY", "BQA2GEXO711FVBVXDWKM") - .withEnv("MINIO_SECRET_KEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC"); - } - - private LocalStackContainer getLocalstackContainer() { - return new LocalStackContainer().withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AWS-LOG")) - .withServices(S3); - } - - private PostgreSQLContainer getPostgreSQLContainer() { - return new PostgreSQLContainer().withDatabaseName("sampledb").withUsername("admin").withPassword("redhat"); - } - - private GenericContainer getKIEContainer() { - return new GenericContainer<>("jboss/kie-server-showcase:7.18.0.Final").withNetworkAliases("kie-server") - .withExposedPorts(8080).withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("KIE-LOG")) - .withEnv("KIE_SERVER_ID", "analytics-kieserver").withEnv("KIE_ADMIN_USER", "kieserver") - .withEnv("KIE_ADMIN_PWD", "kieserver1!").withEnv("KIE_SERVER_MODE", "DEVELOPMENT") - .withEnv("KIE_MAVEN_REPO", "https://oss.sonatype.org/content/repositories/snapshots") - .withEnv("KIE_REPOSITORY", "https://repository.jboss.org/nexus/content/groups/public-jboss") - .withEnv("KIE_SERVER_CONTROLLER_PWD", "admin").withEnv("KIE_SERVER_CONTROLLER_USER", "admin") - .withEnv("KIE_SERVER_LOCATION", "http://kie-server:8080/kie-server/services/rest/server") - .withEnv("KIE_SERVER_PWD", "kieserver1!").withEnv("KIE_SERVER_USER", "kieserver"); - } - - private GenericContainer getActiveMQContainer() { - return (GenericContainer) new GenericContainer<>("vromero/activemq-artemis").withExposedPorts(61616, 8161) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AMQ-LOG")).withEnv("DISABLE_SECURITY", "true") - .withEnv("BROKER_CONFIG_GLOBAL_MAX_SIZE", "50000").withEnv("BROKER_CONFIG_MAX_SIZE_BYTES", "50000") - .withEnv("BROKER_CONFIG_MAX_DISK_USAGE", "100"); - } @NotNull - public String getContainerHost(GenericContainer container, Integer port) { + public static String getContainerHost(GenericContainer container, Integer port) { return container.getContainerIpAddress() + ":" + container.getMappedPort(port); } @NotNull - public String getContainerHost(GenericContainer container) { + public static String getContainerHost(GenericContainer container) { return container.getContainerIpAddress() + ":" + container.getFirstMappedPort(); } - public void createAndStartDockerContainers() throws IOException, InterruptedException { + public static void stopAndDestroyDockerContainers() { + logger.info(">>> Stopping Docker Containers"); + activemqContainer.stop(); + kie_serverContainer.stop(); + postgreSQLContainer.stop(); + localstackContainer.stop(); + minioContainer.stop(); + createbucketsContainer.stop(); + kafkaContainer.stop(); + ingressContainer.stop(); + rbacPostgreSQLContainer.stop(); + rbacServerContainer.stop(); + } + + public static void createAndStartDockerContainers() throws IOException, InterruptedException { cloneIngressRepoAndUnzip(); cloneInsightsRbacRepo_UnzipAndConfigure(); - activemq = getActiveMQContainer(); - activemq.start(); - - kie_server = getKIEContainer(); - kie_server.start(); - - postgreSQL = getPostgreSQLContainer(); - postgreSQL.start(); - - localstack = getLocalstackContainer(); - localstack.start(); - - Network network = Network.newNetwork(); - - minio = getMinioContainer(network); - minio.start(); - - Thread.sleep(5000); - createbuckets = getMinioCreateBucketsContainer(network, minio); - createbuckets.start(); - - kafka = getKafkaContainer(network); - kafka.start(); - - ingress = getIngressContainer(network); - ingress.start(); - - Network rbacNetwork = Network.newNetwork(); - rbacPostgreSQL = getRBACPostgreSQLContainer(rbacNetwork); - rbacPostgreSQL.start(); - - rbacServer = getRBACServerContainer(rbacNetwork); - rbacServer.start(); + logger.info(">>> Starting Docker Containers"); + Startables.deepStart(Stream.of(kie_serverContainer, postgreSQLContainer, localstackContainer, minioContainer, activemqContainer, + createbucketsContainer, kafkaContainer, ingressContainer, rbacPostgreSQLContainer, rbacServerContainer)) + .join(); Thread.sleep(5000); importProjectIntoKIE(); } - private void cloneIngressRepoAndUnzip() throws IOException { + private static void cloneIngressRepoAndUnzip() throws IOException { // downloading, unzipping, renaming String ingressRepoZipURL = "https://github.com/RedHatInsights/insights-ingress-go/archive/" + ingressCommitHash + ".zip"; File compressedFile = new File("src/test/resources/ingressRepo.zip"); + logger.info(">> downloading Ingress repo from " + ingressRepoZipURL); FileUtils.copyURLToFile(new URL(ingressRepoZipURL), compressedFile, 1000, 10000); unzipFile(compressedFile, "src/test/resources"); @@ -205,11 +177,12 @@ private void cloneIngressRepoAndUnzip() throws IOException { new File("src/test/resources/insights-ingress-go")); } - private void cloneInsightsRbacRepo_UnzipAndConfigure() throws IOException { + private static void cloneInsightsRbacRepo_UnzipAndConfigure() throws IOException { // downloading, unzipping, renaming String insightsRbacRepoZipURL = "https://github.com/RedHatInsights/insights-rbac/archive/" + insightsRbacCommitHash + ".zip"; File compressedFile = new File("src/test/resources/insightsRbacRepo.zip"); + logger.info(">> downloading RBAC repo from " + insightsRbacRepoZipURL); FileUtils.copyURLToFile(new URL(insightsRbacRepoZipURL), compressedFile, 1000, 10000); unzipFile(compressedFile, "src/test/resources"); @@ -226,7 +199,7 @@ private void cloneInsightsRbacRepo_UnzipAndConfigure() throws IOException { new File("src/test/resources/insights-rbac/rbac/management/role/definitions/migration-analytics.json")); } - private void unzipFile(File file, String outputDir) throws IOException { + private static void unzipFile(File file, String outputDir) throws IOException { java.util.zip.ZipFile zipFile = new ZipFile(file); try { Enumeration entries = zipFile.entries(); @@ -249,11 +222,11 @@ private void unzipFile(File file, String outputDir) throws IOException { } } - public String getHostForKie() { - return kie_server.getContainerIpAddress() + ":" + kie_server.getFirstMappedPort(); + public static String getHostForKie() { + return kie_serverContainer.getContainerIpAddress() + ":" + kie_serverContainer.getFirstMappedPort(); } - private void importProjectIntoKIE() throws InterruptedException, IOException { + private static void importProjectIntoKIE() throws InterruptedException, IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setCacheControl("no-cache"); @@ -275,44 +248,71 @@ private void importProjectIntoKIE() throws InterruptedException, IOException { } } - public GenericContainer getKie_server() { - return kie_server; + public static GenericContainer getKie_server() { + return kie_serverContainer; } - public PostgreSQLContainer getPostgreSQL() { - return postgreSQL; + public static PostgreSQLContainer getPostgreSQL() { + return postgreSQLContainer; } - public LocalStackContainer getLocalstack() { - return localstack; + public static LocalStackContainer getLocalstack() { + return localstackContainer; } - public GenericContainer getActivemq() { - return activemq; + public static GenericContainer getActivemq() { + return activemqContainer; } - public GenericContainer getMinio() { - return minio; + public static GenericContainer getMinio() { + return minioContainer; } - public GenericContainer getCreatebuckets() { - return createbuckets; + public static GenericContainer getCreatebuckets() { + return createbucketsContainer; } - public KafkaContainer getKafka() { - return kafka; + public static KafkaContainer getKafka() { + return kafkaContainer; } - public GenericContainer getIngress() { - return ingress; + public static GenericContainer getIngress() { + return ingressContainer; } - public GenericContainer getRbacPostgreSQL() { - return rbacPostgreSQL; + public static GenericContainer getRbacPostgreSQL() { + return rbacPostgreSQLContainer; } - public GenericContainer getRbacServer() { - return rbacServer; + public static GenericContainer getRbacServer() { + return rbacServerContainer; } + public static class SpringBootInitializerTestContainers implements ApplicationContextInitializer { + + @Override + public void initialize(ConfigurableApplicationContext configurableApplicationContext) { + + EnvironmentTestUtils.addEnvironment("test", configurableApplicationContext.getEnvironment(), + "amq.server=" + getActivemq().getContainerIpAddress(), + "amq.port=" + getActivemq().getMappedPort(61616), + "minio.host=" + getContainerHost(getMinio(), 9000), + "insights.upload.host=" + getContainerHost(getIngress()), + "insights.properties=yearOverYearGrowthRatePercentage,percentageOfHypervisorsMigratedOnYear1,percentageOfHypervisorsMigratedOnYear2,percentageOfHypervisorsMigratedOnYear3,reportName,reportDescription", +// "camel.component.servlet.mapping.context-path=/api/xavier/*", + "insights.kafka.host=" + getKafka().getBootstrapServers(), + "postgresql.service.name=" + getPostgreSQL().getContainerIpAddress(), + "postgresql.service.port=" + getPostgreSQL().getFirstMappedPort(), + "spring.datasource.username=" + getPostgreSQL().getUsername(), + "spring.datasource.password=" + getPostgreSQL().getPassword(), + "S3_HOST=" + getLocalstack().getEndpointConfiguration(S3).getServiceEndpoint(), + "S3_REGION="+ getLocalstack().getEndpointConfiguration(S3).getSigningRegion(), + "kieserver.devel-service=" + getHostForKie() + "/kie-server", + "spring.datasource.url = jdbc:postgresql://" + getContainerHost(getPostgreSQL()) + "/sampledb" , + "spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect" , + "thread.concurrentConsumers=3", + "insights.rbac.path=/api/v1/access/", + "insights.rbac.host=" + "http://" + getContainerHost(getRbacServer(), 8000)); + } + } } \ No newline at end of file From 81b3b885dca1d2b208009278a356ae94867b9374 Mon Sep 17 00:00:00 2001 From: jonathan Date: Mon, 20 Jul 2020 21:17:00 +0200 Subject: [PATCH 13/26] refactor-end2end Split the start of containers in 3 groups and individuals --- .../org/jboss/xavier/integrations/EndToEndTest.java | 13 +------------ .../integrations/TestContainersInfrastructure.java | 11 +++++++---- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index c3a398ad..4680be13 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -364,18 +364,7 @@ public void configure() { } } - @After - public void closeCamel() throws Exception { - testsExecuted++; - logger.info("After test method ..."); - - if (testsExecuted == 10) { - logger.info("CLOSING CAMEL CONTEXT >>>>>>>>"); - camelContext.stop(); - } - } - - @Test + @Test public void whenRegularTestShouldAnswerInTime() throws Exception { logger.info("+++++++ Regular Test ++++++"); // Start the camel route as if the UI was sending the file to the Camel Rest diff --git a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java index 25e27384..f37ef2dc 100644 --- a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java +++ b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java @@ -106,7 +106,7 @@ public class TestContainersInfrastructure { .withEnv("INGRESS_MINIOSECRETKEY", "uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC") .withEnv("INGRESS_MINIOENDPOINT", "minio:9000") .withEnv("INGRESS_KAFKABROKERS", "kafka:9092") - .dependsOn(kafkaContainer,minioContainer,createbucketsContainer); + .dependsOn(kafkaContainer,localstackContainer, minioContainer,createbucketsContainer); // RBAC @@ -155,9 +155,12 @@ public static void createAndStartDockerContainers() throws IOException, Interrup cloneInsightsRbacRepo_UnzipAndConfigure(); logger.info(">>> Starting Docker Containers"); - Startables.deepStart(Stream.of(kie_serverContainer, postgreSQLContainer, localstackContainer, minioContainer, activemqContainer, - createbucketsContainer, kafkaContainer, ingressContainer, rbacPostgreSQLContainer, rbacServerContainer)) - .join(); + Startables.deepStart(Stream.of(kie_serverContainer, postgreSQLContainer, localstackContainer)).join(); + Startables.deepStart(Stream.of(activemqContainer, kafkaContainer)).join(); + Startables.deepStart(Stream.of(minioContainer, createbucketsContainer)).join(); + ingressContainer.start(); + rbacPostgreSQLContainer.start(); + rbacServerContainer.start(); Thread.sleep(5000); importProjectIntoKIE(); From a90abe3ecce234a9822752655f5fdaf75451e6de Mon Sep 17 00:00:00 2001 From: jonathan Date: Tue, 21 Jul 2020 09:46:58 +0200 Subject: [PATCH 14/26] refactor-end2end modified travis in order to only execute endtoendtest to see the time --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 54a974e2..9f4daac7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,7 +49,7 @@ jobs: - LOGGING_LEVEL_ROOT=info - ANALYSIS_DATAINTEGRITY_LOG=false script: - - mvn test -Pcoverage -Danalysis.dataintegrity.log=false + - mvn test -Pcoverage -Dtest=EndToEndTest -Danalysis.dataintegrity.log=false - 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then sonar-scanner; fi' - bash <(curl -s https://codecov.io/bash) From b229bd309214412213c314ca7c8682334b7bfc8d Mon Sep 17 00:00:00 2001 From: jonathan Date: Tue, 21 Jul 2020 12:39:20 +0200 Subject: [PATCH 15/26] refactor-end2end ensuring info is captured before deleting --- .../xavier/integrations/EndToEndTest.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 4680be13..a39e581f 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -859,6 +859,24 @@ public void whenDeleteReportShouldRemoveFileInS3() throws Exception { return getStorageObjectsSize() == s3ObjectsBefore + 1; }); + ResponseEntity workloadSummaryReport = new RestTemplate().exchange( + getBaseURLAPIPath() + String.format("/report/%d/workload-summary", analysisNum), HttpMethod.GET, + getRequestEntity(), new ParameterizedTypeReference() { + }); + // Call initialCostSavingsReport + ResponseEntity initialCostSavingsReport = new RestTemplate().exchange( + getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference() { + }); + + // Call workloadInventoryReport + ResponseEntity> workloadInventoryReport = new RestTemplate() + .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), + HttpMethod.GET, getRequestEntity(), + new ParameterizedTypeReference>() { + }); + ResponseEntity stringEntity = new RestTemplate().exchange( getBaseURLAPIPath() + String.format("/report/%d", analysisNum), HttpMethod.DELETE, getRequestEntity(), new ParameterizedTypeReference() { From f437b5bfdd3af1970ffb1281e250270b45932ca1 Mon Sep 17 00:00:00 2001 From: jonathan Date: Wed, 22 Jul 2020 10:49:37 +0200 Subject: [PATCH 16/26] end2end refactor Split startup of containers --- .../xavier/integrations/EndToEndTest.java | 37 +++++++++---------- .../TestContainersInfrastructure.java | 7 ++-- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index a39e581f..99dc40b2 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -58,6 +58,7 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; @@ -364,6 +365,17 @@ public void configure() { } } + @After + public void closeCamel() throws Exception { + testsExecuted++; + logger.info("After test method ..."); + + if (testsExecuted == 7) { + logger.info("CLOSING CAMEL CONTEXT >>>>>>>>"); + camelContext.stop(); + } + } + @Test public void whenRegularTestShouldAnswerInTime() throws Exception { logger.info("+++++++ Regular Test ++++++"); @@ -792,6 +804,7 @@ HttpMethod.GET, getRequestEntity(), } @Test + @Ignore public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { // Ultra Performance test logger.info("+++++++ Ultra Performance Test ++++++"); @@ -805,6 +818,7 @@ public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { } @Test + @Ignore public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exception { // Stress test // We load 2 times a BIG file ( 8 Mb ) and 2 times a small file ( 316 Kb ) @@ -853,34 +867,19 @@ public void whenDeleteReportShouldRemoveFileInS3() throws Exception { // we upload a file to be sure there's one report to delete, as it could be that // this test is executed the first new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( - "cloudforms-export-v1_0_0-vm_with_used_disk_storage.json", "application/json"), String.class); + "cloudforms-export-v1_0_0.tar.gz", "application/json"), String.class); analysisNum++; await().atMost(5000, TimeUnit.MILLISECONDS).with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> { return getStorageObjectsSize() == s3ObjectsBefore + 1; }); - - ResponseEntity workloadSummaryReport = new RestTemplate().exchange( - getBaseURLAPIPath() + String.format("/report/%d/workload-summary", analysisNum), HttpMethod.GET, - getRequestEntity(), new ParameterizedTypeReference() { - }); - // Call initialCostSavingsReport - ResponseEntity initialCostSavingsReport = new RestTemplate().exchange( - getBaseURLAPIPath() + String.format("/report/%d/initial-saving-estimation", analysisNum), - HttpMethod.GET, getRequestEntity(), - new ParameterizedTypeReference() { - }); - - // Call workloadInventoryReport - ResponseEntity> workloadInventoryReport = new RestTemplate() - .exchange(getBaseURLAPIPath() + String.format("/report/%d/workload-inventory?limit=100", analysisNum), - HttpMethod.GET, getRequestEntity(), - new ParameterizedTypeReference>() { - }); + logger.info("... after upload"); ResponseEntity stringEntity = new RestTemplate().exchange( getBaseURLAPIPath() + String.format("/report/%d", analysisNum), HttpMethod.DELETE, getRequestEntity(), new ParameterizedTypeReference() { }); + logger.info("... after report"); + assertThat(stringEntity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); assertThat(initialSavingsEstimationReportService.findByAnalysisOwnerAndAnalysisId("dummy@redhat.com", Long.valueOf(analysisNum))).isNull(); diff --git a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java index f37ef2dc..0cef9602 100644 --- a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java +++ b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java @@ -155,9 +155,10 @@ public static void createAndStartDockerContainers() throws IOException, Interrup cloneInsightsRbacRepo_UnzipAndConfigure(); logger.info(">>> Starting Docker Containers"); - Startables.deepStart(Stream.of(kie_serverContainer, postgreSQLContainer, localstackContainer)).join(); - Startables.deepStart(Stream.of(activemqContainer, kafkaContainer)).join(); - Startables.deepStart(Stream.of(minioContainer, createbucketsContainer)).join(); + minioContainer.start(); + Startables.deepStart(Stream.of(activemqContainer, postgreSQLContainer, localstackContainer)).join(); + createbucketsContainer.start(); + Startables.deepStart(Stream.of(kie_serverContainer, kafkaContainer)).join(); ingressContainer.start(); rbacPostgreSQLContainer.start(); rbacServerContainer.start(); From d8af74e67aaabaa2a06676641527f0bf1170bedc Mon Sep 17 00:00:00 2001 From: jonathan Date: Wed, 22 Jul 2020 10:50:11 +0200 Subject: [PATCH 17/26] endtoend refactor removed @Ignore --- src/test/java/org/jboss/xavier/integrations/EndToEndTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 99dc40b2..742cb889 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -804,7 +804,6 @@ HttpMethod.GET, getRequestEntity(), } @Test - @Ignore public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { // Ultra Performance test logger.info("+++++++ Ultra Performance Test ++++++"); @@ -818,7 +817,6 @@ public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { } @Test - @Ignore public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exception { // Stress test // We load 2 times a BIG file ( 8 Mb ) and 2 times a small file ( 316 Kb ) From 7b1bed3dfe03a4c9e6d42ea87befe8a06e129190 Mon Sep 17 00:00:00 2001 From: jonathan Date: Wed, 22 Jul 2020 11:56:46 +0200 Subject: [PATCH 18/26] refactor All containers started in sequence --- .../xavier/integrations/TestContainersInfrastructure.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java index 0cef9602..94dd4b5a 100644 --- a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java +++ b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java @@ -156,9 +156,12 @@ public static void createAndStartDockerContainers() throws IOException, Interrup logger.info(">>> Starting Docker Containers"); minioContainer.start(); - Startables.deepStart(Stream.of(activemqContainer, postgreSQLContainer, localstackContainer)).join(); + activemqContainer.start(); + postgreSQLContainer.start(); + localstackContainer.start(); createbucketsContainer.start(); - Startables.deepStart(Stream.of(kie_serverContainer, kafkaContainer)).join(); + kie_serverContainer.start(); + kafkaContainer.start(); ingressContainer.start(); rbacPostgreSQLContainer.start(); rbacServerContainer.start(); From edcebb3baf2b2537a5663aa5eb2373d1ee091296 Mon Sep 17 00:00:00 2001 From: jonathan Date: Wed, 22 Jul 2020 15:06:27 +0200 Subject: [PATCH 19/26] endtoend refator - adding an await until summaryreport is done on the delete test --- .../jboss/xavier/integrations/EndToEndTest.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 742cb889..4033355b 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -370,7 +370,7 @@ public void closeCamel() throws Exception { testsExecuted++; logger.info("After test method ..."); - if (testsExecuted == 7) { + if (testsExecuted == 10) { logger.info("CLOSING CAMEL CONTEXT >>>>>>>>"); camelContext.stop(); } @@ -865,13 +865,18 @@ public void whenDeleteReportShouldRemoveFileInS3() throws Exception { // we upload a file to be sure there's one report to delete, as it could be that // this test is executed the first new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( - "cloudforms-export-v1_0_0.tar.gz", "application/json"), String.class); + "cloudforms-export-v1_0_0.json", "application/json"), String.class); analysisNum++; - await().atMost(5000, TimeUnit.MILLISECONDS).with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> { - return getStorageObjectsSize() == s3ObjectsBefore + 1; - }); logger.info("... after upload"); + await().atMost(10000, TimeUnit.MILLISECONDS).with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> { + return getStorageObjectsSize() == s3ObjectsBefore + 1; + }); + logger.info("... after S3 check"); + + assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), + timeoutMilliseconds_SmallFileSummaryReport)).isEqualTo(8); + ResponseEntity stringEntity = new RestTemplate().exchange( getBaseURLAPIPath() + String.format("/report/%d", analysisNum), HttpMethod.DELETE, getRequestEntity(), new ParameterizedTypeReference() { From 6fd6bd37c34c19f523176646f127af6a46caaf6e Mon Sep 17 00:00:00 2001 From: jonathan Date: Wed, 22 Jul 2020 15:07:18 +0200 Subject: [PATCH 20/26] endtoend refactor - removed constraint on tests to execute on travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9f4daac7..a3822c94 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,7 +49,7 @@ jobs: - LOGGING_LEVEL_ROOT=info - ANALYSIS_DATAINTEGRITY_LOG=false script: - - mvn test -Pcoverage -Dtest=EndToEndTest -Danalysis.dataintegrity.log=false + - mvn test -Pcoverage -Danalysis.dataintegrity.log=false - 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then sonar-scanner; fi' - bash <(curl -s https://codecov.io/bash) From 94e12ab4985519b767ea12ebf42cba95ade67980 Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 23 Jul 2020 11:11:48 +0200 Subject: [PATCH 21/26] endtoend refactor Increased waiting time for S3 object deletion --- src/test/java/org/jboss/xavier/integrations/EndToEndTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 4033355b..74b11a4a 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -869,7 +869,7 @@ public void whenDeleteReportShouldRemoveFileInS3() throws Exception { analysisNum++; logger.info("... after upload"); - await().atMost(10000, TimeUnit.MILLISECONDS).with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> { + await().atMost(20000, TimeUnit.MILLISECONDS).with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS).until(() -> { return getStorageObjectsSize() == s3ObjectsBefore + 1; }); logger.info("... after S3 check"); From 73e009ecc1a0695255aa85968321eca0769d80aa Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 23 Jul 2020 12:04:15 +0200 Subject: [PATCH 22/26] end2end refactor Removed AMQ --- .../integrations/TestContainersInfrastructure.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java index 94dd4b5a..8fbc254e 100644 --- a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java +++ b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java @@ -60,12 +60,6 @@ public class TestContainersInfrastructure { protected static LocalStackContainer localstackContainer = new LocalStackContainer().withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AWS-LOG")) .withServices(S3); - protected static GenericContainer activemqContainer=new GenericContainer<>("vromero/activemq-artemis").withExposedPorts(61616, 8161) - .withLogConsumer(new Slf4jLogConsumer(logger).withPrefix("AMQ-LOG")).withEnv("DISABLE_SECURITY", "true") - .withEnv("BROKER_CONFIG_GLOBAL_MAX_SIZE", "50000").withEnv("BROKER_CONFIG_MAX_SIZE_BYTES", "50000") - .withEnv("BROKER_CONFIG_MAX_DISK_USAGE", "100"); - - // INGRESS private static Network ingressNetwork = Network.newNetwork(); @@ -138,7 +132,6 @@ public static String getContainerHost(GenericContainer container) { public static void stopAndDestroyDockerContainers() { logger.info(">>> Stopping Docker Containers"); - activemqContainer.stop(); kie_serverContainer.stop(); postgreSQLContainer.stop(); localstackContainer.stop(); @@ -156,7 +149,6 @@ public static void createAndStartDockerContainers() throws IOException, Interrup logger.info(">>> Starting Docker Containers"); minioContainer.start(); - activemqContainer.start(); postgreSQLContainer.start(); localstackContainer.start(); createbucketsContainer.start(); @@ -267,10 +259,6 @@ public static LocalStackContainer getLocalstack() { return localstackContainer; } - public static GenericContainer getActivemq() { - return activemqContainer; - } - public static GenericContainer getMinio() { return minioContainer; } @@ -301,8 +289,6 @@ public static class SpringBootInitializerTestContainers implements ApplicationCo public void initialize(ConfigurableApplicationContext configurableApplicationContext) { EnvironmentTestUtils.addEnvironment("test", configurableApplicationContext.getEnvironment(), - "amq.server=" + getActivemq().getContainerIpAddress(), - "amq.port=" + getActivemq().getMappedPort(61616), "minio.host=" + getContainerHost(getMinio(), 9000), "insights.upload.host=" + getContainerHost(getIngress()), "insights.properties=yearOverYearGrowthRatePercentage,percentageOfHypervisorsMigratedOnYear1,percentageOfHypervisorsMigratedOnYear2,percentageOfHypervisorsMigratedOnYear3,reportName,reportDescription", From 96d1db8dcc060a10278dbd2839136d7b2f50bcf1 Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 23 Jul 2020 13:07:32 +0200 Subject: [PATCH 23/26] end2end refactor Changed management port to avoid issue on used port on Travis --- src/main/resources/application-test.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties index 729bc755..02dc2eec 100644 --- a/src/main/resources/application-test.properties +++ b/src/main/resources/application-test.properties @@ -1,5 +1,5 @@ # Binding health checks to an internal port -management.port=8081 +management.port=9082 server.port=9081 # disable all management enpoints except health From 7827c0ec31c0638fbdee7519d0a9c64adb008d3f Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 23 Jul 2020 15:28:00 +0200 Subject: [PATCH 24/26] endtoend refactor added dirtiescontext to endtoendtest --- src/main/resources/application-test.properties | 2 +- src/test/java/org/jboss/xavier/integrations/EndToEndTest.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties index 02dc2eec..729bc755 100644 --- a/src/main/resources/application-test.properties +++ b/src/main/resources/application-test.properties @@ -1,5 +1,5 @@ # Binding health checks to an internal port -management.port=9082 +management.port=8081 server.port=9081 # disable all management enpoints except health diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 74b11a4a..77610c89 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -74,6 +74,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.util.LinkedMultiValueMap; @@ -86,6 +87,7 @@ @ContextConfiguration(initializers = TestContainersInfrastructure.SpringBootInitializerTestContainers.class) @Import(TestConfigurationS3.class) @ActiveProfiles("test") +@DirtiesContext public class EndToEndTest extends TestContainersInfrastructure { private Logger logger = LoggerFactory.getLogger(EndToEndTest.class); From c1ee1eeb2029fede26169802f13f1d35267ffbfe Mon Sep 17 00:00:00 2001 From: jonathan Date: Thu, 23 Jul 2020 17:08:57 +0200 Subject: [PATCH 25/26] refactor-endtoend cleaned imports and annotations --- .travis.yml | 2 +- .../org/jboss/xavier/integrations/EndToEndTest.java | 4 ++-- .../integrations/TestContainersInfrastructure.java | 6 +----- ...ACRouteBuilder_DirectCheckRbacPermissionsTest.java | 11 ++++++----- ...outeBuilder_DirectFetchRbacAndProcessDataTest.java | 1 + ...BACRouteBuilder_DirectFetchRbacUserAccessTest.java | 3 +++ 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index a3822c94..54a974e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,7 +49,7 @@ jobs: - LOGGING_LEVEL_ROOT=info - ANALYSIS_DATAINTEGRITY_LOG=false script: - - mvn test -Pcoverage -Danalysis.dataintegrity.log=false + - mvn test -Pcoverage -Danalysis.dataintegrity.log=false - 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then sonar-scanner; fi' - bash <(curl -s https://codecov.io/bash) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 77610c89..6069bb8b 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.awaitility.Awaitility.await; + import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -58,7 +59,6 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; @@ -83,7 +83,7 @@ @RunWith(CamelSpringBootRunner.class) @UseAdviceWith // Disables automatic start of Camel context -@SpringBootTest(classes = { Application.class }, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @ContextConfiguration(initializers = TestContainersInfrastructure.SpringBootInitializerTestContainers.class) @Import(TestConfigurationS3.class) @ActiveProfiles("test") diff --git a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java index 8fbc254e..485c36b0 100644 --- a/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java +++ b/src/test/java/org/jboss/xavier/integrations/TestContainersInfrastructure.java @@ -10,7 +10,6 @@ import java.net.URL; import java.nio.file.Paths; import java.util.Enumeration; -import java.util.stream.Stream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -36,7 +35,6 @@ import org.testcontainers.containers.localstack.LocalStackContainer; import org.testcontainers.containers.output.Slf4jLogConsumer; import org.testcontainers.images.builder.ImageFromDockerfile; -import org.testcontainers.lifecycle.Startables; import org.testcontainers.utility.MountableFile; public class TestContainersInfrastructure { @@ -158,7 +156,6 @@ public static void createAndStartDockerContainers() throws IOException, Interrup rbacPostgreSQLContainer.start(); rbacServerContainer.start(); - Thread.sleep(5000); importProjectIntoKIE(); } @@ -292,7 +289,6 @@ public void initialize(ConfigurableApplicationContext configurableApplicationCon "minio.host=" + getContainerHost(getMinio(), 9000), "insights.upload.host=" + getContainerHost(getIngress()), "insights.properties=yearOverYearGrowthRatePercentage,percentageOfHypervisorsMigratedOnYear1,percentageOfHypervisorsMigratedOnYear2,percentageOfHypervisorsMigratedOnYear3,reportName,reportDescription", -// "camel.component.servlet.mapping.context-path=/api/xavier/*", "insights.kafka.host=" + getKafka().getBootstrapServers(), "postgresql.service.name=" + getPostgreSQL().getContainerIpAddress(), "postgresql.service.port=" + getPostgreSQL().getFirstMappedPort(), @@ -308,4 +304,4 @@ public void initialize(ConfigurableApplicationContext configurableApplicationCon "insights.rbac.host=" + "http://" + getContainerHost(getRbacServer(), 8000)); } } -} \ No newline at end of file +} diff --git a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java index 83ed0fc8..0dc99bca 100644 --- a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java +++ b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java @@ -1,5 +1,10 @@ package org.jboss.xavier.integrations.rbac; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.ArrayList; +import java.util.List; + import org.apache.camel.Exchange; import org.jboss.xavier.Application; import org.jboss.xavier.integrations.route.XavierCamelTest; @@ -8,11 +13,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; -import java.util.ArrayList; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; - +@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RBACRouteBuilder_DirectCheckRbacPermissionsTest extends XavierCamelTest { @Value("${camel.component.servlet.mapping.context-path}") diff --git a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacAndProcessDataTest.java b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacAndProcessDataTest.java index 2fdbe6e0..bf423481 100644 --- a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacAndProcessDataTest.java +++ b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacAndProcessDataTest.java @@ -21,6 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat; +@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RBACRouteBuilder_DirectFetchRbacAndProcessDataTest extends XavierCamelTest { @Value("${camel.component.servlet.mapping.context-path}") diff --git a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacUserAccessTest.java b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacUserAccessTest.java index 41ffad84..fde78611 100644 --- a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacUserAccessTest.java +++ b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectFetchRbacUserAccessTest.java @@ -7,11 +7,13 @@ import org.apache.camel.component.http4.HttpMethods; import org.apache.http.HttpHeaders; import org.apache.http.entity.ContentType; +import org.jboss.xavier.Application; import org.jboss.xavier.integrations.route.XavierCamelTest; import org.jboss.xavier.integrations.util.TestUtil; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.Arrays; @@ -20,6 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat; +@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RBACRouteBuilder_DirectFetchRbacUserAccessTest extends XavierCamelTest { @Value("${camel.component.servlet.mapping.context-path}") From b54a89eb928871dbae8fea65e5e1229dcfefbf2a Mon Sep 17 00:00:00 2001 From: jonathan Date: Fri, 24 Jul 2020 13:05:35 +0200 Subject: [PATCH 26/26] refactor-endtoend Removed Performance test as it's duplicated on Stress test --- .../org/jboss/xavier/integrations/EndToEndTest.java | 13 ------------- ...RouteBuilder_DirectCheckRbacPermissionsTest.java | 10 +++++----- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java index 6069bb8b..f85a2d71 100644 --- a/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java +++ b/src/test/java/org/jboss/xavier/integrations/EndToEndTest.java @@ -805,19 +805,6 @@ HttpMethod.GET, getRequestEntity(), } - @Test - public void whenBigFileAnalisedItShouldEndOnTime() throws Exception { - // Ultra Performance test - logger.info("+++++++ Ultra Performance Test ++++++"); - analysisNum++; - new RestTemplate().postForEntity(getBaseURLAPIPath() + "/upload", getRequestEntityForUploadRESTCall( - "cfme_inventory20190807-32152-jimd0q_large_dataset_5254_vms.tar.gz", "application/zip"), String.class); - assertThat(callSummaryReportAndCheckVMs(String.format("/report/%d/workload-summary", analysisNum), - timeoutMilliseconds_UltraPerformaceTest)).isEqualTo(numberVMsExpected_InBigFile); - logger.info("------- End Ultra Performance Test ------"); - - } - @Test public void whenSeveralAnalysisRunningLargerShouldNotAffectSmaller() throws Exception { // Stress test diff --git a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java index 0dc99bca..947fdd2a 100644 --- a/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java +++ b/src/test/java/org/jboss/xavier/integrations/rbac/RBACRouteBuilder_DirectCheckRbacPermissionsTest.java @@ -1,10 +1,5 @@ package org.jboss.xavier.integrations.rbac; -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.ArrayList; -import java.util.List; - import org.apache.camel.Exchange; import org.jboss.xavier.Application; import org.jboss.xavier.integrations.route.XavierCamelTest; @@ -13,6 +8,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + @SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class RBACRouteBuilder_DirectCheckRbacPermissionsTest extends XavierCamelTest {