Skip to content

Commit fe9006d

Browse files
committed
Added movies microservice for Ch 8 EoC Lab
1 parent 2aa4b3a commit fe9006d

File tree

8 files changed

+371
-0
lines changed

8 files changed

+371
-0
lines changed

movies/Jenkinsfile

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
pipeline {
2+
options {
3+
// set a timeout of 60 minutes for this pipeline
4+
timeout(time: 60, unit: 'MINUTES')
5+
}
6+
agent {
7+
node {
8+
//TODO: Add label for the Maven jenkins agent
9+
}
10+
}
11+
12+
environment {
13+
//TODO: Customize these variables for your environment
14+
DEV_PROJECT = "youruser-movies-dev"
15+
STAGE_PROJECT = "youruser-movies-stage"
16+
APP_GIT_URL = "https://github.com/youruser/DO288-apps"
17+
NEXUS_SERVER = "http://nexus-common.apps.cluster.domain.example.com/repository/java"
18+
19+
// DO NOT CHANGE THE GLOBAL VARS BELOW THIS LINE
20+
APP_NAME = "movies"
21+
}
22+
23+
24+
stages {
25+
26+
stage('Compilation Check') {
27+
steps {
28+
echo '### Checking for compile errors ###'
29+
sh '''
30+
cd ${APP_NAME}
31+
mvn -s settings.xml -B clean compile
32+
'''
33+
}
34+
}
35+
36+
stage('Run Unit Tests') {
37+
steps {
38+
echo '### Running unit tests ###'
39+
sh '''
40+
cd ${APP_NAME}
41+
mvn -s settings.xml -B clean test
42+
'''
43+
}
44+
}
45+
46+
stage('Static Code Analysis') {
47+
steps {
48+
echo '### Running pmd on code ###'
49+
sh '''
50+
cd ${APP_NAME}
51+
mvn -s settings.xml -B clean pmd:check
52+
'''
53+
}
54+
}
55+
56+
stage('Create fat JAR') {
57+
steps {
58+
echo '### Creating fat JAR ###'
59+
sh '''
60+
cd ${APP_NAME}
61+
mvn -s settings.xml -B clean package -DskipTests=true
62+
'''
63+
}
64+
}
65+
66+
stage('Launch new app in DEV env') {
67+
steps {
68+
echo '### Cleaning existing resources in DEV env ###'
69+
sh '''
70+
oc delete all -l app=${APP_NAME} -n ${DEV_PROJECT}
71+
oc delete all -l build=${APP_NAME} -n ${DEV_PROJECT}
72+
sleep 5
73+
oc new-build java:8 --name=${APP_NAME} --binary=true -n ${DEV_PROJECT}
74+
'''
75+
76+
echo '### Creating a new app in DEV env ###'
77+
script {
78+
openshift.withCluster() {
79+
openshift.withProject(env.DEV_PROJECT) {
80+
openshift.selector("bc", "${APP_NAME}").startBuild("--from-file=${APP_NAME}/target/${APP_NAME}.jar", "--wait=true", "--follow=true")
81+
}
82+
}
83+
}
84+
// TODO: Create a new OpenShift application based on the ${APP_NAME}:latest image stream
85+
// TODO: Expose the ${APP_NAME} service for external access
86+
}
87+
}
88+
89+
stage('Wait for deployment in DEV env') {
90+
//TODO: Watch deployment until pod is in 'Running' state
91+
}
92+
93+
stage('Promote to Staging Env') {
94+
steps {
95+
timeout(time: 60, unit: 'MINUTES') {
96+
input message: "Promote to Staging?"
97+
}
98+
script {
99+
openshift.withCluster() {
100+
// TODO: Tag the ${APP_NAME}:latest image stream in the dev env as ${APP_NAME}:stage in staging
101+
}
102+
}
103+
}
104+
}
105+
106+
stage('Deploy to Staging Env') {
107+
steps {
108+
echo '### Cleaning existing resources in Staging ###'
109+
sh '''
110+
oc project ${STAGE_PROJECT}
111+
oc delete all -l app=${APP_NAME}
112+
sleep 5
113+
'''
114+
115+
echo '### Creating a new app in Staging ###'
116+
// TODO: Create a new app in staging
117+
}
118+
}
119+
120+
stage('Wait for deployment in Staging') {
121+
steps {
122+
sh "oc get route ${APP_NAME} -n ${STAGE_PROJECT} -o jsonpath='{ .spec.host }' --loglevel=4 > routehost"
123+
124+
script {
125+
routeHost = readFile('routehost').trim()
126+
127+
openshift.withCluster() {
128+
openshift.withProject( "${STAGE_PROJECT}" ) {
129+
def deployment = openshift.selector("dc", "${APP_NAME}").rollout()
130+
openshift.selector("dc", "${APP_NAME}").related('pods').untilEach(1) {
131+
return (it.object().status.phase == "Running")
132+
}
133+
}
134+
echo "Deployment to Staging env is complete. Access the API endpoint at the URL http://${routeHost}/movies."
135+
}
136+
}
137+
}
138+
}
139+
}
140+
}

movies/pom.xml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.springframework.boot</groupId>
6+
<artifactId>spring-boot-starter-parent</artifactId>
7+
<version>2.1.6.RELEASE</version>
8+
<relativePath /> <!-- lookup parent from repository -->
9+
</parent>
10+
<groupId>com.redhat</groupId>
11+
<artifactId>movies</artifactId>
12+
<version>1.0.0</version>
13+
<name>movies</name>
14+
<description>Demo project for Spring Boot</description>
15+
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-web</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-test</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<finalName>movies</finalName>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-maven-plugin</artifactId>
39+
</plugin>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-pmd-plugin</artifactId>
43+
<version>3.12.0</version>
44+
<configuration>
45+
<rulesets>
46+
<ruleset>/category/java/bestpractices.xml</ruleset>
47+
</rulesets>
48+
<failOnViolation>true</failOnViolation>
49+
<printFailingErrors>true</printFailingErrors>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
</project>

movies/settings.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<settings>
3+
<mirrors>
4+
<mirror>
5+
<id>internal-repository</id>
6+
<name>Maven Repository Manager for classroom</name>
7+
<!-- // TODO: Change the url attribute to the nexus proxy server URL for your environment. -->
8+
<url>http://nexus-common.apps.cluster.domain.example.com/repository/java</url>
9+
<mirrorOf>*</mirrorOf>
10+
</mirror>
11+
</mirrors>
12+
</settings>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.redhat.movies;
2+
3+
import java.io.Serializable;
4+
5+
public class Movie implements Serializable {
6+
7+
private static final long serialVersionUID = -3240337073623122124L;
8+
9+
private Integer movieId;
10+
private String name;
11+
private String genre;
12+
13+
public Movie(Integer movieId, String name, String genre) {
14+
this.movieId = movieId;
15+
this.name = name;
16+
this.genre = genre;
17+
}
18+
19+
public Integer getMovieId() {
20+
return movieId;
21+
}
22+
23+
public void setMovieId(Integer movieId) {
24+
this.movieId = movieId;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getGenre() {
36+
return genre;
37+
}
38+
39+
public void setGenre(String genre) {
40+
this.genre = genre;
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.redhat.movies;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class MoviesApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(MoviesApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.redhat.movies;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.io.File;
6+
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/")
13+
public class MoviesController {
14+
15+
private List<Movie> movies;
16+
private String status = "OK";
17+
private String flag = "READY";
18+
19+
@GetMapping("/movies")
20+
public List<Movie> getAllMovies() {
21+
22+
//Generate fake static data
23+
movies = new ArrayList<Movie>();
24+
movies.add(new Movie(1,"The Godfather","Crime/Thriller"));
25+
movies.add(new Movie(2,"Star Wars","Sci-Fi"));
26+
movies.add(new Movie(3,"The Mask","Comedy"));
27+
movies.add(new Movie(4,"Die Hard","Action"));
28+
movies.add(new Movie(5,"The Exorcist","Horror"));
29+
movies.add(new Movie(6,"The Silence of the Lambs","Drama"));
30+
31+
return movies;
32+
}
33+
34+
@GetMapping("/status")
35+
public String getStatus() {
36+
return status;
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.jackson.serialization.indent_output=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.redhat.movies;
2+
3+
import java.util.List;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.web.client.TestRestTemplate;
11+
import org.springframework.boot.web.server.LocalServerPort;
12+
import org.springframework.core.ParameterizedTypeReference;
13+
import org.springframework.http.HttpEntity;
14+
import org.springframework.http.HttpHeaders;
15+
import org.springframework.http.HttpMethod;
16+
import org.springframework.http.ResponseEntity;
17+
import org.springframework.test.context.junit4.SpringRunner;
18+
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest(classes = MoviesApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
21+
public class MoviesApplicationTests {
22+
23+
@Autowired
24+
private TestRestTemplate restTemplate;
25+
26+
@LocalServerPort
27+
private int port;
28+
29+
@Test
30+
public void contextLoads() {
31+
}
32+
33+
@Test
34+
public void testNotNullResponse() {
35+
HttpHeaders headers = new HttpHeaders();
36+
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
37+
38+
ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/movies", HttpMethod.GET,
39+
entity, String.class);
40+
41+
Assert.assertNotNull(response.getBody());
42+
}
43+
44+
@Test
45+
public void testGetAllMovies() {
46+
47+
ResponseEntity <List<Movie>> response = restTemplate.exchange("http://localhost:" + port + "/movies",
48+
HttpMethod.GET, null, new ParameterizedTypeReference <List<Movie>> () {});
49+
50+
List <Movie> movies = response.getBody();
51+
Assert.assertNotNull(movies);
52+
Assert.assertEquals(7, movies.size());
53+
Assert.assertEquals("The Godfather", movies.get(0).getName());
54+
}
55+
56+
@Test
57+
public void testGetStatus() {
58+
HttpHeaders headers = new HttpHeaders();
59+
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
60+
61+
ResponseEntity<String> response = restTemplate.exchange("http://localhost:" + port + "/status", HttpMethod.GET,
62+
entity, String.class);
63+
64+
Assert.assertNotNull(response.getBody());
65+
Assert.assertEquals("Ready", response.getBody());
66+
}
67+
68+
}

0 commit comments

Comments
 (0)