Advertisement
Guest User

Untitled

a guest
May 19th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. package com.greenwavereality.riot.cloud;
  2.  
  3. import com.github.dockerjava.api.DockerClient;
  4. import com.github.dockerjava.api.model.ExposedPort;
  5. import com.greenwavereality.commons.test.docker.DockerContainerConfig;
  6. import com.greenwavereality.commons.test.docker.DockerContainerWrapper;
  7. import com.greenwavereality.riot.node.protocol.core.dto.DeviceUpdateEvent;
  8. import com.jayway.restassured.response.Response;
  9. import com.predixion.riot.server.api.domain.*;
  10. import org.apache.commons.io.FileUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.SpringBootConfiguration;
  13. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  14. import org.springframework.boot.test.context.SpringBootTest;
  15. import org.springframework.boot.test.context.TestConfiguration;
  16. import org.springframework.context.annotation.Configuration;
  17. import org.springframework.test.context.ActiveProfiles;
  18. import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
  19. import org.testng.annotations.AfterClass;
  20. import org.testng.annotations.AfterSuite;
  21. import org.testng.annotations.BeforeClass;
  22. import org.testng.annotations.BeforeSuite;
  23.  
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.UUID;
  27.  
  28. import static com.greenwavereality.commons.lang.utils.StringFUtils.format;
  29. import static com.greenwavereality.commons.test.docker.DockerUtils.createLocalDockerClient;
  30. import static com.jayway.restassured.RestAssured.given;
  31. import static java.util.Collections.emptySet;
  32.  
  33. @ActiveProfiles("test")
  34. @SpringBootTest(classes = RiotCloudFunctionalTest.RiotCloudConfiguration.class)
  35. public abstract class RiotCloudFunctionalTest extends AbstractTestNGSpringContextTests{
  36.  
  37. @SpringBootConfiguration
  38. @EnableConfigurationProperties(TestProperties.class)
  39. public static class RiotCloudConfiguration { }
  40.  
  41. private static final int RIOT_START_TIMEOUT = 60000;
  42. private static final String RIOT_CLOUD_CONTAINER_NAME = "cloud-ft-riot-cloud-container";
  43. private static final String keycloak_url = "http://172.24.0.5:8080/auth";
  44.  
  45. @Autowired
  46. private TestProperties testProperties;
  47.  
  48. private DockerContainerWrapper riotCloudContainer;
  49. protected UUID organizationUUID;
  50. protected TestNode node1;
  51. protected TestNode node2;
  52. protected Organization organization;
  53. protected RiotCloudClient client;
  54. protected UUID deviceGroupId;
  55. protected DeviceGroup deviceGroup;
  56.  
  57. @BeforeSuite
  58. public void runContainer() throws Exception {
  59. springTestContextPrepareTestInstance();
  60. runCloudContainer();
  61. }
  62.  
  63. @AfterSuite(alwaysRun = true)
  64. public void stopContainer() throws Exception {
  65.  
  66. writeContainerLogs(riotCloudContainer, RIOT_CLOUD_CONTAINER_NAME);
  67.  
  68. forceRemoveContainer(riotCloudContainer);
  69. }
  70.  
  71. @BeforeClass
  72. public void prepareOrganization() throws Exception {
  73. client = createRiotClient();
  74.  
  75. organization = new Organization(null, "organization", emptySet());
  76. organizationUUID = client.createOrganization(organization);
  77. organization.setId(organizationUUID);
  78.  
  79. node1 = TestNode.create("tcp://localhost:1883", organizationUUID);
  80. node2 = TestNode.create("tcp://localhost:1883", organizationUUID);
  81. }
  82.  
  83. @AfterClass(alwaysRun = true)
  84. public void removeOrganization() throws Exception {
  85. if (organizationUUID != null) {
  86. client.deleteOrganization(organizationUUID);
  87. }
  88. }
  89.  
  90. protected RiotCloudClient createRiotClient() {
  91. Response response = given()
  92. .header("Content-Type", "application/x-www-form-urlencoded")
  93. .formParam("username", "admin")
  94. .formParam("password", "admin")
  95. .formParam("grant_type", "password")
  96. .formParam("client_id", "riot-cloud-client")
  97. .post(keycloak_url + "/realms/master/protocol/openid-connect/token");
  98. String accessToken = response.jsonPath().get("access_token").toString();
  99. return new RiotCloudClient("ws://localhost:8080/sockjs?access_token="+accessToken+"");
  100. }
  101.  
  102. protected boolean checkDeviceEvent(DeviceUpdateEvent deviceUpdateEvent, DeviceUpdateEventType type, UUID deviceId) {
  103. return deviceUpdateEvent.getType() == type && deviceUpdateEvent.getMetadata().getId().equals(deviceId);
  104. }
  105.  
  106. protected boolean checkNodeEvent(NodeMetadataUpdateEvent nodeUpdateEvent, NodeMetadataUpdateType type, UUID nodeId) {
  107. return nodeUpdateEvent.getType() == type && nodeUpdateEvent.getMetadata().getId().equals(nodeId);
  108. }
  109.  
  110. private DockerContainerConfig riotCloudDockerConfig() {
  111. return DockerContainerConfig.builder()
  112. .exposedPort(new ExposedPort(8080))
  113. .image(format("{}/{}/{}:{}", testProperties.getDockerRegistry(),
  114. testProperties.getDockerRegistryProject(), testProperties.getDockerImage(),
  115. testProperties.getRiotVersion()))
  116. .networkMode("riotcloudenv_default")
  117. .name(RIOT_CLOUD_CONTAINER_NAME)
  118. .port(8080, 8080)
  119. .env("SPRING_DATASOURCE_URL", "jdbc:postgresql://riotcloudenv_postgres_1:5432/riot?currentSchema=public")
  120. .env("SPRING_DATA_CASSANDRA_CONTACT-POINTS", "riotcloudenv_cassandra_1")
  121. .env("KEYCLOAK_AUTH-SERVER-URL", keycloak_url)
  122. .env("RIOT_CLIENT_HOST-URI", "tcp://172.24.0.2:1883")
  123. .env("RIOT_CLOUD_NODE_INACTIVE_TIMEOUT_SECONDS", "2")
  124. .env("RIOT_CLOUD_NODE_INACTIVE_CHECK_INTERVAL_SECONDS", "2")
  125. .build();
  126. }
  127.  
  128. private void runCloudContainer() {
  129. DockerClient dockerClient = createLocalDockerClient();
  130. riotCloudContainer = DockerContainerWrapper.wrap(dockerClient, riotCloudDockerConfig());
  131. riotCloudContainer.forceRun();
  132. riotCloudContainer.waitForContainerLog(RIOT_START_TIMEOUT, "Started RiotCloud");
  133. }
  134.  
  135. private void writeContainerLogs(DockerContainerWrapper container, String containerName) throws IOException {
  136. if (container != null) {
  137. File outputFile = new File(System.getProperty("functional.logDir"), containerName + ".log");
  138. FileUtils.forceMkdirParent(outputFile);
  139. container.writeContainerLogs(outputFile);
  140. }
  141. }
  142.  
  143. private void forceRemoveContainer(DockerContainerWrapper container) {
  144. container.forceRemove();
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement