Skip to content

Commit 1d7bd9e

Browse files
committed
Agrega DTO PalindromeResponse y tests funcionando(puerto 8081)
1 parent 03c7d16 commit 1d7bd9e

4 files changed

Lines changed: 44 additions & 24 deletions

File tree

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,30 @@
11
package com.PracticaPropuesta1.controllers;
22

3-
import org.springframework.web.bind.annotation.GetMapping;
4-
import org.springframework.web.bind.annotation.PathVariable;
5-
import org.springframework.web.bind.annotation.RestController;
6-
/*
7-
* Controlador para verificar palíndromos.
8-
*/
3+
import com.PracticaPropuesta1.dto.PalindromeResponse;
4+
import org.springframework.web.bind.annotation.*;
5+
96
@RestController
107
public class PalindromeController {
118

12-
/**
13-
* Endpoint para verificar si una palabra es un palíndromo
14-
* @param word La palabra a verificar
15-
* @return un mesaje indicando si la palabra es un palíndromo o no
16-
*/
17-
18-
@GetMapping ("/validar-palindromo/{word}")
19-
public String Palindrome(@PathVariable String word){
20-
if (isPalindrome(word)){
21-
return "La palabra " + word + " es un palíndromo";
22-
}else {
23-
return "La palabra " + word + " NO es un palíndromo";
24-
}
9+
@GetMapping("/palindrome")
10+
public PalindromeResponse check(@RequestParam String word) {
11+
boolean result = isPalindrome(word);
12+
return new PalindromeResponse(word, result);
2513
}
2614

2715
/**
2816
* Método para verificar si una palabra es un palíndromo
29-
* @param word la palabra a verificar
30-
* @return true si la palabra es un palíndromo, false de lo contrario.
3117
*/
3218
private boolean isPalindrome(String word) {
3319
int length = word.length();
3420
for (int i = 0; i < length / 2; i++) {
3521
if (word.charAt(i) != word.charAt(length - i - 1)) {
3622
return false;
37-
3823
}
3924
}
4025
return true;
4126
}
42-
43-
}
27+
}
4428

4529

4630

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.PracticaPropuesta1.dto;
2+
3+
public record PalindromeResponse(String word, boolean palindrome) {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
spring.application.name=Practica Propuesta 1
2+
server.port=8081
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.PracticaPropuesta1.controller;
2+
3+
import com.PracticaPropuesta1.dto.PalindromeResponse;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.test.web.client.TestRestTemplate;
8+
import org.springframework.http.ResponseEntity;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
13+
class PalindromeControllerTest {
14+
15+
@Autowired
16+
TestRestTemplate rest;
17+
18+
@Test
19+
void ana_debe_ser_palindromo() {
20+
ResponseEntity<PalindromeResponse> res =
21+
rest.getForEntity("/palindrome?word=ana", PalindromeResponse.class);
22+
assertThat(res.getBody().palindrome()).isTrue();
23+
assertThat(res.getBody().word()).isEqualTo("ana");
24+
}
25+
26+
@Test
27+
void hola_no_es_palindromo() {
28+
ResponseEntity<PalindromeResponse> res =
29+
rest.getForEntity("/palindrome?word=hola", PalindromeResponse.class);
30+
assertThat(res.getBody().palindrome()).isFalse();
31+
}
32+
}

0 commit comments

Comments
 (0)