Skip to content

Commit 427038d

Browse files
authored
Finished basic integration tests with trip-cmd and trip-query (#3)
* Updated dependencies for running tests/created test * Added system tests/updated docker-compose files * Changed docker-compose, set up testing framework * Refined docker-compose * Added docker-compose into resources * Updated Integration Tests * Fixed GET test * Renamed tests and fixed authentication integration * Cleaned up integration tests * Deleted bad request test * Wrong credentials being encoded fixed * Deleted logging in gradle * Updated checkstyle fixes Updated travis with docker service * Added script to stop mysql if already running * Updated docker composes to pull user-service image * Updated gradle with palantir docker-compose latest version * Merging conflicts * Fixed gradle, ready for merging
1 parent c94c584 commit 427038d

File tree

7 files changed

+338
-46
lines changed

7 files changed

+338
-46
lines changed

.travis.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
language: java
2-
32
jdk:
43
- oraclejdk8
54

6-
encoding: utf8
5+
sudo: required
6+
7+
services:
8+
- docker
9+
10+
before_script:
11+
- sudo service mysql stop

build.gradle

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
11
buildscript {
2-
ext {
3-
springBootVersion = '1.5.9.RELEASE'
4-
}
5-
repositories {
6-
mavenCentral()
7-
}
8-
dependencies {
9-
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10-
}
2+
ext {
3+
springBootVersion = '1.5.9.RELEASE'
4+
}
5+
repositories {
6+
mavenCentral()
7+
}
8+
dependencies {
9+
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10+
classpath("com.jayway.restassured:rest-assured:2.5.0")
11+
classpath("com.jayway.restassured:spring-mock-mvc:2.5.0")
12+
}
1113
}
1214

1315
apply plugin: 'java'
1416
apply plugin: 'eclipse-wtp'
1517
apply plugin: 'org.springframework.boot'
1618
apply plugin: 'checkstyle'
1719

20+
checkstyle {
21+
configFile = new File(rootDir, "checkstyle.xml")
22+
toolVersion = "8.7"
23+
}
24+
1825
group = 'aist.edge'
1926
version = '0.0.1'
2027
sourceCompatibility = 1.8
2128

2229
repositories {
23-
mavenCentral()
30+
mavenCentral()
31+
maven {
32+
url 'https://dl.bintray.com/palantir/releases'
33+
}
2434
}
2535

2636
configurations {
27-
providedRuntime
37+
providedRuntime
2838
}
2939

3040
ext {
31-
springCloudVersion = 'Edgware.SR1'
41+
springCloudVersion = 'Edgware.SR1'
3242
}
3343

3444
dependencies {
35-
compile('org.springframework.cloud:spring-cloud-starter-eureka')
36-
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
37-
compile('org.springframework.cloud:spring-cloud-starter-oauth2')
38-
compile('org.springframework.cloud:spring-cloud-starter-zuul')
39-
compile('org.springframework.boot:spring-boot-starter-web')
40-
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
41-
testCompile('org.springframework.boot:spring-boot-starter-test')
45+
compile('org.springframework.security:spring-security-test')
46+
compile('org.springframework.cloud:spring-cloud-starter-eureka')
47+
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
48+
compile('org.springframework.cloud:spring-cloud-starter-oauth2')
49+
compile('org.springframework.cloud:spring-cloud-starter-zuul')
50+
compile('org.springframework.boot:spring-boot-starter-web')
51+
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
52+
testCompile ('org.springframework.cloud:spring-cloud-starter-eureka-server')
53+
testCompile('com.jayway.restassured:rest-assured:2.5.0')
54+
testCompile ('com.jayway.restassured:spring-mock-mvc:2.5.0')
55+
testCompile ('org.springframework.cloud:spring-cloud-starter-contract-stub-runner')
56+
testCompile ('com.palantir.docker.compose:docker-compose-rule-junit4:0.33.0')
4257
}
4358

4459
dependencyManagement {
45-
imports {
46-
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
47-
}
60+
imports {
61+
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
62+
}
4863
}
4964

5065
checkstyle {
51-
toolVersion = "8.7"
52-
configFile = new File(rootDir, "checkstyle.xml")
53-
}
66+
toolVersion = "8.7"
67+
configFile = new File(rootDir, "checkstyle.xml")
68+
}

docker-compose.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,101 @@
11
version: '3'
2+
23
services:
4+
35
edge-service:
46
build: ./
57
ports:
68
- "8080:8080"
79
depends_on:
810
- discovery-service
11+
912
discovery-service:
1013
image: springcloud/eureka
1114
ports:
1215
- "8761:8761"
16+
container_name: eureka-server
17+
18+
# hystrix-dashboard:
19+
# image: kbastani/hystrix-dashboard
20+
# container_name: hystrix-dashboard
21+
# environment:
22+
# - SPRING_PROFILES_ACTIVE=docker
23+
# ports:
24+
# - 6161:6161
25+
26+
# config-service:
27+
# build: config-service
28+
# container_name: config-service
29+
# environment:
30+
# - SPRING_PROFILES_ACTIVE=docker
31+
# ports:
32+
# - 8888:8888
33+
34+
mysql-server:
35+
image: mysql:5.7
36+
container_name: mysql-server
37+
volumes:
38+
- mysql-data:/var/lib/mysql:rw
39+
restart: always
40+
ports:
41+
- '3306:3306'
42+
environment:
43+
MYSQL_USER:
44+
MYSQL_PASSWORD:
45+
MYSQL_ROOT_PASSWORD: 'root'
46+
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
47+
MYSQL_DATABASE: 'user_service'
48+
49+
microservice--user-service:
50+
image: aista/user-service
51+
depends_on:
52+
- discovery-service
53+
- mysql-server
54+
container_name: microservice--user-service
55+
ports:
56+
- '8091:8091'
57+
58+
mongo:
59+
image: 'mongo:3.4.1'
60+
container_name: 'mongo'
61+
ports:
62+
- '27017:27017'
63+
volumes:
64+
- 'mongo:/data/db'
65+
66+
rabbitmq:
67+
image: rabbitmq:management
68+
container_name: 'rabbitmq'
69+
ports:
70+
- "5672:5672"
71+
- "15672:15672"
72+
73+
trip-management-cmd:
74+
image: aista/trip-management-query
75+
container_name: trip-management-cmd
76+
environment:
77+
- RABBIT_HOST=rabbitmq
78+
- MONGO_HOST=mongo
79+
ports:
80+
- '8092:8092'
81+
depends_on:
82+
- discovery-service
83+
- rabbitmq
84+
- mongo
85+
86+
trip-management-query:
87+
image: aista/trip-management-query
88+
container_name: trip-management-query
89+
environment:
90+
- RABBIT_HOST=rabbitmq
91+
- MONGO_HOST=mongo
92+
ports:
93+
- '8093:8093'
94+
depends_on:
95+
- rabbitmq
96+
- mongo
97+
- discovery-service
98+
99+
volumes:
100+
mongo:
101+
mysql-data:

src/main/resources/application.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ zuul:
1414
security:
1515
oauth2:
1616
resource:
17-
userInfoUri: http://192.168.99.100:8181/uaa/user
17+
userInfoUri: http://localhost:8181/uaa/user
1818
ignored: /catalog/**
1919
eureka:
2020
instance:
@@ -23,7 +23,8 @@ eureka:
2323
registerWithEureka: true
2424
fetchRegistry: true
2525
serviceUrl:
26-
defaultZone: http://192.168.99.100:8761/eureka/
26+
defaultZone: http://localhost:8761/eureka/
27+
2728
---
2829
spring:
2930
profiles: docker
@@ -37,7 +38,7 @@ zuul:
3738
security:
3839
oauth2:
3940
resource:
40-
userInfoUri: http://${DOCKER_IP:192.168.99.100}:8181/uaa/user
41+
userInfoUri: http://localhost:8181/uaa/user
4142
ignored: /catalog/**
4243
eureka:
4344
instance:

src/test/java/aist/edge/edgeservice/EdgeServiceApplicationTests.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package aist.edge.edgeservice;
2+
3+
import static org.assertj.core.api.Java6Assertions.assertThat;
4+
5+
import com.palantir.docker.compose.DockerComposeRule;
6+
import com.palantir.docker.compose.connection.waiting.HealthChecks;
7+
8+
import java.util.*;
9+
10+
import org.json.JSONException;
11+
import org.json.JSONObject;
12+
import org.junit.Before;
13+
import org.junit.ClassRule;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
17+
import org.springframework.boot.test.context.SpringBootTest;
18+
import org.springframework.boot.test.web.client.TestRestTemplate;
19+
import org.springframework.http.*;
20+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
21+
import org.springframework.util.*;
22+
23+
@RunWith(SpringJUnit4ClassRunner.class)
24+
@SpringBootTest(classes = EdgeServiceApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
25+
public class EdgeServiceIntegrationTests {
26+
27+
@ClassRule
28+
public static DockerComposeRule docker = DockerComposeRule.builder().pullOnStartup(true)
29+
.file("src/test/resources/docker-compose.yml")
30+
.waitingForService("microservice--user-service", HealthChecks.toHaveAllPortsOpen())
31+
.waitingForService("microservice--user-service", HealthChecks.toRespondOverHttp(8091,
32+
(port) -> port.inFormat("http://localhost:8091")))
33+
.waitingForService("trip-management-cmd", HealthChecks.toHaveAllPortsOpen())
34+
.waitingForService("trip-management-cmd", HealthChecks.toRespondOverHttp(8080,
35+
(port) -> port.inFormat("http://localhost:8092")))
36+
.waitingForService("trip-management-query", HealthChecks.toHaveAllPortsOpen())
37+
.waitingForService("trip-management-query", HealthChecks.toRespondOverHttp(8080,
38+
(port) -> port.inFormat("http://localhost:8093")))
39+
.waitingForService("discovery-service", HealthChecks.toHaveAllPortsOpen())
40+
.waitingForService("discovery-service", HealthChecks.toRespondOverHttp(8761,
41+
(port) -> port.inFormat("http://localhost:8761")))
42+
.build();
43+
44+
private TestRestTemplate restTemplate = new TestRestTemplate();
45+
46+
private String token;
47+
48+
@Before
49+
public void setUp() throws JSONException {
50+
String plainCreds = "eagleeye:thisissecret";
51+
byte[] plainCredsBytes = plainCreds.getBytes();
52+
byte[] base64CredsBytes = Base64.getEncoder().encode(plainCredsBytes);
53+
String base64Creds = new String(base64CredsBytes);
54+
55+
HttpHeaders headers = new HttpHeaders();
56+
headers.add("Authorization", "Basic " + base64Creds);
57+
headers.add("Content-Type", "application/x-www-form-urlencoded");
58+
59+
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
60+
parameters.add("username", "eagleeye");
61+
parameters.add("secret", "thisissecret");
62+
String body = "grant_type=password&scope=webclient&username=user1&password=password";
63+
HttpEntity<String> request = new HttpEntity<>(body, headers);
64+
65+
//when:
66+
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8091/auth/oauth/token", request, String.class, parameters);
67+
68+
//then:
69+
assertThat(response.getStatusCodeValue()).isEqualTo(200);
70+
JSONObject json = new JSONObject(response.getBody());
71+
token = json.getString("access_token");
72+
}
73+
74+
@Test
75+
public void tripCommandPOSTRequestSuccess() {
76+
//given:
77+
HttpHeaders headers = new HttpHeaders();
78+
headers.add("Authorization", "Bearer " + token);
79+
headers.add("Content-Type", "application/json");
80+
81+
String body = "{ \"originAddress\": \"Somewhere of the origin\", \"destinationAddress\": "
82+
+ "\"Somewhere destination\", \"userId\": \"123e4567-e89b-12d3-a456-426655440000\" }";
83+
HttpEntity<String> request = new HttpEntity<>(body, headers);
84+
85+
//when:
86+
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8092/api/trip", request, String.class);
87+
88+
//then:
89+
assertThat(response.getStatusCodeValue()).isEqualTo(200);
90+
}
91+
92+
@Test
93+
public void tripQueryGETRequestSuccess() {
94+
//given:
95+
HttpHeaders headers = new HttpHeaders();
96+
headers.add("Authorization", "Bearer " + token);
97+
98+
//when:
99+
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8093/api/trips", String.class);
100+
101+
//then:
102+
assertThat(response.getStatusCodeValue()).isEqualTo(200);
103+
}
104+
}

0 commit comments

Comments
 (0)