Skip to content

Commit

Permalink
Formatage tp-2 et correction des diffs non significatifs entre énoncé…
Browse files Browse the repository at this point in the history
… et solution
  • Loading branch information
Pierre-Yves Fourmond committed Oct 26, 2023
1 parent eda1996 commit c147b7d
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 131 deletions.
13 changes: 0 additions & 13 deletions src/main/java/com/octo/ajava/AjavaApplication.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
package com.octo.ajava;

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AjavaApplication {

public static final DateTimeFormatter FORMATEUR_DATE =
(new DateTimeFormatterBuilder())
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('/')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('/')
.appendValue(ChronoField.YEAR_OF_ERA, 4, 10, SignStyle.EXCEEDS_PAD)
.toFormatter();

public static void main(String[] args) {
SpringApplication.run(AjavaApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.octo.ajava.domain.Film;
import com.octo.ajava.domain.repositories.FilmRepository;
import org.springframework.stereotype.Component;

import java.util.List;
import org.springframework.stereotype.Component;

@Component
public class RecupererLesFilmsUseCase {
Expand All @@ -18,4 +17,4 @@ public class RecupererLesFilmsUseCase {
public List<Film> executer() {
return filmRepository.recupererLesFilms();
}
}
}
55 changes: 21 additions & 34 deletions src/main/java/com/octo/ajava/infra/api_client/TMDBHttpClient.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
package com.octo.ajava.infra.api_client;

import static java.util.Collections.emptyList;

import com.octo.ajava.infra.api_client.entities.PaginatedTMDBMovies;
import com.octo.ajava.infra.api_client.entities.TMDBMovie;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.DefaultUriBuilderFactory;

@Service
@ConditionalOnProperty(name = "film.source", havingValue = "TMDB")
public class TMDBHttpClient {

private static final String BEARER = "Bearer ";

private WebClient webClient;

public TMDBHttpClient(
@Value("${tmdb.baseUrl}") String urlTmdb,
@Value("${tmdb.token}") String jetonTmdb
) {
webClient = WebClient.builder()
.baseUrl(urlTmdb)
.defaultHeader(HttpHeaders.AUTHORIZATION, BEARER + jetonTmdb)
.build();
}


public PaginatedTMDBMovies recupererLesFilmsPopulaires() {
return webClient.get()
.uri("/movie/popular")
.retrieve()
.bodyToMono(PaginatedTMDBMovies.class)
.block();
}
private static final String BEARER = "Bearer ";

private WebClient webClient;

public TMDBHttpClient(
@Value("${tmdb.baseUrl}") String urlTmdb, @Value("${tmdb.token}") String jetonTmdb) {
webClient =
WebClient.builder()
.baseUrl(urlTmdb)
.defaultHeader(HttpHeaders.AUTHORIZATION, BEARER + jetonTmdb)
.build();
}

public PaginatedTMDBMovies recupererLesFilmsPopulaires() {
return webClient
.get()
.uri("/movie/popular")
.retrieve()
.bodyToMono(PaginatedTMDBMovies.class)
.block();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.octo.ajava.infra.api_client.TMDBHttpClient;
import com.octo.ajava.infra.mapper.TMDBFilmMapper;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
Expand Down
34 changes: 16 additions & 18 deletions src/test/java/com/octo/ajava/ObjectMapperBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;

public class ObjectMapperBuilder {

public static final DateTimeFormatter FORMATEUR_DATE =
(new DateTimeFormatterBuilder())
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('/')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('/')
.appendValue(ChronoField.YEAR_OF_ERA, 4, 10, SignStyle.EXCEEDS_PAD)
.toFormatter();

public static ObjectMapper handle() {
var objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(new LocalDateSerializer(FORMATEUR_DATE));
objectMapper.registerModule(javaTimeModule);
private static final DateTimeFormatter FORMATEUR_DATE =
(new DateTimeFormatterBuilder())
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('/')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('/')
.appendValue(ChronoField.YEAR_OF_ERA, 4, 10, SignStyle.EXCEEDS_PAD)
.toFormatter();

return objectMapper;
}
}
public static ObjectMapper handle() {
var objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(new LocalDateSerializer(FORMATEUR_DATE));
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

public class TMDBJsonResponseFixture {

public static String pasDeResultat() {
return """
public static String pasDeResultat() {
return """
{
"page": 1,
"results": [],
"total_pages": 1,
"total_results": 0
}
""";
}
}

public static String deuxFilms() {
return """
public static String deuxFilms() {
return """
{
"page": 1,
"results": [
Expand Down Expand Up @@ -63,5 +63,5 @@ public static String deuxFilms() {
"total_results": 146
}
""";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;

import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import com.octo.ajava.fixture.TMDBJsonResponseFixture;
Expand All @@ -19,29 +20,28 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TMDBHttpClientITest {

private TMDBHttpClient tmdbHttpClient;
private TMDBHttpClient tmdbHttpClient;

// permet d'aller chercher dans la valeur de la clé dans le fichier applications.properties
@Value("${tmdb.token}")
String jetonTmdb;
// permet d'aller chercher dans la valeur de la clé dans le fichier applications.properties
@Value("${tmdb.token}")
String jetonTmdb;

@BeforeAll()
public void prepare(WireMockRuntimeInfo wmRuntimeInfo) {
tmdbHttpClient = new TMDBHttpClient(wmRuntimeInfo.getHttpBaseUrl(), jetonTmdb);
}
@BeforeAll()
public void prepare(WireMockRuntimeInfo wmRuntimeInfo) {
tmdbHttpClient = new TMDBHttpClient(wmRuntimeInfo.getHttpBaseUrl(), jetonTmdb);
}

@Test
public void recupererLesFilmsPopulaires() {
// given
stubFor(get("/movie/popular").willReturn(okJson(TMDBJsonResponseFixture.deuxFilms())));
@Test
public void recupererLesFilmsPopulaires() {
// given
stubFor(get("/movie/popular").willReturn(okJson(TMDBJsonResponseFixture.deuxFilms())));

// when
var result = tmdbHttpClient.recupererLesFilmsPopulaires();
// when
var result = tmdbHttpClient.recupererLesFilmsPopulaires();

// then
verify(getRequestedFor(
urlEqualTo("/movie/popular"))
.withHeader("Authorization", equalTo("Bearer " + jetonTmdb))
);
}
}
// then
verify(
getRequestedFor(urlEqualTo("/movie/popular"))
.withHeader("Authorization", equalTo("Bearer " + jetonTmdb)));
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
package com.octo.ajava.infra.controllers;

import static org.assertj.core.api.Assertions.assertThat;

import com.octo.ajava.AjavaApplication;
import com.octo.ajava.ObjectMapperBuilder;
import com.octo.ajava.domain.Film;
import io.restassured.RestAssured;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;

@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = AjavaApplication.class
)
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = AjavaApplication.class)
class FilmControllerFTest {

@Test
void recuperTousLesFilms_devrait_renvoyer_une_HTTP_200_et_une_liste_de_film() throws Exception {
// Given
@Test
void recuperTousLesFilms_devrait_renvoyer_une_HTTP_200_et_une_liste_de_film() throws Exception {
// Given

// When
var response = RestAssured.given()
.get("/api/films")
.then()
.statusCode(HttpStatus.OK.value())
.extract().response().asString();
// When
var response =
RestAssured.given()
.get("/api/films")
.then()
.statusCode(HttpStatus.OK.value())
.extract()
.response()
.asString();

// Then
Film[] listeDeFilms = ObjectMapperBuilder.handle().readValue(response, Film[].class);
// Then
Film[] listeDeFilms = ObjectMapperBuilder.handle().readValue(response, Film[].class);

assertThat(listeDeFilms.length).isEqualTo(22);
}
}
assertThat(listeDeFilms.length).isEqualTo(22);
}
}
29 changes: 14 additions & 15 deletions src/test/java/com/octo/ajava/infra/mapper/TMDBFilmMapperUTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.octo.ajava.ObjectMapperBuilder;
import com.octo.ajava.infra.api_client.entities.PaginatedTMDBMovies;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -11,23 +10,23 @@
@ExtendWith(MockitoExtension.class)
class TMDBFilmMapperUTest {

@InjectMocks
private TMDBFilmMapper tmdbFilmMapper;
@InjectMocks private TMDBFilmMapper tmdbFilmMapper;

@Test
void test() {
// GIVEN
var jsonResponse = """
@Test
void test() {
// GIVEN
var jsonResponse =
"""
// mettre le json que je veux tester ici
""";
var responseBody = ObjectMapperBuilder.handle().readValue(jsonResponse, PaginatedTMDBMovies.class);
var expected = // se servir de la FilmFixture si vous voulez
var responseBody =
ObjectMapperBuilder.handle().readValue(jsonResponse, PaginatedTMDBMovies.class);
var expected = // se servir de la FilmFixture si vous voulez

// WHEN
var result = tmdbFilmMapper.methodeQueJeVeuxTester(responseBody);

// THEN
Assertions.assertEquals(expected, result);
}
// WHEN
var result = tmdbFilmMapper.methodeQueJeVeuxTester(responseBody);

// THEN
Assertions.assertEquals(expected, result);
}
}
1 change: 0 additions & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ server.servlet.session.timeout=60
film.source=TMDB
tmdb.baseUrl=https://api.themoviedb.org/3
tmdb.token=${TMDB_JETON_ACCES}

0 comments on commit c147b7d

Please sign in to comment.