Parcourir la source

Document Text Controller Tests

Andrew Grant il y a 6 mois
Parent
commit
7448a8f0c9

+ 1 - 1
src/test/java/scot/carricksoftware/grants/controllers/images/images/DocumentTextListControllerTest.java → src/test/java/scot/carricksoftware/grants/controllers/images/images/ImageListControllerTest.java

@@ -26,7 +26,7 @@ import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomLo
 
 
 @ExtendWith(MockitoExtension.class)
-public class DocumentTextListControllerTest {
+public class ImageListControllerTest {
 
     private ImageListControllerImpl controller;
 

+ 79 - 0
src/test/java/scot/carricksoftware/grants/controllers/text/documenttext/DocumentTextControllerSaveOrUpdateTest.java

@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 29/03/2025, 13:55. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.text.documenttext;
+
+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.ui.Model;
+import org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.commands.text.DocumentTextCommand;
+import scot.carricksoftware.grants.commands.text.DocumentTextCommandImpl;
+import scot.carricksoftware.grants.converters.text.documenttext.DocumentTextCommandConverterImpl;
+import scot.carricksoftware.grants.converters.text.documenttext.DocumentTextConverterImpl;
+import scot.carricksoftware.grants.services.text.documenttext.DocumentTextService;
+import scot.carricksoftware.grants.validators.text.DocumentTextCommandValidator;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+
+@ExtendWith(MockitoExtension.class)
+public class DocumentTextControllerSaveOrUpdateTest {
+
+    @SuppressWarnings("unused")
+    private DocumentTextFormControllerImpl documentTextController;
+
+    @Mock
+    private DocumentTextService documentTextServiceMock;
+
+    @Mock
+    private DocumentTextCommandConverterImpl documentTextCommandConverterMock;
+
+    @Mock
+    private DocumentTextConverterImpl documentTextConverterMock;
+
+    @Mock
+    private Model modelMock;
+
+    @Mock
+    private BindingResult bindingResultMock;
+
+    @Mock
+    private DocumentTextCommandValidator documentTextCommandValidatorMock;
+
+    private DocumentTextCommand documentTextCommand;
+
+
+    @BeforeEach
+    public void setUp() {
+        documentTextController = new DocumentTextFormControllerImpl(documentTextServiceMock,
+                documentTextCommandConverterMock,
+                documentTextConverterMock,
+                documentTextCommandValidatorMock);
+        documentTextCommand = new DocumentTextCommandImpl();
+    }
+
+    @Test
+    public void saveOrUpdateNoErrorsTest() {
+        Long id = 4L;
+        documentTextCommand.setId(id);
+        when(documentTextServiceMock.saveDocumentTextCommand(any(DocumentTextCommand.class))).thenReturn(documentTextCommand);
+        assertEquals("redirect:/documentText/4/show", documentTextController.saveOrUpdate(documentTextCommand, bindingResultMock, modelMock));
+    }
+
+    @Test
+    public void saveOrUpdateErrorsTest() {
+        Long id = 4L;
+        documentTextCommand.setId(id);
+        when(bindingResultMock.hasErrors()).thenReturn(true);
+        assertEquals("text/documentText/form", documentTextController.saveOrUpdate(documentTextCommand, bindingResultMock, modelMock));
+    }
+
+}

+ 94 - 0
src/test/java/scot/carricksoftware/grants/controllers/text/documenttext/DocumentTextFormControllerTest.java

@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 29/03/2025, 13:55. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.text.documenttext;
+
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.ui.Model;
+import scot.carricksoftware.grants.commands.text.DocumentTextCommand;
+import scot.carricksoftware.grants.constants.TextAttributeConstants;
+import scot.carricksoftware.grants.converters.text.documenttext.DocumentTextCommandConverterImpl;
+import scot.carricksoftware.grants.converters.text.documenttext.DocumentTextConverterImpl;
+import scot.carricksoftware.grants.domains.text.DocumentText;
+import scot.carricksoftware.grants.services.text.documenttext.DocumentTextService;
+import scot.carricksoftware.grants.validators.text.DocumentTextCommandValidator;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomLong;
+import static scot.carricksoftware.grants.GenerateRandomTextValues.GetRandomDocumentText;
+import static scot.carricksoftware.grants.GenerateRandomTextValues.GetRandomDocumentTextCommand;
+
+
+@ExtendWith(MockitoExtension.class)
+public class DocumentTextFormControllerTest {
+
+    @SuppressWarnings("unused")
+    private DocumentTextFormControllerImpl documentTextController;
+
+    @Mock
+    private DocumentTextService documentTextServiceMock;
+
+    @Mock
+    private DocumentTextCommandConverterImpl documentTextCommandConverterMock;
+
+    @Mock
+    private DocumentTextConverterImpl documentTextConverterMock;
+
+    @Mock
+    private Model modelMock;
+
+    @Mock
+    private DocumentTextCommandValidator documentTextCommandValidatorMock;
+
+
+    @BeforeEach
+    public void setUp() {
+        documentTextController = new DocumentTextFormControllerImpl(documentTextServiceMock,
+                documentTextCommandConverterMock,
+                documentTextConverterMock,
+                documentTextCommandValidatorMock);
+    }
+
+    @Test
+    public void getNewDocumentTextTest() {
+        ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+        ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
+        assertEquals("text/documentText/form", documentTextController.getNewDocumentText(modelMock));
+        verify(modelMock).addAttribute(stringCaptor.capture(), objectCaptor.capture());
+        assertEquals("documentTextCommand", stringCaptor.getValue());
+        assertEquals("DocumentTextCommandImpl", objectCaptor.getValue().getClass().getSimpleName());
+    }
+
+    @Test
+    public void documentTextEditTestEditTest() {
+        Long id = GetRandomLong();
+        DocumentText documentText = GetRandomDocumentText();
+        when(documentTextServiceMock.findById(id)).thenReturn(documentText);
+
+        assertEquals("text/documentText/form", documentTextController.documentTextEdit(id + "", modelMock));
+        verify(modelMock).addAttribute(TextAttributeConstants.DOCUMENT_TEXT_COMMAND, documentText);
+    }
+
+    @Test
+    public void showByIdTest() {
+        Long id = GetRandomLong();
+        DocumentText documentText = GetRandomDocumentText();
+        DocumentTextCommand documentTextCommand = GetRandomDocumentTextCommand();
+
+        when(documentTextServiceMock.findById(id)).thenReturn(documentText);
+        when(documentTextConverterMock.convert(documentText)).thenReturn(documentTextCommand);
+        assertEquals("text/documentText/form", documentTextController.showById(id + "", modelMock));
+        verify(modelMock).addAttribute(TextAttributeConstants.DOCUMENT_TEXT_COMMAND, documentTextCommand);
+    }
+
+}

+ 73 - 0
src/test/java/scot/carricksoftware/grants/controllers/text/documenttext/DocumentTextFormControllerValidationTest.java

@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 29/03/2025, 13:55. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.text.documenttext;
+
+
+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.ui.Model;
+import org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.commands.text.DocumentTextCommand;
+import scot.carricksoftware.grants.converters.text.documenttext.DocumentTextCommandConverterImpl;
+import scot.carricksoftware.grants.converters.text.documenttext.DocumentTextConverterImpl;
+import scot.carricksoftware.grants.services.text.documenttext.DocumentTextService;
+import scot.carricksoftware.grants.validators.text.DocumentTextCommandValidator;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+
+@ExtendWith(MockitoExtension.class)
+public class DocumentTextFormControllerValidationTest {
+
+    @SuppressWarnings("unused")
+    private DocumentTextFormControllerImpl documentTextController;
+
+    @Mock
+    private DocumentTextService documentTextServiceMock;
+
+    @Mock
+    private DocumentTextCommandConverterImpl documentTextCommandConverterMock;
+
+    @Mock
+    private DocumentTextConverterImpl documentTextConverterMock;
+
+
+    @Mock
+    private DocumentTextCommand documentTextCommandMock;
+
+    @Mock
+    private BindingResult bindingResultMock;
+
+    @Mock
+    private DocumentTextCommandValidator documentTextCommandValidatorMock;
+
+    @Mock
+    private Model modelMock;
+
+
+    @BeforeEach
+    public void setUp() {
+        documentTextController = new DocumentTextFormControllerImpl(documentTextServiceMock,
+                documentTextCommandConverterMock,
+                documentTextConverterMock,
+                documentTextCommandValidatorMock);
+    }
+
+
+    @Test
+    public void saveOrUpdateValidationTest() {
+        when(documentTextServiceMock.saveDocumentTextCommand(any())).thenReturn(documentTextCommandMock);
+        documentTextController.saveOrUpdate(documentTextCommandMock, bindingResultMock, modelMock);
+        verify(documentTextCommandValidatorMock).validate(documentTextCommandMock, bindingResultMock);
+    }
+
+
+}

+ 123 - 0
src/test/java/scot/carricksoftware/grants/controllers/text/documenttext/DocumentTextListControllerTest.java

@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 29/03/2025, 13:55. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.text.documenttext;
+
+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.ui.Model;
+import scot.carricksoftware.grants.constants.ApplicationConstants;
+import scot.carricksoftware.grants.controllers.ControllerHelper;
+import scot.carricksoftware.grants.domains.text.DocumentText;
+import scot.carricksoftware.grants.services.text.documenttext.DocumentTextService;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.*;
+import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomLong;
+import static scot.carricksoftware.grants.GenerateRandomTextValues.GetRandomDocumentText;
+
+
+@ExtendWith(MockitoExtension.class)
+public class DocumentTextListControllerTest {
+
+    private DocumentTextListControllerImpl controller;
+
+    @Mock
+    private ControllerHelper controllerHelperMock;
+
+    @Mock
+    private DocumentTextService documentTextServiceMock;
+
+    @Mock
+    private Model modelMock;
+
+    @Mock
+    List<DocumentText> documentTextListMock;
+
+    @BeforeEach
+    public void setUp() {
+        controller = new DocumentTextListControllerImpl(controllerHelperMock, documentTextServiceMock);
+    }
+
+    @Test
+    public void getListPageTest() {
+        when(documentTextServiceMock.getPagedDocumentTexts(0)).thenReturn(documentTextListMock);
+        assertEquals("text/documentText/list", controller.getListPage(modelMock));
+        verify(modelMock).addAttribute("documentTexts", documentTextListMock);
+        verify(controllerHelperMock).addAttributes(modelMock);
+    }
+
+    @Test
+    public void getLastPageTest() {
+        int page = 25;
+        int count = page * ApplicationConstants.DEFAULT_PAGE_SIZE;
+        when(documentTextServiceMock.count()).thenReturn((long) count);
+        controller.getLastPage(modelMock);
+        controller.getPreviousPage(modelMock);
+        assertEquals("text/documentText/list", controller.getLastPage(modelMock));
+        verify(documentTextServiceMock, times(2)).getPagedDocumentTexts(page);
+    }
+
+    @Test
+    public void placeDeleteTest() {
+        Long id = GetRandomLong();
+        assertEquals("redirect:/documentTexts", controller.DocumentTextDelete(Long.toString(id)));
+        verify(documentTextServiceMock).deleteById(id);
+    }
+
+    @Test
+    public void getFirstPlaceTest() {
+        List<DocumentText> documentTextList = new ArrayList<>();
+        documentTextList.add(GetRandomDocumentText());
+        when(documentTextServiceMock.getPagedDocumentTexts(0)).thenReturn(documentTextList);
+
+        assertEquals("text/documentText/list", controller.getFirstPage(modelMock));
+        assertEquals(0, controller.getPageNumber());
+        verify(modelMock).addAttribute("documentTexts", documentTextList);
+    }
+
+    @Test
+    public void getNextPlaceTest() {
+        List<DocumentText> documentTextList = new ArrayList<>();
+        documentTextList.add(GetRandomDocumentText());
+        when(documentTextServiceMock.getPagedDocumentTexts(0)).thenReturn(documentTextList);
+
+        controller.getFirstPage(modelMock);
+        assertEquals("text/documentText/list", controller.getNextPage(modelMock));
+        assertEquals(1, controller.getPageNumber());
+        verify(modelMock).addAttribute("documentTexts", documentTextList);
+
+    }
+
+    @Test
+    public void getPreviousFromFirstTest() {
+        List<DocumentText> documentTextList = new ArrayList<>();
+        documentTextList.add(GetRandomDocumentText());
+        when(documentTextServiceMock.getPagedDocumentTexts(0)).thenReturn(documentTextList);
+
+        controller.getFirstPage(modelMock);
+        assertEquals("text/documentText/list", controller.getPreviousPage(modelMock));
+        assertEquals(0, controller.getPageNumber());
+        verify(modelMock, times(2)).addAttribute("documentTexts", documentTextList);
+    }
+
+    @Test
+    public void getPreviousFromLastTest() {
+        int page = 25;
+        int count = page * ApplicationConstants.DEFAULT_PAGE_SIZE;
+        when(documentTextServiceMock.count()).thenReturn((long) count);
+        controller.getLastPage(modelMock);
+        assertEquals("text/documentText/list", controller.getPreviousPage(modelMock));
+        assertEquals(24, controller.getPageNumber());
+    }
+
+
+}