Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.projectlombok:lombok:1.18.24'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
}

tasks.named('test') {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/origin/challenge/ChallengeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.net.http.HttpClient;

@SpringBootApplication
public class ChallengeApplication {
Expand All @@ -10,4 +13,8 @@ public static void main(String[] args) {
SpringApplication.run(ChallengeApplication.class, args);
}

@Bean
public HttpClient httpClient() {
return HttpClient.newHttpClient();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
package com.origin.challenge.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

@RestController
@RequestMapping("/pokemon")
public class PokemonController {

@Autowired
private HttpClient httpClient;

@GetMapping
public ResponseEntity<Map<String, String>> root() {
return ResponseEntity.ok(Map.of("message", "Hello World"));
public ResponseEntity<PokemonDTO> listPokemon(@RequestParam(name = "page", defaultValue = "1") int page)
throws Exception {
final int limit = 20;
final int offset = page - 1;
final String url = String.format("https://pokeapi.co/api/v2/pokemon?offset=%d&limit=%d", offset, limit);

final HttpRequest r = HttpRequest.newBuilder()
.uri(new URI(url))
.header(
"Authorization",
"eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZ" +
"SIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI"
)
.GET()
.build();
final HttpResponse<String> r2 = this.httpClient.send(r, HttpResponse.BodyHandlers.ofString());

final ObjectMapper objectMapper = new ObjectMapper();
final PokemonDTO r3 = objectMapper.readValue(r2.body(), PokemonDTO.class);

return ResponseEntity.ok(r3);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/origin/challenge/controllers/PokemonDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.origin.challenge.controllers;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class PokemonDTO {
@JsonProperty("results")
private List<Type> results;

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Type {
@JsonProperty("name")
private String name;

@JsonProperty("url")
private String url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.origin.challenge.controllers;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
@SuppressWarnings({"unchecked", "rawtypes"})
public class PokemonControllerIntegrationTest {
@Mock
private HttpClient httpClient;

@InjectMocks
private PokemonController pokemonController;


private static HttpResponse httpResponse;


@BeforeEach
public void setup() {
httpResponse = mock(HttpResponse.class);
when(httpResponse.body()).thenReturn("{\"count\":1154,\"next\":\"https://pokeapi.co/api/v2/pokemon?offset=20" +
"&limit=20\",\"previous\":null,\"results\":[{\"name\":\"bulbasaur\",\"url\":\"https://pokeapi.co/api" +
"/v2/pokemon/1/\"}]}"
);
}


@Test
void test1() throws Exception {
when(httpClient.send(any(), any())).thenReturn(httpResponse);
final HttpRequest expected = HttpRequest.newBuilder()
.uri(new URI("https://pokeapi.co/api/v2/pokemon?offset=0&limit=20"))
.header(
"Authorization",
"eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZ" +
"SIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI"
)
.GET()
.build();

pokemonController.listPokemon(1);
verify(httpClient, times(1)).send(expected, HttpResponse.BodyHandlers.ofString());
}

@Test
void test2() throws Exception {
when(httpClient.send(any(), any())).thenReturn(httpResponse);
final HttpRequest expected = HttpRequest.newBuilder()
.uri(new URI("https://pokeapi.co/api/v2/pokemon?offset=20&limit=20"))
.header(
"Authorization",
"eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZ" +
"SIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI"
)
.GET()
.build();

pokemonController.listPokemon(2);
verify(httpClient, times(1)).send(expected, HttpResponse.BodyHandlers.ofString());
}
}