Skip to content

Commit 7e6ed40

Browse files
authored
Add star wars working example (#3)
* Add star wars working example (without tests) * Add planetConverter test * Add service test
1 parent e7613a7 commit 7e6ed40

File tree

23 files changed

+716
-7
lines changed

23 files changed

+716
-7
lines changed

.idea/misc.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

snapshot-matcher-example/pom.xml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
<artifactId>snapshot-matcher-example</artifactId>
99
<version>1.0-SNAPSHOT</version>
1010

11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.5.9.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
1118
<properties>
1219
<jdk.version>1.8</jdk.version>
1320
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
@@ -24,14 +31,22 @@
2431
<target>${jdk.version}</target>
2532
</configuration>
2633
</plugin>
34+
<plugin>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-maven-plugin</artifactId>
37+
</plugin>
2738
</plugins>
2839
</build>
2940

3041
<dependencies>
3142
<dependency>
32-
<groupId>junit</groupId>
33-
<artifactId>junit</artifactId>
34-
<version>4.12</version>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-web</artifactId>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-test</artifactId>
3550
<scope>test</scope>
3651
</dependency>
3752

@@ -43,5 +58,4 @@
4358

4459
</dependencies>
4560

46-
4761
</project>

snapshot-matcher-example/snapshot-matcher-example.iml

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,55 @@
1212
</content>
1313
<orderEntry type="inheritedJdk" />
1414
<orderEntry type="sourceFolder" forTests="false" />
15+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:1.5.9.RELEASE" level="project" />
16+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:1.5.9.RELEASE" level="project" />
17+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.5.9.RELEASE" level="project" />
18+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:1.5.9.RELEASE" level="project" />
19+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:1.5.9.RELEASE" level="project" />
20+
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.1.11" level="project" />
21+
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.1.11" level="project" />
22+
<orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.25" level="project" />
23+
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.25" level="project" />
24+
<orderEntry type="library" name="Maven: org.slf4j:log4j-over-slf4j:1.7.25" level="project" />
25+
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.17" level="project" />
26+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:1.5.9.RELEASE" level="project" />
27+
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.5.23" level="project" />
28+
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-annotations-api:8.5.23" level="project" />
29+
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:8.5.23" level="project" />
30+
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:8.5.23" level="project" />
31+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-validator:5.3.6.Final" level="project" />
32+
<orderEntry type="library" name="Maven: javax.validation:validation-api:1.1.0.Final" level="project" />
33+
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.1.Final" level="project" />
34+
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.4" level="project" />
35+
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.10" level="project" />
36+
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
37+
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.3.13.RELEASE" level="project" />
38+
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.13.RELEASE" level="project" />
39+
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.13.RELEASE" level="project" />
40+
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.13.RELEASE" level="project" />
41+
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.3.13.RELEASE" level="project" />
42+
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.13.RELEASE" level="project" />
43+
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:1.5.9.RELEASE" level="project" />
44+
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:1.5.9.RELEASE" level="project" />
45+
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:1.5.9.RELEASE" level="project" />
46+
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.2.0" level="project" />
47+
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.2.1" level="project" />
48+
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.1" level="project" />
49+
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.3" level="project" />
50+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
1551
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
52+
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:2.6.0" level="project" />
53+
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.10.19" level="project" />
54+
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" />
1655
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
56+
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" />
57+
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.4.0" level="project" />
58+
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
59+
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.13.RELEASE" level="project" />
60+
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:4.3.13.RELEASE" level="project" />
1761
<orderEntry type="module" module-name="snapshot-matcher" />
1862
<orderEntry type="library" name="Maven: com.googlecode.java-diff-utils:diffutils:1.3.0" level="project" />
1963
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-all:1.3" level="project" />
20-
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.4.6" level="project" />
21-
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.4.6" level="project" />
22-
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.4.0" level="project" />
64+
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.10" level="project" />
2365
</component>
2466
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.zenika.snapshotmatcherexample;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SnapshotMatcherExampleApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SnapshotMatcherExampleApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.zenika.snapshotmatcherexample.configuration;
2+
3+
import static java.util.Collections.singletonList;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
@Configuration
11+
public class RestTemplateConfiguration {
12+
13+
@Autowired
14+
private UserAgentRequestInterceptor requestInterceptor;
15+
16+
@Bean
17+
public RestTemplate restTemplate(){
18+
RestTemplate restTemplate = new RestTemplate();
19+
20+
restTemplate.setInterceptors(singletonList(requestInterceptor));
21+
22+
return restTemplate;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.zenika.snapshotmatcherexample.configuration;
2+
3+
import java.io.IOException;
4+
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.HttpRequest;
8+
import org.springframework.http.client.ClientHttpRequestExecution;
9+
import org.springframework.http.client.ClientHttpRequestInterceptor;
10+
import org.springframework.http.client.ClientHttpResponse;
11+
import org.springframework.stereotype.Component;
12+
13+
/**
14+
* Request interceptor which add a User-Agent header to each request.
15+
*/
16+
@Component
17+
public class UserAgentRequestInterceptor implements ClientHttpRequestInterceptor {
18+
19+
@Value("${spring.application.name}")
20+
private String applicationName;
21+
22+
@Override
23+
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
24+
httpRequest.getHeaders().set(HttpHeaders.USER_AGENT, applicationName);
25+
26+
return clientHttpRequestExecution.execute(httpRequest, bytes);
27+
}
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.zenika.snapshotmatcherexample.starwars;
2+
3+
import java.util.stream.Collectors;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.web.client.RestTemplate;
8+
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.zenika.snapshotmatcherexample.starwars.dtos.FilmDTO;
11+
import com.zenika.snapshotmatcherexample.starwars.dtos.PeopleDTO;
12+
import com.zenika.snapshotmatcherexample.starwars.dtos.PlanetDTO;
13+
import com.zenika.snapshotmatcherexample.starwars.entities.Planet;
14+
15+
@Component
16+
public class PlanetConverter {
17+
18+
@Autowired
19+
private ObjectMapper objectMapper;
20+
21+
@Autowired
22+
private RestTemplate restTemplate;
23+
24+
public PlanetDTO convertPlanet(Planet planet) {
25+
PlanetDTO dto = objectMapper.convertValue(planet, PlanetDTO.class);
26+
27+
dto.films = planet.filmsUrls.stream()
28+
.map(filmUrl -> restTemplate.getForObject(filmUrl, FilmDTO.class))
29+
.collect(Collectors.toList());
30+
31+
dto.residents = planet.residentsUrls.stream()
32+
.map(filmUrl -> restTemplate.getForObject(filmUrl, PeopleDTO.class))
33+
.collect(Collectors.toList());
34+
35+
return dto;
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.zenika.snapshotmatcherexample.starwars;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import com.zenika.snapshotmatcherexample.starwars.dtos.PlanetDTO;
10+
11+
@RestController
12+
@RequestMapping("planets")
13+
public class StarWarsRest {
14+
15+
@Autowired
16+
private StarWarsService starWarsService;
17+
18+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
19+
public PlanetDTO getPlanet(@PathVariable Long id){
20+
return starWarsService.getPlanet(id);
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.zenika.snapshotmatcherexample.starwars;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
import com.zenika.snapshotmatcherexample.starwars.dtos.PlanetDTO;
9+
import com.zenika.snapshotmatcherexample.starwars.entities.Planet;
10+
11+
@Service
12+
public class StarWarsService {
13+
14+
@Autowired
15+
private RestTemplate restTemplate;
16+
17+
@Autowired
18+
private PlanetConverter planetConverter;
19+
20+
@Value("${endpoints.planets}")
21+
private String endpoint;
22+
23+
public PlanetDTO getPlanet(Long id){
24+
return planetConverter.convertPlanet(restTemplate.getForObject(endpoint + id, Planet.class));
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.zenika.snapshotmatcherexample.starwars.dtos;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
6+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
public class FilmDTO implements Serializable {
11+
public String title;
12+
13+
@JsonProperty("episode_id")
14+
public int episodeId;
15+
16+
@JsonProperty("opening_crawl")
17+
public String openingCrawl;
18+
19+
public String director;
20+
public String producer;
21+
public String url;
22+
public String created;
23+
public String edited;
24+
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.zenika.snapshotmatcherexample.starwars.dtos;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
6+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
public class PeopleDTO implements Serializable {
11+
public String name;
12+
13+
@JsonProperty("birth_year")
14+
public String birthYear;
15+
16+
public String gender;
17+
18+
@JsonProperty("hair_color")
19+
public String hairColor;
20+
21+
public String height;
22+
23+
public String mass;
24+
25+
@JsonProperty("skin_color")
26+
public String skinColor;
27+
28+
public String created;
29+
public String edited;
30+
public String url;
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.zenika.snapshotmatcherexample.starwars.dtos;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
8+
public class PlanetDTO implements Serializable {
9+
public String name;
10+
public String diameter;
11+
public String gravity;
12+
public String population;
13+
public String climate;
14+
public String terrain;
15+
public String created;
16+
public String edited;
17+
public String url;
18+
19+
@JsonProperty("rotation_period")
20+
public String rotationPeriod;
21+
22+
@JsonProperty("orbital_period")
23+
public String orbitalPeriod;
24+
25+
@JsonProperty("surface_water")
26+
public String surfaceWater;
27+
28+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
29+
public List<PeopleDTO> residents;
30+
31+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
32+
public List<FilmDTO> films;
33+
}

0 commit comments

Comments
 (0)