From 86b8407b9abc26a574ca26ee73bebe30ebe92388 Mon Sep 17 00:00:00 2001 From: Sergi-Virgili Date: Sun, 18 Feb 2024 19:40:11 +0100 Subject: [PATCH 1/6] Add Student PUT Controller --- .../app/mooc/controller/students/Request.java | 22 ++++++++++++++++ .../students/StudentsPutController.java | 26 +++++++++++++++++++ .../students/StudentsPutControllerShould.java | 17 ++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 app/main/tv/codely/app/mooc/controller/students/Request.java create mode 100644 app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java create mode 100644 app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java diff --git a/app/main/tv/codely/app/mooc/controller/students/Request.java b/app/main/tv/codely/app/mooc/controller/students/Request.java new file mode 100644 index 00000000..5677ce24 --- /dev/null +++ b/app/main/tv/codely/app/mooc/controller/students/Request.java @@ -0,0 +1,22 @@ +package tv.codely.app.mooc.controller.students; + +public class Request { + private String name; + private String surname; + + public String name() { + return name; + } + + public String surname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java b/app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java new file mode 100644 index 00000000..979d46bb --- /dev/null +++ b/app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java @@ -0,0 +1,26 @@ +package tv.codely.app.mooc.controller.students; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import tv.codely.mooc.students.application.create.StudentCreator; + +@RestController +public class StudentsPutController { + + private final StudentCreator creator; + + public StudentsPutController(StudentCreator creator) { + this.creator = creator; + } + + @PutMapping("/students/{id}") + public ResponseEntity create(@PathVariable String id, @RequestBody Request request) { + creator.create(id, request.name(), request.surname()); + + return new ResponseEntity<>(HttpStatus.CREATED); + } +} diff --git a/app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java b/app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java new file mode 100644 index 00000000..9f5a627b --- /dev/null +++ b/app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java @@ -0,0 +1,17 @@ +package tv.codely.app.mooc.controller.students; + +import org.junit.jupiter.api.Test; +import tv.codely.app.mooc.controller.RequestTestCase; + +public class StudentsPutControllerShould extends RequestTestCase { + + @Test + public void create_a_valid_non_existing_student() throws Exception { + assertRequestWithBody( + "PUT", + "/students/1aab45ba-3c7a-4344-8936-78466eca77fa", + "{\"name\": \"The best student\", \"lastname\": \"The best lastname\"}", + 201); + } + +} From 0acfe68c7f8ae6cbd3dd154551ace85a3334abf9 Mon Sep 17 00:00:00 2001 From: Sergi-Virgili Date: Sun, 18 Feb 2024 19:42:28 +0100 Subject: [PATCH 2/6] Add Student Creator UseCase --- .../application/create/StudentCreator.java | 18 +++++++++ .../codely/mooc/students/domain/Student.java | 40 +++++++++++++++++++ .../students/domain/StudentRepository.java | 9 +++++ .../create/StudentCreatorTest.java | 26 ++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java create mode 100644 src/mooc/main/tv/codely/mooc/students/domain/Student.java create mode 100644 src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java create mode 100644 src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java diff --git a/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java b/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java new file mode 100644 index 00000000..76f9f5d4 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java @@ -0,0 +1,18 @@ +package tv.codely.mooc.students.application.create; + +import tv.codely.mooc.students.domain.Student; +import tv.codely.mooc.students.domain.StudentRepository; +import tv.codely.shared.domain.Service; + +@Service +public class StudentCreator { + private final StudentRepository repository; + + public StudentCreator(StudentRepository repository) { + this.repository = repository; + } + + public void create(String id, String name, String surname) { + repository.save(new Student(id, name, surname)); + } +} diff --git a/src/mooc/main/tv/codely/mooc/students/domain/Student.java b/src/mooc/main/tv/codely/mooc/students/domain/Student.java new file mode 100644 index 00000000..d42c355c --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/domain/Student.java @@ -0,0 +1,40 @@ +package tv.codely.mooc.students.domain; + +public class Student { + private String id; + private String name; + private String surname; + + public Student(String id, String name, String surname) { + this.id = id; + this.name = name; + this.surname = surname; + } + + public String id() { + return id; + } + + public String name() { + return name; + } + + public String surname() { + return surname; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Student)) return false; + + Student student = (Student) o; + + return id.equals(student.id); + } + + @Override + public int hashCode() { + return id.hashCode(); + } +} diff --git a/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java b/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java new file mode 100644 index 00000000..2a6c36ec --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java @@ -0,0 +1,9 @@ +package tv.codely.mooc.students.domain; + +import java.util.Optional; + +public interface StudentRepository { + void save(Student student); + + Optional search(String id); +} diff --git a/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java b/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java new file mode 100644 index 00000000..d41e0e44 --- /dev/null +++ b/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java @@ -0,0 +1,26 @@ +package tv.codely.mooc.students.application.create; + +import org.junit.jupiter.api.Test; +import tv.codely.mooc.students.domain.Student; +import tv.codely.mooc.students.domain.StudentRepository; + +import static org.mockito.Mockito.*; +public class StudentCreatorTest { + + @Test + public void create_a_valid_student() { + StudentRepository repository = mock(StudentRepository.class); + StudentCreator creator = new StudentCreator(repository); + + String id = "some-id"; + String name = "name"; + String surname = "surname"; + + Student student = new Student(id, name, surname); + + creator.create(student.id(), student.name(), student.surname()); + + verify(repository, atLeastOnce()).save(student); + } + +} From d765b976f8b326ecb26f850d5f5a05c310d5f2dd Mon Sep 17 00:00:00 2001 From: Sergi-Virgili Date: Sun, 18 Feb 2024 19:42:51 +0100 Subject: [PATCH 3/6] Add Student Repository In Memory Implementation --- .../InMemoryStudentRepository.java | 22 ++++++++++++ .../InMemoryStudentRepositoryTest.java | 36 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java create mode 100644 src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java diff --git a/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java b/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java new file mode 100644 index 00000000..56905d04 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java @@ -0,0 +1,22 @@ +package tv.codely.mooc.students.infrastructure.persistence; + +import tv.codely.mooc.students.domain.Student; +import tv.codely.mooc.students.domain.StudentRepository; +import tv.codely.shared.domain.Service; + +import java.util.HashMap; +import java.util.Optional; + +@Service +public class InMemoryStudentRepository implements StudentRepository { + private final HashMap students = new HashMap<>(); + + public void save(Student student) { + students.put(student.id(), student); + } + + @Override + public Optional search(String id) { + return Optional.ofNullable(students.get(id)); + } +} diff --git a/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java b/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java new file mode 100644 index 00000000..b01d3fc0 --- /dev/null +++ b/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java @@ -0,0 +1,36 @@ +package tv.codely.mooc.students.infrastructure.persistence; + +import org.junit.jupiter.api.Test; +import tv.codely.mooc.students.domain.Student; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +class InMemoryStudentRepositoryTest { + + @Test + void save_a_student() { + InMemoryStudentRepository repository = new InMemoryStudentRepository(); + Student student = new Student("id", "name", "surname"); + + repository.save(student); + } + + @Test + void return_an_existing_student() { + InMemoryStudentRepository repository = new InMemoryStudentRepository(); + Student student = new Student("id", "name", "surname"); + + repository.save(student); + + assertEquals(Optional.of(student), repository.search(student.id())); + } + + @Test + void not_return_a_non_existing_student() { + InMemoryStudentRepository repository = new InMemoryStudentRepository(); + + assertFalse(repository.search("randomId").isPresent()); + } +} From 82b41620ce1e964b71c7861a3d027c2a10e29d3b Mon Sep 17 00:00:00 2001 From: Sergi-Virgili Date: Mon, 19 Feb 2024 09:04:54 +0100 Subject: [PATCH 4/6] Students app add email field and moved to new apps folder --- .../app/mooc/controller/students/Request.java | 22 ------------------- .../mooc/controller/students/Request.java | 22 +++++++++++++++++++ .../students/StudentsPutController.java | 7 ++++-- .../students/StudentsPutControllerShould.java | 6 ++--- 4 files changed, 30 insertions(+), 27 deletions(-) delete mode 100644 app/main/tv/codely/app/mooc/controller/students/Request.java create mode 100644 apps/main/tv/codely/apps/mooc/controller/students/Request.java rename {app/main/tv/codely/app => apps/main/tv/codely/apps}/mooc/controller/students/StudentsPutController.java (73%) rename {app/test/tv/codely/app => apps/test/tv/codely/apps}/mooc/controller/students/StudentsPutControllerShould.java (63%) diff --git a/app/main/tv/codely/app/mooc/controller/students/Request.java b/app/main/tv/codely/app/mooc/controller/students/Request.java deleted file mode 100644 index 5677ce24..00000000 --- a/app/main/tv/codely/app/mooc/controller/students/Request.java +++ /dev/null @@ -1,22 +0,0 @@ -package tv.codely.app.mooc.controller.students; - -public class Request { - private String name; - private String surname; - - public String name() { - return name; - } - - public String surname() { - return surname; - } - - public void setSurname(String surname) { - this.surname = surname; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/apps/main/tv/codely/apps/mooc/controller/students/Request.java b/apps/main/tv/codely/apps/mooc/controller/students/Request.java new file mode 100644 index 00000000..1ba67907 --- /dev/null +++ b/apps/main/tv/codely/apps/mooc/controller/students/Request.java @@ -0,0 +1,22 @@ +package tv.codely.apps.mooc.controller.students; + +public class Request { + private String name; + private String email; + + public String name() { + return name; + } + + public String email() { + return email; + } + + public void setEmail(String surname) { + this.email = surname; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java b/apps/main/tv/codely/apps/mooc/controller/students/StudentsPutController.java similarity index 73% rename from app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java rename to apps/main/tv/codely/apps/mooc/controller/students/StudentsPutController.java index 979d46bb..afe34168 100644 --- a/app/main/tv/codely/app/mooc/controller/students/StudentsPutController.java +++ b/apps/main/tv/codely/apps/mooc/controller/students/StudentsPutController.java @@ -1,4 +1,4 @@ -package tv.codely.app.mooc.controller.students; +package tv.codely.apps.mooc.controller.students; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import tv.codely.mooc.students.application.create.StudentCreator; +import tv.codely.mooc.students.application.create.StudentCreatorRequest; @RestController public class StudentsPutController { @@ -19,7 +20,9 @@ public StudentsPutController(StudentCreator creator) { @PutMapping("/students/{id}") public ResponseEntity create(@PathVariable String id, @RequestBody Request request) { - creator.create(id, request.name(), request.surname()); + StudentCreatorRequest studentCreatorRequest = new StudentCreatorRequest(id, request.name(), request.email()); + + creator.create(studentCreatorRequest); return new ResponseEntity<>(HttpStatus.CREATED); } diff --git a/app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java b/apps/test/tv/codely/apps/mooc/controller/students/StudentsPutControllerShould.java similarity index 63% rename from app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java rename to apps/test/tv/codely/apps/mooc/controller/students/StudentsPutControllerShould.java index 9f5a627b..28229b3e 100644 --- a/app/test/tv/codely/app/mooc/controller/students/StudentsPutControllerShould.java +++ b/apps/test/tv/codely/apps/mooc/controller/students/StudentsPutControllerShould.java @@ -1,7 +1,7 @@ -package tv.codely.app.mooc.controller.students; +package tv.codely.apps.mooc.controller.students; import org.junit.jupiter.api.Test; -import tv.codely.app.mooc.controller.RequestTestCase; +import tv.codely.apps.mooc.controller.RequestTestCase; public class StudentsPutControllerShould extends RequestTestCase { @@ -10,7 +10,7 @@ public void create_a_valid_non_existing_student() throws Exception { assertRequestWithBody( "PUT", "/students/1aab45ba-3c7a-4344-8936-78466eca77fa", - "{\"name\": \"The best student\", \"lastname\": \"The best lastname\"}", + "{\"name\": \"The best student\", \"email\": \"example@example.com\"}", 201); } From 818981244284dcdf06414420fec7556ba6365314 Mon Sep 17 00:00:00 2001 From: Sergi-Virgili Date: Mon, 19 Feb 2024 09:07:44 +0100 Subject: [PATCH 5/6] Refactor Student Domain to Value Objets --- .../codely/mooc/students/domain/Student.java | 18 ++++++++--------- .../mooc/students/domain/StudentEmail.java | 9 +++++++++ .../mooc/students/domain/StudentId.java | 9 +++++++++ .../mooc/students/domain/StudentName.java | 9 +++++++++ .../shared/domain/EmailValueObject.java | 20 +++++++++++++++++++ 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 src/mooc/main/tv/codely/mooc/students/domain/StudentEmail.java create mode 100644 src/mooc/main/tv/codely/mooc/students/domain/StudentId.java create mode 100644 src/mooc/main/tv/codely/mooc/students/domain/StudentName.java create mode 100644 src/shared/main/tv/codely/shared/domain/EmailValueObject.java diff --git a/src/mooc/main/tv/codely/mooc/students/domain/Student.java b/src/mooc/main/tv/codely/mooc/students/domain/Student.java index d42c355c..afcf0c5a 100644 --- a/src/mooc/main/tv/codely/mooc/students/domain/Student.java +++ b/src/mooc/main/tv/codely/mooc/students/domain/Student.java @@ -1,26 +1,26 @@ package tv.codely.mooc.students.domain; public class Student { - private String id; - private String name; - private String surname; + private StudentId id; + private StudentName name; + private StudentEmail email; - public Student(String id, String name, String surname) { + public Student(StudentId id, StudentName name, StudentEmail email) { this.id = id; this.name = name; - this.surname = surname; + this.email = email; } - public String id() { + public StudentId id() { return id; } - public String name() { + public StudentName name() { return name; } - public String surname() { - return surname; + public StudentEmail email() { + return email; } @Override diff --git a/src/mooc/main/tv/codely/mooc/students/domain/StudentEmail.java b/src/mooc/main/tv/codely/mooc/students/domain/StudentEmail.java new file mode 100644 index 00000000..65db052b --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/domain/StudentEmail.java @@ -0,0 +1,9 @@ +package tv.codely.mooc.students.domain; + +import tv.codely.shared.domain.EmailValueObject; + +public class StudentEmail extends EmailValueObject { + public StudentEmail(String value) { + super(value); + } +} diff --git a/src/mooc/main/tv/codely/mooc/students/domain/StudentId.java b/src/mooc/main/tv/codely/mooc/students/domain/StudentId.java new file mode 100644 index 00000000..bf4eb9d9 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/domain/StudentId.java @@ -0,0 +1,9 @@ +package tv.codely.mooc.students.domain; + +import tv.codely.shared.domain.Identifier; + +public class StudentId extends Identifier { + public StudentId(String value) { + super(value); + } +} diff --git a/src/mooc/main/tv/codely/mooc/students/domain/StudentName.java b/src/mooc/main/tv/codely/mooc/students/domain/StudentName.java new file mode 100644 index 00000000..8cf59f64 --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/domain/StudentName.java @@ -0,0 +1,9 @@ +package tv.codely.mooc.students.domain; + +import tv.codely.shared.domain.StringValueObject; + +public class StudentName extends StringValueObject { + public StudentName(String value) { + super(value); + } +} diff --git a/src/shared/main/tv/codely/shared/domain/EmailValueObject.java b/src/shared/main/tv/codely/shared/domain/EmailValueObject.java new file mode 100644 index 00000000..a2686eff --- /dev/null +++ b/src/shared/main/tv/codely/shared/domain/EmailValueObject.java @@ -0,0 +1,20 @@ +package tv.codely.shared.domain; + +public abstract class EmailValueObject { + private final String value; + + public EmailValueObject(String value) { + ensureIsValidEmail(value); + this.value = value; + } + + private void ensureIsValidEmail(String value) { + if (!value.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$")) { + throw new IllegalArgumentException(String.format("The email <%s> is not valid", value)); + } + } + + public String value() { + return value; + } +} From f40830150f8e1229f644c2a1393cc1e04a63d122 Mon Sep 17 00:00:00 2001 From: Sergi-Virgili Date: Mon, 19 Feb 2024 09:09:46 +0100 Subject: [PATCH 6/6] Refactor Student Creator to Request input and Value Objects --- .../application/create/StudentCreator.java | 12 ++++++--- .../create/StudentCreatorRequest.java | 25 +++++++++++++++++++ .../students/domain/StudentRepository.java | 2 +- .../InMemoryStudentRepository.java | 7 +++--- .../create/StudentCreatorTest.java | 23 +++++++++-------- .../InMemoryStudentRepositoryTest.java | 19 +++++++++++--- 6 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 src/mooc/main/tv/codely/mooc/students/application/create/StudentCreatorRequest.java diff --git a/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java b/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java index 76f9f5d4..9355a15b 100644 --- a/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java +++ b/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreator.java @@ -1,7 +1,7 @@ package tv.codely.mooc.students.application.create; -import tv.codely.mooc.students.domain.Student; -import tv.codely.mooc.students.domain.StudentRepository; +import tv.codely.mooc.courses.domain.Course; +import tv.codely.mooc.students.domain.*; import tv.codely.shared.domain.Service; @Service @@ -12,7 +12,11 @@ public StudentCreator(StudentRepository repository) { this.repository = repository; } - public void create(String id, String name, String surname) { - repository.save(new Student(id, name, surname)); + public void create(StudentCreatorRequest request) { + StudentId id = new StudentId(request.id()); + StudentName name = new StudentName(request.name()); + StudentEmail email = new StudentEmail(request.email()); + + repository.save(new Student(id, name, email)); } } diff --git a/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreatorRequest.java b/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreatorRequest.java new file mode 100644 index 00000000..f5c1d43b --- /dev/null +++ b/src/mooc/main/tv/codely/mooc/students/application/create/StudentCreatorRequest.java @@ -0,0 +1,25 @@ +package tv.codely.mooc.students.application.create; + +public class StudentCreatorRequest { + private final String id; + private final String name; + private final String email; + + public StudentCreatorRequest(String id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + public String id() { + return id; + } + + public String name() { + return name; + } + + public String email() { + return email; + } +} diff --git a/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java b/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java index 2a6c36ec..e6f3bbe9 100644 --- a/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java +++ b/src/mooc/main/tv/codely/mooc/students/domain/StudentRepository.java @@ -5,5 +5,5 @@ public interface StudentRepository { void save(Student student); - Optional search(String id); + Optional search(StudentId id); } diff --git a/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java b/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java index 56905d04..df93eda2 100644 --- a/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java +++ b/src/mooc/main/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepository.java @@ -1,6 +1,7 @@ package tv.codely.mooc.students.infrastructure.persistence; import tv.codely.mooc.students.domain.Student; +import tv.codely.mooc.students.domain.StudentId; import tv.codely.mooc.students.domain.StudentRepository; import tv.codely.shared.domain.Service; @@ -12,11 +13,11 @@ public class InMemoryStudentRepository implements StudentRepository { private final HashMap students = new HashMap<>(); public void save(Student student) { - students.put(student.id(), student); + students.put(student.id().value(), student); } @Override - public Optional search(String id) { - return Optional.ofNullable(students.get(id)); + public Optional search(StudentId id) { + return Optional.ofNullable(students.get(id.value())); } } diff --git a/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java b/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java index d41e0e44..d9546c79 100644 --- a/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java +++ b/src/mooc/test/tv/codely/mooc/students/application/create/StudentCreatorTest.java @@ -1,8 +1,7 @@ package tv.codely.mooc.students.application.create; import org.junit.jupiter.api.Test; -import tv.codely.mooc.students.domain.Student; -import tv.codely.mooc.students.domain.StudentRepository; +import tv.codely.mooc.students.domain.*; import static org.mockito.Mockito.*; public class StudentCreatorTest { @@ -11,14 +10,18 @@ public class StudentCreatorTest { public void create_a_valid_student() { StudentRepository repository = mock(StudentRepository.class); StudentCreator creator = new StudentCreator(repository); - - String id = "some-id"; - String name = "name"; - String surname = "surname"; - - Student student = new Student(id, name, surname); - - creator.create(student.id(), student.name(), student.surname()); + StudentCreatorRequest creatorRequest = new StudentCreatorRequest( + "decf33ca-81a7-419f-a07a-74f214e928e5", + "name", + "example@example.com"); + + Student student = new Student( + new StudentId(creatorRequest.id()), + new StudentName(creatorRequest.name()), + new StudentEmail(creatorRequest.email()) + ); + + creator.create(creatorRequest); verify(repository, atLeastOnce()).save(student); } diff --git a/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java b/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java index b01d3fc0..b9538900 100644 --- a/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java +++ b/src/mooc/test/tv/codely/mooc/students/infrastructure/persistence/InMemoryStudentRepositoryTest.java @@ -2,8 +2,12 @@ import org.junit.jupiter.api.Test; import tv.codely.mooc.students.domain.Student; +import tv.codely.mooc.students.domain.StudentEmail; +import tv.codely.mooc.students.domain.StudentId; +import tv.codely.mooc.students.domain.StudentName; import java.util.Optional; +import java.util.UUID; import static org.junit.jupiter.api.Assertions.*; @@ -12,7 +16,11 @@ class InMemoryStudentRepositoryTest { @Test void save_a_student() { InMemoryStudentRepository repository = new InMemoryStudentRepository(); - Student student = new Student("id", "name", "surname"); + Student student = new Student( + new StudentId(UUID.randomUUID().toString()), + new StudentName("name"), + new StudentEmail("email@sad.com") + ); repository.save(student); } @@ -20,7 +28,10 @@ void save_a_student() { @Test void return_an_existing_student() { InMemoryStudentRepository repository = new InMemoryStudentRepository(); - Student student = new Student("id", "name", "surname"); + Student student = new Student( + new StudentId(UUID.randomUUID().toString()), + new StudentName("name"), + new StudentEmail("as@d.com")); repository.save(student); @@ -31,6 +42,8 @@ void return_an_existing_student() { void not_return_a_non_existing_student() { InMemoryStudentRepository repository = new InMemoryStudentRepository(); - assertFalse(repository.search("randomId").isPresent()); + assertFalse(repository.search(new StudentId( + UUID.randomUUID().toString() + )).isPresent()); } }