AbrigoServiceTest:
package br.com.alura;
import br.com.alura.cliente.ClienteHttpConfiguration;
import br.com.alura.domain.Abrigo;
import br.com.alura.service.AbrigoService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.http.HttpResponse;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class AbrigoServiceTest {
private ClienteHttpConfiguration client = mock(ClienteHttpConfiguration.class);
private AbrigoService abrigoService = new AbrigoService(client);
private HttpResponse<String> response = mock(HttpResponse.class);
private Abrigo abrigo = new Abrigo("Teste", "61981880392", "abrigo_alura@gmail.com");
@Test
public void deveVerificarQuandoHaAbrigo() throws IOException, InterruptedException {
abrigo.setId(0L);
String expectedAbrigosCadastrados = "Abrigos cadastrados:";
String expectedIdENome = "0 - Teste";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
System.setOut(printStream);
when(response.body()).thenReturn("[{"+abrigo.toString()+"}]");
when(client.dispararRequisicaoGet(anyString())).thenReturn(response);
abrigoService.listarAbrigo();
String[] lines = baos.toString().split(System.lineSeparator());
String actualAbrigosCadastrados = lines[0];
String actualIdENome = lines[1];
Assertions.assertEquals(expectedAbrigosCadastrados, actualAbrigosCadastrados);
Assertions.assertEquals(expectedIdENome, actualIdENome);
}
@Test
public void deveVerificarQuandoNaoHaAbrigo() throws IOException, InterruptedException {
abrigo.setId(0L);
String expected = "Não há abrigos cadastrados";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
System.setOut(printStream);
when(response.body()).thenReturn("[]");
when(client.dispararRequisicaoGet(anyString())).thenReturn(response);
abrigoService.listarAbrigo();
String[] lines = baos.toString().split(System.lineSeparator());
String actual = lines[0];
Assertions.assertEquals(expected, actual);
}
}
AbrigoService:
imports feitos!!
public class AbrigoService {
private ClienteHttpConfiguration client;
public AbrigoService(ClienteHttpConfiguration client) {
this.client = client;
}
public void listarAbrigo() {
}
public void listarAbrigos() throws IOException, InterruptedException {
String uri = "http://localhost:8080/abrigos";
HttpResponse<String> response = client.dispararRequisicaoGet(uri);
String responseBody = response.body();
Abrigo[] abrigos = new ObjectMapper().readValue(responseBody, Abrigo[].class);
List<Abrigo> abrigosList = Arrays.stream(abrigos).toList();
if (abrigosList.isEmpty()) {
System.out.println("Não há abrigos cadastrados.");
} else {
mostrarAbrigos(abrigosList);
}
}
private void mostrarAbrigos(List<Abrigo> abrigos){
System.out.println("Abrigos cadastrados:");
for (Abrigo abrigo : abrigos) {
long id = abrigo.getId();
String nome = abrigo.getNome();
System.out.println(id +" - " +nome);
}
}
public void cadastrarAbrigo() throws IOException, InterruptedException {
System.out.println("Digite o nome do abrigo:");
String nome = new Scanner(System.in).nextLine();
System.out.println("Digite o telefone do abrigo:");
String telefone = new Scanner(System.in).nextLine();
System.out.println("Digite o email do abrigo:");
String email = new Scanner(System.in).nextLine();
Abrigo abrigo = new Abrigo(nome, telefone, email);
String uri = "http://localhost:8080/abrigos";
HttpResponse<String> response = client.dispararRequisicaoPost(uri, abrigo);
int statusCode = response.statusCode();
String responseBody = response.body();
if (statusCode == 200) {
System.out.println("Abrigo cadastrado com sucesso!");
System.out.println(responseBody);
} else if (statusCode == 400 || statusCode == 500) {
System.out.println("Erro ao cadastrar o abrigo:");
System.out.println(responseBody);
}
}
}