Quellcode durchsuchen

Organisation Service Tests

Andrew Grant vor 6 Monaten
Ursprung
Commit
bfac45cc78

+ 74 - 0
src/test/java/scot/carricksoftware/grants/services/places/organisations/OrganisationServiceFindTest.java

@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 11/03/2025, 20:24. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.services.places.organisations;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationCommandConverterImpl;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationConverterImpl;
+import scot.carricksoftware.grants.domains.places.Organisation;
+import scot.carricksoftware.grants.repositories.places.OrganisationRepository;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.when;
+import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomLong;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomOrganisation;
+
+@ExtendWith(MockitoExtension.class)
+public class OrganisationServiceFindTest {
+
+    OrganisationService organisationService;
+
+    @Mock
+    OrganisationRepository organisationRepositoryMock;
+
+    @Mock
+    OrganisationConverterImpl organisationConverterMock;
+
+    @Mock
+    OrganisationCommandConverterImpl organisationCommandConverterMock;
+
+    @BeforeEach
+    public void setUp() {
+        organisationService = new OrganisationServiceImpl(organisationRepositoryMock,
+                organisationConverterMock,
+                organisationCommandConverterMock);
+    }
+
+    @Test
+    public void testFindAll() {
+        List<Organisation> organisations = new ArrayList<>();
+        organisations.add(GetRandomOrganisation());
+        when(organisationRepositoryMock.findAll()).thenReturn(organisations);
+        assertEquals(organisations, organisationService.findAll());
+    }
+
+    @Test
+    public void testFindByFoundId() {
+        Long id = GetRandomLong();
+        Organisation organisation = GetRandomOrganisation();
+        Optional<Organisation> organisationOptional = Optional.of(organisation);
+        when(organisationRepositoryMock.findById(id)).thenReturn(organisationOptional);
+        assertEquals(organisation, organisationService.findById(id));
+    }
+
+    @Test
+    public void testFindByNotFoundId() {
+        Long id = GetRandomLong();
+        Optional<Organisation> empty = Optional.empty();
+        when(organisationRepositoryMock.findById(id)).thenReturn(empty);
+        assertNull(organisationService.findById(id));
+    }
+
+}

+ 77 - 0
src/test/java/scot/carricksoftware/grants/services/places/organisations/OrganisationServiceSaveTest.java

@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 11/03/2025, 20:24. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.services.places.organisations;
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.data.domain.Pageable;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommandImpl;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationCommandConverterImpl;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationConverterImpl;
+import scot.carricksoftware.grants.domains.places.Organisation;
+import scot.carricksoftware.grants.repositories.places.OrganisationRepository;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.when;
+import static scot.carricksoftware.grants.GenerateCertificateRandomValues.GetRandomString;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomOrganisation;
+
+
+@ExtendWith(MockitoExtension.class)
+public class OrganisationServiceSaveTest {
+
+    OrganisationService organisationService;
+
+    @Mock
+    OrganisationRepository organisationRepositoryMock;
+
+    @Mock
+    OrganisationConverterImpl organisationConverterImplMock;
+
+    @Mock
+    OrganisationCommandConverterImpl organisationCommandConverterImplMock;
+
+
+    @BeforeEach
+    public void setUp() {
+        organisationService = new OrganisationServiceImpl(organisationRepositoryMock,
+                organisationConverterImplMock,
+                organisationCommandConverterImplMock);
+    }
+
+    @SuppressWarnings("unused")
+    @Mock
+    Pageable pageableMock;
+
+
+    @Test
+    public void saveTest() {
+        Organisation organisation = new Organisation();
+        organisation.setName(GetRandomString());
+
+        when(organisationRepositoryMock.save(organisation)).thenReturn(organisation);
+
+        assertEquals(organisation, organisationService.save(organisation));
+    }
+
+    @Test
+    public void saveOrganisationCommandTest() {
+        Organisation organisation = GetRandomOrganisation();
+        OrganisationCommand organisationCommand = new OrganisationCommandImpl();
+        when(organisationCommandConverterImplMock.convert(organisationCommand)).thenReturn(organisation);
+        when(organisationRepositoryMock.save(organisation)).thenReturn(organisation);
+        when(organisationConverterImplMock.convert((organisation))).thenReturn(organisationCommand);
+
+        assertEquals(organisationCommand, organisationService.saveOrganisationCommand(organisationCommand));
+    }
+
+
+}

+ 86 - 0
src/test/java/scot/carricksoftware/grants/services/places/organisations/OrganisationServiceTest.java

@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 11/03/2025, 20:24. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.services.places.organisations;
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationCommandConverterImpl;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationConverterImpl;
+import scot.carricksoftware.grants.domains.places.Organisation;
+import scot.carricksoftware.grants.repositories.places.OrganisationRepository;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomLong;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomOrganisation;
+
+
+@ExtendWith(MockitoExtension.class)
+public class OrganisationServiceTest {
+
+    OrganisationService organisationService;
+
+    @Mock
+    OrganisationRepository organisationRepositoryMock;
+
+    @Mock
+    OrganisationConverterImpl organisationConverterImplMock;
+
+    @Mock
+    OrganisationCommandConverterImpl organisationCommandConverterImplMock;
+
+
+    @BeforeEach
+    public void setUp() {
+        organisationService = new OrganisationServiceImpl(organisationRepositoryMock,
+                organisationConverterImplMock,
+                organisationCommandConverterImplMock);
+    }
+
+    @SuppressWarnings("unused")
+    @Mock
+    Pageable pageableMock;
+
+    @Mock
+    Page<Organisation> pageMock;
+
+    @Test
+    public void deleteByIdTest() {
+        Long id = GetRandomLong();
+        organisationService.deleteById(id);
+        verify(organisationRepositoryMock).deleteById(id);
+    }
+
+    @Test
+    public void CountTest() {
+        long result = GetRandomLong();
+        when(organisationRepositoryMock.count()).thenReturn(result);
+        assertEquals(result, organisationService.count());
+    }
+
+    @Test
+    public void getPagedCountriesTest() {
+        List<Organisation> result = new ArrayList<>();
+        Organisation organisation = GetRandomOrganisation();
+        result.add(organisation);
+        when(pageMock.getContent()).thenReturn(result);
+        when(organisationRepositoryMock.findAll(any(Pageable.class))).thenReturn(pageMock);
+        assertEquals(result, organisationService.getPagedOrganisations(0));
+    }
+
+
+}