Skip to content

Commit

Permalink
#2 Crud de curso - Alteração de Review
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhê Cardoso committed Feb 11, 2021
1 parent 6a54802 commit 161126f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 43 deletions.
9 changes: 4 additions & 5 deletions src/main/java/org/ayty/hatcher/api/v1/course/dto/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.Serializable;

@Entity
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude={"name", "description"})
Expand All @@ -25,10 +26,8 @@ public class Course implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter @Setter private Long id;

@Getter @Setter private String name;

@Getter @Setter private String description;
private Long id;
private String name;
private String description;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,57 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
import java.util.List;


@RequiredArgsConstructor
@RestController
@RequestMapping("/courses")
public class CourseController {
public final class CourseController {

@Autowired
private final CourseService service;

@GetMapping
@ResponseStatus(code = HttpStatus.OK)
public ResponseEntity<List<Course>> findAll() {
List<Course> list = service.findAll();
return ResponseEntity.ok().body(list);
public List<Course> findAll() {
return service.findAll();
}

@GetMapping(value = "/{id}")
@ResponseStatus(code = HttpStatus.OK)
public ResponseEntity<Course> findById (@PathVariable Long id){
Course obj = this.service.findById(id);
return ResponseEntity.ok().body(obj);
public Course findById (@PathVariable Long id){
return this.service.findById(id);
}

@PutMapping(value = "/{id}")
@ResponseStatus(code = HttpStatus.NO_CONTENT)
public ResponseEntity <Course> update (@PathVariable Long id, @RequestBody Course obj) {
public void update (@PathVariable Long id, @RequestBody Course obj) {
Course newObj = this.service.update(id, obj);
return ResponseEntity.ok().body(newObj);
}

@PostMapping
@ResponseStatus(code = HttpStatus.CREATED)
public ResponseEntity<Course> create(@RequestBody Course obj) {
public void create(@RequestBody Course obj) {
Course newObj = service.create(obj);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(newObj.getId()).toUri();
return ResponseEntity.created(uri).build();
}

@DeleteMapping(value = "/{id}")
@ResponseStatus(code = HttpStatus.NO_CONTENT)
public ResponseEntity<Course> delete(@PathVariable Long id){
public void delete(@PathVariable Long id){
service.delete(id);
return ResponseEntity.noContent().build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public class StandardError {

@Getter @Setter private Integer status;
@Getter @Setter private Long timestamp;
@Getter @Setter private String message;
private Integer status;
private Long timestamp;
private String message;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ayty.hatcher.api.v1.course.service;


import lombok.RequiredArgsConstructor;
import org.ayty.hatcher.api.v1.course.dto.Course;
import org.ayty.hatcher.api.v1.course.exception.ObjectNotFoundException;
import org.ayty.hatcher.api.v1.course.jpa.CourseRepository;
Expand All @@ -13,6 +14,7 @@
import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class CourseService {

Expand All @@ -33,11 +35,10 @@ public Course update (Long id, Course obj) {
return repository.save(newObj);
}
public Course create(Course obj) {
obj.setId(null);
return repository.save(obj);
}
public void delete(Long id) {
findById(id);
repository.existsById(id);
repository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,81 @@

import org.ayty.hatcher.api.v1.course.dto.Course;
import org.ayty.hatcher.api.v1.course.jpa.CourseRepository;
import org.junit.jupiter.api.DisplayName;
import org.ayty.hatcher.api.v1.course.service.CourseService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;

@ExtendWith(MockitoExtension.class)
@Tag("Repository")
@DisplayName("Persistence of Data")

@DataJpaTest
class CourseRepositoryTest {

@Autowired
@Mock
private CourseRepository courseRepository;

@InjectMocks
CourseService courseService;

private static final String COURSE_NAME= "SI";
private static final String DESCRIPTION = "Description";
private static final Long ID = 1L;

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


}
@Test
void createShouldPersistData(){
Course course = new Course(null, "name", "desc");
this.courseRepository.save(course);
assertThat(course.getId()).isNotNull();
assertThat(course.getName()).isEqualTo("name");
assertThat(course.getDescription()).isEqualTo("desc");
@DisplayName("Should save data")
final void shouldPersist() {

Course course = new Course(ID,COURSE_NAME,DESCRIPTION);

when(courseService.create(course)).thenReturn(course);
courseService.create(course);

verify(courseRepository, times(1)).save(course);

assertThat(course.getId()).isNotNull();
assertThat(course.getName()).isNotEmpty();
assertThat(course.getDescription()).isNotEmpty();
assertThat(course.getId()).isEqualTo(1L);
assertThat(course.getName()).isEqualTo("SI");
}


@Test
@DisplayName("Should remove data")
void deleteShouldRemoveData() {
Course course = new Course(null, "name", "desc");
this.courseRepository.save(course);
Course course = new Course(ID, COURSE_NAME, DESCRIPTION);

when(courseService.create(course)).thenReturn(course);
courseService.create(course);

this.courseRepository.delete(course);
assertThat(courseRepository.findById(course.getId())).isEmpty();

}

@Test
@DisplayName("Should modify data")
void updateShouldChangeAndPersistData(){
Course course = new Course(null, "name", "desc");
this.courseRepository.save(course);
Course course = new Course(ID, COURSE_NAME, DESCRIPTION);
when(courseService.create(course)).thenReturn(course);
courseService.create(course);
course.setName("name2");
course.setDescription("Desc2");
this.courseRepository.save(course);
Expand All @@ -52,4 +87,5 @@ void updateShouldChangeAndPersistData(){

}


}

0 comments on commit 161126f

Please sign in to comment.