Procházet zdrojové kódy

Person Service (2)

Andrew Grant před 4 měsíci
rodič
revize
73c8972a05

+ 19 - 0
src/main/java/scot/carricksoftware/grantswriter/domains/people/Person.java

@@ -11,5 +11,24 @@ import scot.carricksoftware.grantswriter.BaseEntity;
 @Entity
 public class Person extends BaseEntity {
 
+    String firstName;
+    String lastName;
 
+    @SuppressWarnings("unused")
+    public String getFirstName() {
+        return firstName;
+    }
+
+    public void setFirstName(String firstName) {
+        this.firstName = firstName;
+    }
+
+    @SuppressWarnings("unused")
+    public String getLastName() {
+        return lastName;
+    }
+
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
 }

+ 1 - 2
src/main/java/scot/carricksoftware/grantswriter/repositories/people/ReadOnlyRepository.java

@@ -9,10 +9,9 @@ import org.springframework.data.repository.NoRepositoryBean;
 import org.springframework.data.repository.Repository;
 
 import java.util.List;
-import java.util.Optional;
 
 @NoRepositoryBean
 public interface ReadOnlyRepository<T, ID> extends Repository<T, ID> {
-    Optional<T> findById(ID id);
+
     List<T> findAll();
 }

+ 29 - 0
src/test/java/scot/carricksoftware/grantswriter/GenerateRandomPeopleValues.java

@@ -0,0 +1,29 @@
+/*
+ * Copyright (c)  04 Feb 2025, Andrew Grant of Carrick Software .
+ * All rights reserved.
+ */
+
+package scot.carricksoftware.grantswriter;
+
+import org.springframework.stereotype.Component;
+import scot.carricksoftware.grantswriter.domains.people.Person;
+
+import static scot.carricksoftware.grantswriter.GenerateRandomNumberValues.GetRandomLong;
+import static scot.carricksoftware.grantswriter.GenerateRandomNumberValues.GetRandomString;
+
+
+@SuppressWarnings("unused")
+@Component
+public class GenerateRandomPeopleValues {
+
+    @SuppressWarnings("unused")
+    public static Person GetRandomPerson() {
+        Person person = new Person();
+        person.setId(GetRandomLong());
+        person.setFirstName(GetRandomString());
+        person.setLastName(GetRandomString());
+        return person;
+    }
+
+
+}

+ 17 - 2
src/test/java/scot/carricksoftware/grantswriter/services/people/PersonServiceTest.java

@@ -10,9 +10,15 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
+import scot.carricksoftware.grantswriter.domains.people.Person;
 import scot.carricksoftware.grantswriter.repositories.people.PersonRepository;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.when;
+import static scot.carricksoftware.grantswriter.GenerateRandomPeopleValues.GetRandomPerson;
 
 @ExtendWith(SpringExtension.class)
 class PersonServiceTest {
@@ -20,15 +26,24 @@ class PersonServiceTest {
     private PersonService service;
 
     @Mock
-    private PersonRepository personRepository;
+    private PersonRepository personRepositoryMock;
 
     @BeforeEach
     void setUp() {
-        service = new PersonServiceImpl(personRepository);
+        service = new PersonServiceImpl(personRepositoryMock);
     }
 
     @Test
     void constructorTest() {
         assertNotNull(service);
     }
+
+    @Test
+    public void testFindAll() {
+        List<Person> people = new ArrayList<>();
+        people.add(GetRandomPerson());
+        when(personRepositoryMock.findAll()).thenReturn(people);
+        assertEquals(people, service.findAll());
+    }
+
 }