Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.greenwavereality.riot.cloud;
- import com.github.dockerjava.api.DockerClient;
- import com.github.dockerjava.api.model.ExposedPort;
- import com.greenwavereality.commons.test.docker.DockerContainerConfig;
- import com.greenwavereality.commons.test.docker.DockerContainerWrapper;
- import com.greenwavereality.riot.node.protocol.core.dto.DeviceUpdateEvent;
- import com.jayway.restassured.response.Response;
- import com.predixion.riot.server.api.domain.*;
- import org.apache.commons.io.FileUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringBootConfiguration;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.boot.test.context.TestConfiguration;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.test.context.ActiveProfiles;
- import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
- import org.testng.annotations.AfterClass;
- import org.testng.annotations.AfterSuite;
- import org.testng.annotations.BeforeClass;
- import org.testng.annotations.BeforeSuite;
- import java.io.File;
- import java.io.IOException;
- import java.util.UUID;
- import static com.greenwavereality.commons.lang.utils.StringFUtils.format;
- import static com.greenwavereality.commons.test.docker.DockerUtils.createLocalDockerClient;
- import static com.jayway.restassured.RestAssured.given;
- import static java.util.Collections.emptySet;
- @ActiveProfiles("test")
- @SpringBootTest(classes = RiotCloudFunctionalTest.RiotCloudConfiguration.class)
- public abstract class RiotCloudFunctionalTest extends AbstractTestNGSpringContextTests{
- @SpringBootConfiguration
- @EnableConfigurationProperties(TestProperties.class)
- public static class RiotCloudConfiguration { }
- private static final int RIOT_START_TIMEOUT = 60000;
- private static final String RIOT_CLOUD_CONTAINER_NAME = "cloud-ft-riot-cloud-container";
- private static final String keycloak_url = "http://172.24.0.5:8080/auth";
- @Autowired
- private TestProperties testProperties;
- private DockerContainerWrapper riotCloudContainer;
- protected UUID organizationUUID;
- protected TestNode node1;
- protected TestNode node2;
- protected Organization organization;
- protected RiotCloudClient client;
- protected UUID deviceGroupId;
- protected DeviceGroup deviceGroup;
- @BeforeSuite
- public void runContainer() throws Exception {
- springTestContextPrepareTestInstance();
- runCloudContainer();
- }
- @AfterSuite(alwaysRun = true)
- public void stopContainer() throws Exception {
- writeContainerLogs(riotCloudContainer, RIOT_CLOUD_CONTAINER_NAME);
- forceRemoveContainer(riotCloudContainer);
- }
- @BeforeClass
- public void prepareOrganization() throws Exception {
- client = createRiotClient();
- organization = new Organization(null, "organization", emptySet());
- organizationUUID = client.createOrganization(organization);
- organization.setId(organizationUUID);
- node1 = TestNode.create("tcp://localhost:1883", organizationUUID);
- node2 = TestNode.create("tcp://localhost:1883", organizationUUID);
- }
- @AfterClass(alwaysRun = true)
- public void removeOrganization() throws Exception {
- if (organizationUUID != null) {
- client.deleteOrganization(organizationUUID);
- }
- }
- protected RiotCloudClient createRiotClient() {
- Response response = given()
- .header("Content-Type", "application/x-www-form-urlencoded")
- .formParam("username", "admin")
- .formParam("password", "admin")
- .formParam("grant_type", "password")
- .formParam("client_id", "riot-cloud-client")
- .post(keycloak_url + "/realms/master/protocol/openid-connect/token");
- String accessToken = response.jsonPath().get("access_token").toString();
- return new RiotCloudClient("ws://localhost:8080/sockjs?access_token="+accessToken+"");
- }
- protected boolean checkDeviceEvent(DeviceUpdateEvent deviceUpdateEvent, DeviceUpdateEventType type, UUID deviceId) {
- return deviceUpdateEvent.getType() == type && deviceUpdateEvent.getMetadata().getId().equals(deviceId);
- }
- protected boolean checkNodeEvent(NodeMetadataUpdateEvent nodeUpdateEvent, NodeMetadataUpdateType type, UUID nodeId) {
- return nodeUpdateEvent.getType() == type && nodeUpdateEvent.getMetadata().getId().equals(nodeId);
- }
- private DockerContainerConfig riotCloudDockerConfig() {
- return DockerContainerConfig.builder()
- .exposedPort(new ExposedPort(8080))
- .image(format("{}/{}/{}:{}", testProperties.getDockerRegistry(),
- testProperties.getDockerRegistryProject(), testProperties.getDockerImage(),
- testProperties.getRiotVersion()))
- .networkMode("riotcloudenv_default")
- .name(RIOT_CLOUD_CONTAINER_NAME)
- .port(8080, 8080)
- .env("SPRING_DATASOURCE_URL", "jdbc:postgresql://riotcloudenv_postgres_1:5432/riot?currentSchema=public")
- .env("SPRING_DATA_CASSANDRA_CONTACT-POINTS", "riotcloudenv_cassandra_1")
- .env("KEYCLOAK_AUTH-SERVER-URL", keycloak_url)
- .env("RIOT_CLIENT_HOST-URI", "tcp://172.24.0.2:1883")
- .env("RIOT_CLOUD_NODE_INACTIVE_TIMEOUT_SECONDS", "2")
- .env("RIOT_CLOUD_NODE_INACTIVE_CHECK_INTERVAL_SECONDS", "2")
- .build();
- }
- private void runCloudContainer() {
- DockerClient dockerClient = createLocalDockerClient();
- riotCloudContainer = DockerContainerWrapper.wrap(dockerClient, riotCloudDockerConfig());
- riotCloudContainer.forceRun();
- riotCloudContainer.waitForContainerLog(RIOT_START_TIMEOUT, "Started RiotCloud");
- }
- private void writeContainerLogs(DockerContainerWrapper container, String containerName) throws IOException {
- if (container != null) {
- File outputFile = new File(System.getProperty("functional.logDir"), containerName + ".log");
- FileUtils.forceMkdirParent(outputFile);
- container.writeContainerLogs(outputFile);
- }
- }
- private void forceRemoveContainer(DockerContainerWrapper container) {
- container.forceRemove();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement