├── settings.gradle ├── src ├── main │ └── crypto │ │ ├── Domain │ │ └── Coin.java │ │ ├── ApplicationRunner.java │ │ ├── Controllers │ │ ├── GetAppStatusController.java │ │ └── GetGreetingController.java │ │ ├── Client.java │ │ └── Repositories │ │ ├── CryptoRepository.java │ │ └── MixedCryptoRepository.java └── test │ └── crypto │ ├── Controllers │ ├── GetAppStatusControllerTest.java │ └── GetGreetingControllerTest.java │ └── Repositories │ └── CryptoRepositoryTest.java ├── .gitignore └── README.md /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'CryptoPlatform' 2 | 3 | -------------------------------------------------------------------------------- /src/main/crypto/Domain/Coin.java: -------------------------------------------------------------------------------- 1 | package crypto.Domain; 2 | 3 | public class Coin { 4 | private final String name; 5 | 6 | public Coin(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/crypto/ApplicationRunner.java: -------------------------------------------------------------------------------- 1 | package crypto; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ApplicationRunner { 8 | public static void main(String[] args) { 9 | SpringApplication.run(ApplicationRunner.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/crypto/Controllers/GetAppStatusController.java: -------------------------------------------------------------------------------- 1 | package crypto.Controllers; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class GetAppStatusController { 8 | @GetMapping("/status") 9 | public String index() { 10 | return "Make this work :)"; 11 | } 12 | } -------------------------------------------------------------------------------- /src/main/crypto/Controllers/GetGreetingController.java: -------------------------------------------------------------------------------- 1 | package crypto.Controllers; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class GetGreetingController { 8 | @GetMapping("/greeting") 9 | public String index() { 10 | return "Make this work :)"; 11 | } 12 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | *.DS_Store 3 | 4 | # Java class files 5 | *.class 6 | 7 | # Generated files 8 | bin/ 9 | gen/ 10 | 11 | # Gradle files 12 | gradle/ 13 | gradlew 14 | gradlew.bat 15 | .gradle/ 16 | build/ 17 | /*/build/ 18 | *.iml 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Log Files 24 | *.log 25 | 26 | .idea/ 27 | -------------------------------------------------------------------------------- /src/main/crypto/Client.java: -------------------------------------------------------------------------------- 1 | package crypto; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.JsonObject; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.io.IOException; 8 | import java.net.http.HttpClient; 9 | import java.net.http.HttpRequest; 10 | import java.net.http.HttpResponse; 11 | 12 | @Component 13 | public class Client { 14 | private final HttpClient client; 15 | 16 | public Client() { 17 | this.client = HttpClient.newHttpClient(); 18 | } 19 | 20 | public JsonObject send(HttpRequest request) { 21 | try { 22 | HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); 23 | return new Gson().fromJson(response.body(), JsonObject.class); 24 | } catch (IOException | InterruptedException e) { 25 | throw new RuntimeException(e); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/crypto/Controllers/GetAppStatusControllerTest.java: -------------------------------------------------------------------------------- 1 | package crypto.Controllers; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.test.web.servlet.MockMvc; 9 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 10 | 11 | import static org.hamcrest.Matchers.equalTo; 12 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 13 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 14 | 15 | @SpringBootTest 16 | @AutoConfigureMockMvc 17 | public class GetAppStatusControllerTest { 18 | @Autowired 19 | private MockMvc mvc; 20 | 21 | @Test 22 | public void systemIsDown() throws Exception { 23 | mvc.perform(MockMvcRequestBuilders.get("/estado-url").accept(MediaType.APPLICATION_JSON)) 24 | .andExpect(status().isOk()) 25 | .andExpect(content().string(equalTo("We are down!"))); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/crypto/Controllers/GetGreetingControllerTest.java: -------------------------------------------------------------------------------- 1 | package crypto.Controllers; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.test.web.servlet.MockMvc; 9 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 10 | 11 | import static org.hamcrest.Matchers.equalTo; 12 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 13 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 14 | 15 | @SpringBootTest 16 | @AutoConfigureMockMvc 17 | public class GetGreetingControllerTest { 18 | @Autowired 19 | private MockMvc mvc; 20 | 21 | @Test 22 | public void getsRegards() throws Exception { 23 | mvc.perform(MockMvcRequestBuilders.get("/greeting").accept(MediaType.APPLICATION_JSON)) 24 | .andExpect(status().isOk()) 25 | .andExpect(content().string(equalTo("Implement this part"))); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/crypto/Repositories/CryptoRepository.java: -------------------------------------------------------------------------------- 1 | package crypto.Repositories; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.JsonElement; 5 | import com.google.gson.JsonObject; 6 | import crypto.Client; 7 | import crypto.Domain.Coin; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Repository; 10 | 11 | import java.net.URI; 12 | import java.net.http.HttpRequest; 13 | import java.util.List; 14 | 15 | @Repository 16 | public class CryptoRepository { 17 | @Autowired 18 | private final Client client; 19 | 20 | public CryptoRepository(Client client) { 21 | this.client = client; 22 | } 23 | 24 | public List getCoins() { 25 | HttpRequest request = HttpRequest 26 | .newBuilder(URI.create("https://api.coinlore.net/api/tickers/")) 27 | .header("accept", "application/json") 28 | .build(); 29 | 30 | Coin[] coins = new Coin[0]; 31 | JsonObject jsonObject = client.send(request); 32 | if (jsonObject != null){ 33 | JsonElement data = jsonObject.get("data"); 34 | coins = new Gson().fromJson(data, Coin[].class); 35 | } 36 | 37 | return List.of(coins); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/crypto/Repositories/MixedCryptoRepository.java: -------------------------------------------------------------------------------- 1 | package crypto.Repositories; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.JsonElement; 5 | import com.google.gson.JsonObject; 6 | import crypto.Domain.Coin; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.io.IOException; 10 | import java.net.URI; 11 | import java.net.http.HttpClient; 12 | import java.net.http.HttpRequest; 13 | import java.net.http.HttpResponse; 14 | import java.util.List; 15 | 16 | @Component 17 | public class MixedCryptoRepository { 18 | public List getCoins() { 19 | HttpClient client = HttpClient.newHttpClient(); 20 | HttpRequest request = HttpRequest 21 | .newBuilder(URI.create("https://api.coinlore.net/api/tickers/")) 22 | .header("accept", "application/json") 23 | .build(); 24 | 25 | Coin[] coins; 26 | try { 27 | HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); 28 | JsonObject jsonObject = new Gson().fromJson(response.body(), JsonObject.class); 29 | JsonElement data = jsonObject.get("data"); 30 | coins = new Gson().fromJson(data, Coin[].class); 31 | } catch (IOException | InterruptedException e) { 32 | throw new RuntimeException(e); 33 | } 34 | 35 | return List.of(coins); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Crypto platform 2 | 3 | Ejercicio para trabajar dobles de test, test unitarios y tests de integración 4 | 5 | # Descripción 6 | 7 | Crypto platform es una API que nos va a permitir estás al día en el mercado del estado de las criptomonedas. 8 | 9 | Para eso, hace uso de una API externa cuya documentación se encuentra [aquí](https://www.coinlore.com/cryptocurrency-data-api) 10 | 11 | Nuestra idea es ir añadiendo funcionalidades poco a poco en medida que las vayamos necesitando, pero el proyecto es 12 | ambicioso y va a tener clave para la organización dado que nos va a permitir tomar mejores decisiones gracias 13 | a la información procesada y organizada en esta plataforma. 14 | 15 | 16 | ## Properties 17 | 18 | Java version: 18 19 | 20 | Gradle version: 7.5 21 | 22 | Uses Spring boot as base framework and GSON for object serialization. 23 | 24 | Build project: 25 | - ./gradlew build 26 | 27 | :warning: :warning: **Build will fail because of tests, this is done in purpose, have a look at them!** 28 | 29 | Run project: 30 | - ./gradlew bootRun 31 | 32 | --- 33 | 34 | - Get Java version: 35 | - java --version 36 | 37 | - Update Java version: 38 | - Download the JDK (version 17 right now) 39 | - Remember to adjust Intellij settings File > Project Structure > Project 40 | 41 | --- 42 | 43 | - Get current Gradle version: 44 | - ./gradlew --version 45 | 46 | - Update Gradle version ([take in account project's Java version for compatibility](https://docs.gradle.org/current/userguide/compatibility.html)): 47 | - ./gradlew wrapper --gradle-version -------------------------------------------------------------------------------- /src/test/crypto/Repositories/CryptoRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package crypto.Repositories; 2 | 3 | import com.google.gson.JsonObject; 4 | import com.google.gson.JsonParser; 5 | import crypto.Client; 6 | import crypto.Domain.Coin; 7 | import crypto.Repositories.CryptoRepository; 8 | import org.junit.jupiter.api.Test; 9 | 10 | import java.net.http.HttpRequest; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | import static org.junit.jupiter.api.Assertions.assertEquals; 15 | import static org.mockito.ArgumentMatchers.any; 16 | import static org.mockito.Mockito.mock; 17 | import static org.mockito.Mockito.when; 18 | 19 | public class CryptoRepositoryTest { 20 | @Test 21 | void getsNoCoins() { 22 | Client client = mock(Client.class); 23 | CryptoRepository cryptoRepository = new CryptoRepository(client); 24 | 25 | when(client.send(any(HttpRequest.class))).thenReturn(null); 26 | 27 | List coins = cryptoRepository.getCoins(); 28 | 29 | assertEquals(new ArrayList(), coins); 30 | } 31 | 32 | @Test 33 | void getsCoins() { 34 | Client client = mock(Client.class); 35 | CryptoRepository cryptoRepository = new CryptoRepository(client); 36 | List expectedCoins = new ArrayList<>(); 37 | expectedCoins.add(new Coin("Bitcoin")); 38 | String jsonString = "{data: [{name: Bitcoin}]}"; 39 | JsonObject jsonResponse = (JsonObject) JsonParser.parseString(jsonString); 40 | 41 | when(client.send(any(HttpRequest.class))).thenReturn(jsonResponse); 42 | List coins = cryptoRepository.getCoins(); 43 | 44 | assertEquals(expectedCoins.get(0).getName(), coins.get(0).getName()); 45 | } 46 | } 47 | --------------------------------------------------------------------------------