3de06c36dc4557ae9fd0d062838183084fd98204
[integration/simulators/pnf-simulator.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.integration.suites;
22
23 import com.palantir.docker.compose.DockerComposeRule;
24 import com.palantir.docker.compose.connection.waiting.HealthChecks;
25 import org.junit.BeforeClass;
26 import org.junit.ClassRule;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Suite;
29 import org.junit.runners.Suite.SuiteClasses;
30 import org.onap.pnfsimulator.integration.BasicAvailabilityTest;
31 import org.onap.pnfsimulator.integration.OptionalTemplatesTest;
32 import org.onap.pnfsimulator.integration.SearchInTemplatesTest;
33 import org.onap.pnfsimulator.integration.TemplatesManagementTest;
34 import org.onap.pnfsimulator.integration.VariablesReplacement;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.http.HttpStatus;
38
39 import static io.restassured.RestAssured.given;
40
41 @RunWith(Suite.class)
42 @SuiteClasses({BasicAvailabilityTest.class, TemplatesManagementTest.class, OptionalTemplatesTest.class,
43     SearchInTemplatesTest.class, VariablesReplacement.class})
44 public class DockerBasedTestsSuite {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(DockerBasedTestsSuite.class);
47
48     private static final String HEALTH_CHECK_ADDRESS = "http://0.0.0.0:5000/health";
49     private static final int RETRY_COUNT = 10;
50     private static final int RETRY_INTERVAL = 1000;
51
52     @ClassRule
53     public static DockerComposeRule docker = DockerComposeRule.builder()
54         .file("../docker-compose.yml")
55         .waitingForService("pnf-simulator", HealthChecks.toHaveAllPortsOpen())
56         .waitingForService("mongo", HealthChecks.toHaveAllPortsOpen())
57         .build();
58
59     @BeforeClass
60     public static void waitForPnfSimulatorToBeHealthy() throws InterruptedException {
61         boolean isHealthy = false;
62         int retry = 0;
63         while (!isHealthy && retry < RETRY_COUNT) {
64             retry++;
65             LOGGER.info("Checking PNF health, try {} out of {}", retry, RETRY_COUNT);
66             isHealthy = performHealthCheck();
67             if (isHealthy) {
68                 LOGGER.info("PNF is healthy");
69             } else {
70                 LOGGER.info("PNF no healthy retrying in  {}", RETRY_COUNT);
71                 Thread.sleep(RETRY_INTERVAL);
72             }
73         }
74     }
75
76     private static boolean performHealthCheck() {
77         boolean isUp = false;
78         try {
79             int statusCode = given().get(HEALTH_CHECK_ADDRESS).getStatusCode();
80             if (statusCode == HttpStatus.OK.value()) {
81                 isUp = true;
82             }
83         } catch (Exception e) {
84             e.printStackTrace();
85         }
86         return isUp;
87     }
88
89 }