Skip to content

Commit

Permalink
feat: Update Spring Boot version, add H2 dependency, and adjust Objec…
Browse files Browse the repository at this point in the history
…tMapper

- Update Spring Boot version in pom.xml from 3.2.1 to 3.2.2.
- Add H2 database dependency in pom.xml.
- Adjust ObjectMapper interface with null value handling strategies.
- Refactor SchoolController with RequiredArgsConstructor annotation.
- Remove AlreadyExistException class.
- Modify SchoolService update method parameter names for clarity.

Signed-off-by: pierresomny <[email protected]>
  • Loading branch information
pierresomny committed Jan 31, 2024
1 parent 80df494 commit 17e2338
Show file tree
Hide file tree
Showing 13 changed files with 992 additions and 19 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Test

on: [ workflow_dispatch, push ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Run the Maven verify phase
run: mvn --batch-mode --update-snapshots verify
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<version>3.2.2</version>
<relativePath/>
</parent>
<groupId>com.sfeir-open-source</groupId>
Expand Down Expand Up @@ -52,6 +52,10 @@
</dependency>

<!-- TOOLS -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
Expand All @@ -20,17 +21,14 @@
@Slf4j
@RestController
@CrossOrigin("*")
@RequiredArgsConstructor
@RequestMapping("schools")
@SecurityRequirement(name = "bearer-key")
@Tag(name = "School", description = "School controller")
public class SchoolController implements ExceptionInterceptor {

private final SchoolService schoolService;

SchoolController(SchoolService schoolService) {
this.schoolService = schoolService;
}

/**
* Retrieve all schools from database
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
@Mapper(
componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.IGNORE,
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
nullValueCheckStrategy = NullValueCheckStrategy.ON_IMPLICIT_CONVERSION,
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE,
nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_NULL,
nullValueMapMappingStrategy = NullValueMappingStrategy.RETURN_NULL,
nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT,
nullValueMapMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT,
nullValueMappingStrategy = NullValueMappingStrategy.RETURN_NULL
)
public interface ObjectMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public ResponseEntity<Page<SchoolDTO>> search(int pageNumber, int pageSize, Sear
/**
* Update school inside the DB
*
* @param id int
* @param schoolDTO CreateSchoolDTO
* @param id int
* @param createSchoolDTO CreateSchoolDTO
* @return ResponseEntity<SchoolDTO>D
*/
public ResponseEntity<SchoolDTO> update(String id, CreateSchoolDTO schoolDTO) throws NotFoundException {
public ResponseEntity<SchoolDTO> update(String id, CreateSchoolDTO createSchoolDTO) throws NotFoundException {
Optional<School> request = schoolRepository.findById(id);
if (request.isEmpty()) {
throw new NotFoundException("No School is attached to this id");
Expand All @@ -113,7 +113,7 @@ public ResponseEntity<SchoolDTO> update(String id, CreateSchoolDTO schoolDTO) th
School school = request.get();

// Update school from dto.
objectMapper.updateSchoolFromSchoolDto(schoolDTO, school);
objectMapper.updateSchoolFromSchoolDto(createSchoolDTO, school);

schoolRepository.save(school);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.sfeiropensource.schoolapp.controller;

import com.sfeiropensource.schoolapp.exception.NotFoundException;
import com.sfeiropensource.schoolapp.model.CreateSchoolDTO;
import com.sfeiropensource.schoolapp.model.SchoolDTO;
import com.sfeiropensource.schoolapp.model.SearchSchoolDTO;
import com.sfeiropensource.schoolapp.service.SchoolService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

import static com.sfeiropensource.schoolapp.utils.Objects.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

class SchoolControllerTest {

@Mock
private SchoolService schoolService;

@InjectMocks
private SchoolController schoolController;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void getAll() {
ResponseEntity<List<SchoolDTO>> expectedResult = ResponseEntity.ok(List.of());

when(schoolService.getAll()).thenReturn(expectedResult);

ResponseEntity<List<SchoolDTO>> result = schoolController.getAll();

assertEquals(expectedResult, result);
}

@Test
void search() {
int pageNumber = 0;
int pageSize = 10;
SearchSchoolDTO searchSchoolDTO = generateSearchSchoolDTO();
ResponseEntity<Page<SchoolDTO>> expectedResult = ResponseEntity.ok(new PageImpl<>(List.of()));

when(schoolService.search(pageNumber, pageSize, searchSchoolDTO)).thenReturn(expectedResult);

ResponseEntity<Page<SchoolDTO>> result = schoolController.search(searchSchoolDTO, pageNumber, pageSize);

assertEquals(expectedResult, result);
}

@Test
void add() {
CreateSchoolDTO createSchoolDTO = generateCreateSchoolDTO();
ResponseEntity<SchoolDTO> expectedResult = ResponseEntity.ok(generateSchoolDTO());

when(schoolService.saveSchool(createSchoolDTO)).thenReturn(expectedResult);

ResponseEntity<SchoolDTO> result = schoolController.add(createSchoolDTO);

assertEquals(expectedResult, result);
}

@Test
void get() throws NotFoundException {
String id = "id";
ResponseEntity<SchoolDTO> expectedResult = ResponseEntity.ok(generateSchoolDTO());

when(schoolService.get(id)).thenReturn(expectedResult);

ResponseEntity<SchoolDTO> result = schoolController.get(id);

assertEquals(expectedResult, result);
}

@Test
void update() throws NotFoundException {
String id = "id";
CreateSchoolDTO createSchoolDTO = generateCreateSchoolDTO();
ResponseEntity<SchoolDTO> expectedResult = ResponseEntity.ok(generateSchoolDTO());

when(schoolService.update(id, createSchoolDTO)).thenReturn(expectedResult);

ResponseEntity<SchoolDTO> result = schoolController.update(id, createSchoolDTO);

assertEquals(expectedResult, result);
}

@Test
void delete() {
String id = "id";
ResponseEntity<HttpStatus> expectedResult = new ResponseEntity<>(HttpStatus.NO_CONTENT);

when(schoolService.delete(id)).thenReturn(expectedResult);

ResponseEntity<HttpStatus> result = schoolController.delete(id);

assertEquals(expectedResult, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.sfeiropensource.schoolapp.controller;

import com.sfeiropensource.schoolapp.exception.NotFoundException;
import com.sfeiropensource.schoolapp.model.CreateUserDTO;
import com.sfeiropensource.schoolapp.model.SearchUserDTO;
import com.sfeiropensource.schoolapp.model.UserDTO;
import com.sfeiropensource.schoolapp.service.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

import static com.sfeiropensource.schoolapp.utils.Objects.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

class UserControllerTest {

@Mock
private UserService schoolService;

@InjectMocks
private UserController schoolController;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void getAll() {
ResponseEntity<List<UserDTO>> expectedResult = ResponseEntity.ok(List.of());

when(schoolService.getAll()).thenReturn(expectedResult);

ResponseEntity<List<UserDTO>> result = schoolController.getAll();

assertEquals(expectedResult, result);
}

@Test
void search() {
int pageNumber = 0;
int pageSize = 10;
SearchUserDTO searchUserDTO = generateSearchUserDTO();
ResponseEntity<Page<UserDTO>> expectedResult = ResponseEntity.ok(new PageImpl<>(List.of()));

when(schoolService.search(pageNumber, pageSize, searchUserDTO)).thenReturn(expectedResult);

ResponseEntity<Page<UserDTO>> result = schoolController.search(searchUserDTO, pageNumber, pageSize);

assertEquals(expectedResult, result);
}

@Test
void add() {
CreateUserDTO createUserDTO = generateCreateUserDTO();
ResponseEntity<UserDTO> expectedResult = ResponseEntity.ok(generateUserDTO());

when(schoolService.add(createUserDTO)).thenReturn(expectedResult);

ResponseEntity<UserDTO> result = schoolController.add(createUserDTO);

assertEquals(expectedResult, result);
}

@Test
void get() throws NotFoundException {
String id = "id";
ResponseEntity<UserDTO> expectedResult = ResponseEntity.ok(generateUserDTO());

when(schoolService.get(id)).thenReturn(expectedResult);

ResponseEntity<UserDTO> result = schoolController.get(id);

assertEquals(expectedResult, result);
}

@Test
void update() throws NotFoundException {
String id = "id";
CreateUserDTO createUserDTO = generateCreateUserDTO();
ResponseEntity<UserDTO> expectedResult = ResponseEntity.ok(generateUserDTO());

when(schoolService.update(id, createUserDTO)).thenReturn(expectedResult);

ResponseEntity<UserDTO> result = schoolController.update(id, createUserDTO);

assertEquals(expectedResult, result);
}

@Test
void delete() {
String id = "id";
ResponseEntity<HttpStatus> expectedResult = new ResponseEntity<>(HttpStatus.NO_CONTENT);

when(schoolService.delete(id)).thenReturn(expectedResult);

ResponseEntity<HttpStatus> result = schoolController.delete(id);

assertEquals(expectedResult, result);
}
}
Loading

0 comments on commit 17e2338

Please sign in to comment.