diff --git a/qa/common/src/main/java/org/eclipse/kapua/qa/common/utils/InitShiro.java b/qa/common/src/main/java/org/eclipse/kapua/qa/common/utils/InitShiro.java index 93486350fed..43b08e79023 100644 --- a/qa/common/src/main/java/org/eclipse/kapua/qa/common/utils/InitShiro.java +++ b/qa/common/src/main/java/org/eclipse/kapua/qa/common/utils/InitShiro.java @@ -27,26 +27,25 @@ @ScenarioScoped public class InitShiro { - private static final Logger logger = LoggerFactory.getLogger(InitShiro.class); + private static final Logger LOG = LoggerFactory.getLogger(InitShiro.class); @Given("^Init Security Context$") public void start() throws IOException { - logger.info("Init shiro security manager..."); + LOG.info("Init shiro security manager..."); try { SecurityManager securityManager = SecurityUtils.getSecurityManager(); - logger.info("Found Shiro security manager {}", securityManager); + LOG.info("Found Shiro security manager {}", securityManager); } catch (UnavailableSecurityManagerException e) { - logger.info("Init shiro security manager..."); + LOG.info("Init shiro security manager..."); SecurityUtil.initSecurityManager(); } - logger.info("Init shiro security manager... DONE"); + LOG.info("Init shiro security manager... DONE"); } @Given("^Reset Security Context$") public void stop() { - logger.info("Reset shiro security manager..."); + LOG.info("Reset shiro security manager..."); SecurityUtils.setSecurityManager(null); - logger.info("Reset shiro security manager... DONE"); + LOG.info("Reset shiro security manager... DONE"); } - } diff --git a/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/DockerSteps.java b/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/DockerSteps.java index 981f7631853..e0770152bfc 100644 --- a/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/DockerSteps.java +++ b/qa/integration-steps/src/main/java/org/eclipse/kapua/qa/integration/steps/DockerSteps.java @@ -12,8 +12,6 @@ *******************************************************************************/ package org.eclipse.kapua.qa.integration.steps; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; import com.google.inject.Singleton; @@ -49,7 +47,6 @@ import javax.inject.Inject; import java.io.BufferedReader; -import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; @@ -57,6 +54,7 @@ import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -76,7 +74,7 @@ public class DockerSteps { private static final String API_IMAGE = "kapua-api"; private static final List DEFAULT_DEPLOYMENT_CONTAINERS_NAME; private static final List DEFAULT_BASE_DEPLOYMENT_CONTAINERS_NAME; - private static final int WAIT_COUNT = 120;//total wait time = 240 secs (120 * 2000ms) + private static final int WAIT_COUNT = 120; //total wait time = 240 secs (120 * 2000ms) private static final long WAIT_STEP = 2000; private static final long WAIT_FOR_DB = 10000; private static final long WAIT_FOR_ES = 10000; @@ -106,6 +104,7 @@ public class DockerSteps { DEFAULT_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.AUTH_SERVICE_CONTAINER_NAME); DEFAULT_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.MESSAGE_BROKER_CONTAINER_NAME); DEFAULT_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.API_CONTAINER_NAME); + DEFAULT_BASE_DEPLOYMENT_CONTAINERS_NAME = new ArrayList<>(); DEFAULT_BASE_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.JOB_ENGINE_CONTAINER_NAME); DEFAULT_BASE_DEPLOYMENT_CONTAINERS_NAME.add(BasicSteps.EVENTS_BROKER_CONTAINER_NAME); @@ -235,6 +234,90 @@ public void startFullDockerEnvironment() throws Exception { } } + /** + * Starts Docker container requested. + * + * @param dockerContainers The Docker containers to start + * @throws Exception + * @since 2.1.0 + */ + @Given("Start Docker environment with resources") + public void startDockerEnvironmentWithResources(List dockerContainers) throws Exception { + cleanDockerEnvironmentInternal(); + + pullImage(ES_IMAGE); + + createNetwork(); + + for (String dockerContainer : dockerContainers) { + switch (dockerContainer) { + case "db": { + startDBContainer(BasicSteps.DB_CONTAINER_NAME); + synchronized (this) { + this.wait(WAIT_FOR_DB); + } + } break; + case "es": { + startESContainer(BasicSteps.ES_CONTAINER_NAME); + synchronized (this) { + this.wait(WAIT_FOR_ES); + } + } break; + case "events-broker": { + startEventBrokerContainer(BasicSteps.EVENTS_BROKER_CONTAINER_NAME); + synchronized (this) { + this.wait(WAIT_FOR_EVENTS_BROKER); + } + } break; + case "job-engine": { + startJobEngineContainer(BasicSteps.JOB_ENGINE_CONTAINER_NAME); + synchronized (this) { + this.wait(WAIT_FOR_JOB_ENGINE); + } + } break; + case "message-broker": { + startMessageBrokerContainer(BasicSteps.MESSAGE_BROKER_CONTAINER_NAME); + synchronized (this) { + this.wait(WAIT_FOR_BROKER); + } + } break; + case "broker-auth-service": { + startAuthServiceContainer(BasicSteps.AUTH_SERVICE_CONTAINER_NAME); + + long timeout = System.currentTimeMillis(); + while (System.currentTimeMillis() - timeout < 30000) { + isServiceReady(AUTH_SERVICE_CHECK_WEB_APP); + Thread.sleep(500); + } + } break; + case "consumer-lifecycle": { + startLifecycleConsumerContainer(BasicSteps.LIFECYCLE_CONSUMER_CONTAINER_NAME); + + long timeout = System.currentTimeMillis(); + while (System.currentTimeMillis() - timeout < 30000) { + isServiceReady(LIFECYCLE_CHECK_WEB_APP); + Thread.sleep(500); + } + } break; + default: + throw new UnsupportedOperationException("Unknown container resource: " + dockerContainer); + } + } + } + + private void cleanDockerEnvironmentInternal() throws DockerException, InterruptedException { + removeContainers( + Arrays.asList( + BasicSteps.JOB_ENGINE_CONTAINER_NAME, + BasicSteps.EVENTS_BROKER_CONTAINER_NAME, + BasicSteps.ES_CONTAINER_NAME, + BasicSteps.DB_CONTAINER_NAME + ) + ); + + removeNetwork(); + } + @Given("Start base docker environment") public void startBaseDockerEnvironment() throws Exception { stopBaseDockerEnvironment(); @@ -320,14 +403,14 @@ public void startApiDockerEnvironment(String tokenTTL, String refreshTokenTTL, i } } - private boolean areServicesReady() throws JsonParseException, JsonMappingException, IOException { + private boolean areServicesReady() throws IOException { if (isServiceReady(LIFECYCLE_CHECK_WEB_APP) && isServiceReady(TELEMETRY_CHECK_WEB_APP) && isServiceReady(AUTH_SERVICE_CHECK_WEB_APP)) { return true; } return false; } - private boolean isServiceReady(String type) throws JsonParseException, JsonMappingException, IOException { + private boolean isServiceReady(String type) throws IOException { URL serviceUrl = new URL(LIFECYCLE_HEALTH_URL);//lifecycle endpoint if (TELEMETRY_CHECK_WEB_APP.equals(type)) { serviceUrl = new URL(TELEMETRY_HEALTH_URL);//telemetry endpoint @@ -336,22 +419,22 @@ private boolean isServiceReady(String type) throws JsonParseException, JsonMappi } logger.debug("Querying {} consumer status for url: {}", type, serviceUrl); HttpURLConnection conn = null; - DataOutputStream out = null; - BufferedReader in = null; - InputStreamReader isr = null; + try { conn = (HttpURLConnection) serviceUrl.openConnection(); conn.setConnectTimeout(HTTP_COMMUNICATION_TIMEOUT); conn.setReadTimeout(HTTP_COMMUNICATION_TIMEOUT); - //works with spring boot actuator servlet mappings + // Works with spring boot actuator servlet mappings conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); + int status = conn.getResponseCode(); if (status == 200) { - isr = new InputStreamReader(conn.getInputStream()); - in = new BufferedReader(isr); - return isRunning(MAPPER.readValue(in, Map.class)); + try (InputStreamReader isr = new InputStreamReader(conn.getInputStream()); + BufferedReader in = new BufferedReader(isr)) { + return isRunning(MAPPER.readValue(in, Map.class)); + } } else { logger.info("Querying {} consumer status for url: {} - ERROR", type, serviceUrl); return false; @@ -359,27 +442,6 @@ private boolean isServiceReady(String type) throws JsonParseException, JsonMappi } catch (IOException e) { //nothing to do } finally { - if (isr != null) { - try { - isr.close(); - } catch (Exception e) { - logger.warn("Cannot close InputStreamReader", e.getMessage(), e); - } - } - if (in != null) { - try { - in.close(); - } catch (Exception e) { - logger.warn("Cannot close BufferedReader", e.getMessage(), e); - } - } - if (out != null) { - try { - out.close(); - } catch (Exception e) { - logger.warn("Cannot close DataOutputStream", e.getMessage(), e); - } - } if (conn != null) { try { conn.disconnect(); @@ -636,7 +698,7 @@ public void stopContainer(List names) throws DockerException, Interrupte } @Then("Remove container with name {string}") - public void removeContainers(List names) throws DockerException, InterruptedException { + public void removeContainers(List names) { for (String name : names) { removeContainer(name); //search for images with / at the beginning diff --git a/qa/integration/src/test/resources/features/job/JobExecutionServiceI9n.feature b/qa/integration/src/test/resources/features/job/JobExecutionServiceI9n.feature index 464c1f3e2c2..4308d95271f 100644 --- a/qa/integration/src/test/resources/features/job/JobExecutionServiceI9n.feature +++ b/qa/integration/src/test/resources/features/job/JobExecutionServiceI9n.feature @@ -17,11 +17,14 @@ Feature: Job Execution service CRUD tests The Job service is responsible for maintaining the status of the target step executions. -@setup -Scenario: Init Security Context for all scenarios - Given Init Jaxb Context - And Init Security Context - And Start base docker environment + @setup + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | Scenario: Regular job execution creation diff --git a/qa/integration/src/test/resources/features/job/JobServiceI9n.feature b/qa/integration/src/test/resources/features/job/JobServiceI9n.feature index 2786ca5132b..b809ddec505 100644 --- a/qa/integration/src/test/resources/features/job/JobServiceI9n.feature +++ b/qa/integration/src/test/resources/features/job/JobServiceI9n.feature @@ -18,10 +18,13 @@ Feature: Job service CRUD tests The Job service is responsible for executing scheduled actions on various targets. @setup - Scenario: Init Security Context for all scenarios - Given Init Jaxb Context - And Init Security Context - And Start base docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | Scenario: Regular job creation diff --git a/qa/integration/src/test/resources/features/job/JobTargetsServiceI9n.feature b/qa/integration/src/test/resources/features/job/JobTargetsServiceI9n.feature index 7cece8a8fd8..0ee2d53561d 100644 --- a/qa/integration/src/test/resources/features/job/JobTargetsServiceI9n.feature +++ b/qa/integration/src/test/resources/features/job/JobTargetsServiceI9n.feature @@ -18,10 +18,13 @@ Feature: Job Target service CRUD tests The Job service is responsible for maintaining a list of job targets. @setup - Scenario: Init Security Context for all scenarios - Given Init Jaxb Context - And Init Security Context - And Start base docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | Scenario: Regular target creation diff --git a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceKeystoreStepDefinitionsI9n.feature b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceKeystoreStepDefinitionsI9n.feature index b3b11033612..3fd26b87760 100644 --- a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceKeystoreStepDefinitionsI9n.feature +++ b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceKeystoreStepDefinitionsI9n.feature @@ -17,10 +17,16 @@ Feature: Job Engine Service - Keystore Step Definitions @setup - Scenario: Start full docker environment - Given Init Jaxb Context - And Init Security Context - And Start full docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | + | message-broker | + | broker-auth-service | + | consumer-lifecycle | # # Tests diff --git a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOfflineDeviceI9n.feature b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOfflineDeviceI9n.feature index f1a8ca893cf..68be0f3623c 100644 --- a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOfflineDeviceI9n.feature +++ b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOfflineDeviceI9n.feature @@ -16,10 +16,16 @@ Feature: JobEngineService tests for restarting job with offline device @setup - Scenario: Start full docker environment - Given Init Jaxb Context - And Init Security Context - And Start full docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | + | message-broker | + | broker-auth-service | + | consumer-lifecycle | # ************************************************* # * Restarting a job with one Target and one Step * diff --git a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOnlineDeviceI9n.feature b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOnlineDeviceI9n.feature index 4d59361d657..8b17d083a16 100644 --- a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOnlineDeviceI9n.feature +++ b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOnlineDeviceI9n.feature @@ -16,10 +16,16 @@ Feature: JobEngineService restart job tests with online device @setup - Scenario: Start full docker environment - Given Init Jaxb Context - And Init Security Context - And Start full docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | + | message-broker | + | broker-auth-service | + | consumer-lifecycle | # ************************************************* # * Restarting a job with one Target and one Step * diff --git a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOnlineDeviceSecondPartI9n.feature b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOnlineDeviceSecondPartI9n.feature index 78eeaf88edb..d7168de2915 100644 --- a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOnlineDeviceSecondPartI9n.feature +++ b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceRestartOnlineDeviceSecondPartI9n.feature @@ -16,10 +16,16 @@ Feature: JobEngineService restart job tests with online device - second part @setup - Scenario: Start full docker environment - Given Init Jaxb Context - And Init Security Context - And Start full docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | + | message-broker | + | broker-auth-service | + | consumer-lifecycle | # ************************************************* # * Restarting a job with one Target and one Step * diff --git a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStartOfflineDeviceI9n.feature b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStartOfflineDeviceI9n.feature index 40228f348fb..4a918acb9b2 100644 --- a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStartOfflineDeviceI9n.feature +++ b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStartOfflineDeviceI9n.feature @@ -16,10 +16,16 @@ Feature: JobEngineService tests for starting job with offline device @setup - Scenario: Start full docker environment - Given Init Jaxb Context - And Init Security Context - And Start full docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | + | message-broker | + | broker-auth-service | + | consumer-lifecycle | # *********************************************** # * Starting a job with one Target and one Step * diff --git a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStartOnlineDeviceI9n.feature b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStartOnlineDeviceI9n.feature index d2d2c73093b..2d92386e066 100644 --- a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStartOnlineDeviceI9n.feature +++ b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStartOnlineDeviceI9n.feature @@ -16,10 +16,16 @@ Feature: JobEngineService start job tests with online device @setup - Scenario: Start full docker environment - Given Init Jaxb Context - And Init Security Context - And Start full docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | + | message-broker | + | broker-auth-service | + | consumer-lifecycle | # *********************************************** # * Starting a job with one Target and one Step * diff --git a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStopOnlineDeviceI9n.feature b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStopOnlineDeviceI9n.feature index 6769704801d..441f655016b 100644 --- a/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStopOnlineDeviceI9n.feature +++ b/qa/integration/src/test/resources/features/jobEngine/JobEngineServiceStopOnlineDeviceI9n.feature @@ -18,10 +18,16 @@ Feature: JobEngineService stop job tests with online device one target and multiple steps, multiple targets and one step and multiple targets and multiple steps. @setup - Scenario: Start full docker environment - Given Init Jaxb Context - And Init Security Context - And Start full docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | + | message-broker | + | broker-auth-service | + | consumer-lifecycle | # ***************************************************** # * Stopping a job with one Target and multiple Steps * diff --git a/qa/integration/src/test/resources/features/jobScheduling/ExecuteOnDeviceConnectI9n.feature b/qa/integration/src/test/resources/features/jobScheduling/ExecuteOnDeviceConnectI9n.feature index 5ab6fdf05da..d8f674f19ca 100644 --- a/qa/integration/src/test/resources/features/jobScheduling/ExecuteOnDeviceConnectI9n.feature +++ b/qa/integration/src/test/resources/features/jobScheduling/ExecuteOnDeviceConnectI9n.feature @@ -16,10 +16,16 @@ Feature: JobEngineService execute job on device connect @setup - Scenario: Start full docker environment - Given Init Jaxb Context - And Init Security Context - And Start full docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | + | message-broker | + | broker-auth-service | + | consumer-lifecycle | Scenario: Executing Job When Device Connected After The Specified Start Date And Time Login as the kapua-sys user and create a new job with a Command Execution job step. diff --git a/qa/integration/src/test/resources/features/jobScheduling/TriggerServiceI9n.feature b/qa/integration/src/test/resources/features/jobScheduling/TriggerServiceI9n.feature index 3c1cb73d812..ec7c4fb88ef 100644 --- a/qa/integration/src/test/resources/features/jobScheduling/TriggerServiceI9n.feature +++ b/qa/integration/src/test/resources/features/jobScheduling/TriggerServiceI9n.feature @@ -16,10 +16,13 @@ Feature: Trigger service tests @setup - Scenario: Init Security Context for all scenarios - Given Init Jaxb Context - And Init Security Context - And Start base docker environment + Scenario: Setup test resources + Given Init Security Context + And Init Jaxb Context + And Start Docker environment with resources + | db | + | events-broker | + | job-engine | Scenario: Adding "Device Connect" Schedule With All Valid Parameters Login as kapua-sys user and create a job with name job0.