Skip to content

Commit

Permalink
update files
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerJuve2023 committed Jan 30, 2024
1 parent 8b8cb4b commit f38015f
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 19 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ no apoyan ni promueven actividades ilegales o maliciosas.
- [Puntos Fin de API](#puntos-fin-de-api)
- [Configuración de la Base de Datos](#configuración-de-la-base-de-datos)
- [Registro](#registro)
- [Ejecutar Pruebas](#ejecutar-pruebas)

## Visión General

Expand Down Expand Up @@ -123,6 +124,7 @@ spring:
```

## Uso

```bash
docker run -p 8080:8080 intranet-backend

Expand Down Expand Up @@ -168,3 +170,10 @@ La aplicación utiliza la configuración de registro predeterminada de Spring Bo
paquete org.springframework están configurados en INFO. Puede personalizar la configuración de registro en el archivo
`application.yml`.

## Ejecutar Pruebas

Para ejecutar las pruebas, puedes utilizar el siguiente comando:

```bash
./mvnw test
```
5 changes: 4 additions & 1 deletion src/main/java/com/example/intranetbackend/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.intranetbackend.domain;

import jakarta.persistence.*;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.intranetbackend.dto;

import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.intranetbackend.dto;

import lombok.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example.intranetbackend.controller;

import com.example.intranetbackend.domain.User;
import com.example.intranetbackend.dto.UserRequest;
import com.example.intranetbackend.dto.UserResponse;
import com.example.intranetbackend.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@WebMvcTest(UserController.class)
class UserControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private UserService userService;

@Test
void testGetUser() throws Exception {
// Arrange
UserResponse userResponse = new UserResponse("tester", "password", null);
when(userService.getUsers()).thenReturn(Collections.singletonList(userResponse));

// Act & Assert
mockMvc.perform(MockMvcRequestBuilders.get("/api/user/findAll")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(1))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].username").value("tester"))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].password").value("password"));
}

@Test
void testSetUser() throws Exception {
// Arrange
UserRequest userRequest = new UserRequest("tester", "password");

// Mocking the service method call
when(userService.setUser(any(UserRequest.class))).thenReturn(new User());

// Act & Assert
mockMvc.perform(MockMvcRequestBuilders.post("/api/user/add")
.content(asJsonString(userRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk());
}

private static String asJsonString(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.example.intranetbackend.service;

import com.example.intranetbackend.domain.User;
import com.example.intranetbackend.dto.UserRequest;
import com.example.intranetbackend.dto.UserResponse;
import com.example.intranetbackend.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
class UserServiceTest {

@Mock
private UserRepository userRepository;

@InjectMocks
private UserService userService;

@Test
void testSetUser() {
// Arrange
UserRequest userRequest = new UserRequest("tester", "password");
// Mocking the repository save method
Mockito.when(userRepository.save(Mockito.any(User.class))).thenReturn(new User());
// Act
User savedUser = userService.setUser(userRequest);
// Assert
assertNotNull(savedUser);

}


@Test
void testGetUsers() {
// Arrange
UserResponse userResponse = new UserResponse("tester", "password", null);
List<UserResponse> userResponses = Arrays.asList(userResponse);
// Mocking the repository method call
Mockito.when(userRepository.findUsernameAndPasswordAndFecha()).thenReturn(userResponses);

// Act
List<UserResponse> result = userService.getUsers();

// Assert
assertEquals(1, result.size());
assertEquals("tester", result.get(0).getUsername());
assertEquals("password", result.get(0).getPassword());
}
}

0 comments on commit f38015f

Please sign in to comment.