Pārlūkot izejas kodu

CapitaliseCountry

Andrew Grant 5 mēneši atpakaļ
vecāks
revīzija
4c25d7a0f1

+ 14 - 0
src/main/java/scot/carricksoftware/grants/capitalisation/places/countries/CapitaliseCountry.java

@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grants.capitalisation.places.countries;
+
+import scot.carricksoftware.grants.commands.places.countries.CountryCommand;
+
+public interface CapitaliseCountry {
+
+    @SuppressWarnings({"unused", "EmptyMethod"})
+    void capitalise(CountryCommand command);
+}

+ 27 - 0
src/main/java/scot/carricksoftware/grants/capitalisation/places/countries/CapitaliseCountryImpl.java

@@ -0,0 +1,27 @@
+
+
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grants.capitalisation.places.countries;
+
+import org.springframework.stereotype.Component;
+import scot.carricksoftware.grants.capitalisation.CapitaliseString;
+import scot.carricksoftware.grants.commands.places.countries.CountryCommand;
+
+@Component
+public class CapitaliseCountryImpl implements CapitaliseCountry {
+
+   private final CapitaliseString capitaliseString;
+
+    public CapitaliseCountryImpl(CapitaliseString capitaliseString) {
+        this.capitaliseString = capitaliseString;
+    }
+
+    @Override
+    public void capitalise(CountryCommand command) {
+        command.setName(capitaliseString.capitalise(command.getName()));
+    }
+}

+ 14 - 22
src/main/java/scot/carricksoftware/grants/controllers/places/countries/CountryFormControllerImpl.java

@@ -15,13 +15,15 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
+import scot.carricksoftware.grants.capitalisation.places.countries.CapitaliseCountry;
 import scot.carricksoftware.grants.commands.places.countries.CountryCommand;
 import scot.carricksoftware.grants.commands.places.countries.CountryCommandImpl;
 import scot.carricksoftware.grants.constants.AttributeConstants;
 import scot.carricksoftware.grants.constants.MappingConstants;
 import scot.carricksoftware.grants.constants.ViewConstants;
-import scot.carricksoftware.grants.converters.Capitalisation;
+import scot.carricksoftware.grants.converters.places.countries.CountryCommandConverter;
 import scot.carricksoftware.grants.converters.places.countries.CountryCommandConverterImpl;
+import scot.carricksoftware.grants.converters.places.countries.CountryConverter;
 import scot.carricksoftware.grants.converters.places.countries.CountryConverterImpl;
 import scot.carricksoftware.grants.services.places.countries.CountryService;
 import scot.carricksoftware.grants.validators.places.CountryCommandValidator;
@@ -33,22 +35,21 @@ public class CountryFormControllerImpl implements CountryFormController {
     private static final Logger logger = LogManager.getLogger(CountryFormControllerImpl.class);
     private final CountryService countryService;
     @SuppressWarnings({"FieldCanBeLocal", "unused"})
-    private final CountryCommandConverterImpl countryCommandConverter;
-    private final CountryConverterImpl countryConverter;
-    private final Capitalisation capitalisation;
+    private final CountryCommandConverter countryCommandConverter;
+    private final CountryConverter countryConverter;
+    private final CapitaliseCountry capitaliseCountry;
     private final CountryCommandValidator countryCommandValidator;
 
-
-    public CountryFormControllerImpl(CountryService countryService,
-                                     CountryCommandConverterImpl countryCommandConverter,
-                                     CountryConverterImpl countryConverter,
-                                     Capitalisation capitalisation, CountryCommandValidator countryCommandValidator) {
+    public CountryFormControllerImpl(
+            CountryService countryService,
+            CountryCommandConverterImpl countryCommandConverter,
+            CountryConverterImpl countryConverter,
+            CapitaliseCountry capitaliseCountry,
+            CountryCommandValidator countryCommandValidator) {
         this.countryService = countryService;
         this.countryCommandConverter = countryCommandConverter;
-
-
         this.countryConverter = countryConverter;
-        this.capitalisation = capitalisation;
+        this.capitaliseCountry = capitaliseCountry;
         this.countryCommandValidator = countryCommandValidator;
     }
 
@@ -68,30 +69,23 @@ public class CountryFormControllerImpl implements CountryFormController {
         return ViewConstants.COUNTRY_FORM;
     }
 
-
     @Override
     @PostMapping(MappingConstants.COUNTRY)
     public String saveOrUpdate(@Valid @ModelAttribute CountryCommand countryCommand, BindingResult bindingResult, Model model) {
         logger.debug("CountryFormControllerImpl::saveOrUpdate");
-
+        capitaliseCountry.capitalise(countryCommand);
         countryCommandValidator.validate(countryCommand, bindingResult);
 
-
         if (bindingResult.hasErrors()) {
             bindingResult.getAllErrors().forEach(error -> logger.debug(error.getDefaultMessage()));
             return ViewConstants.COUNTRY_FORM;
         }
 
-        cleanUp(countryCommand);
         CountryCommand savedCommand = countryService.saveCountryCommand(countryCommand);
         model.addAttribute(AttributeConstants.COUNTRY_COMMAND, savedCommand);
         return MappingConstants.REDIRECT + MappingConstants.COUNTRY_SHOW.replace("{id}", "" + savedCommand.getId());
     }
 
-    private void cleanUp(CountryCommand countryCommand) {
-        countryCommand.setName(capitalisation.getCapitalisation(countryCommand.getName()));
-    }
-
     @SuppressWarnings("SameReturnValue")
     @GetMapping(MappingConstants.COUNTRY_SHOW)
     public String showById(@PathVariable String id, Model model) {
@@ -100,6 +94,4 @@ public class CountryFormControllerImpl implements CountryFormController {
         model.addAttribute(AttributeConstants.COUNTRY_COMMAND, savedCommand);
         return ViewConstants.COUNTRY_FORM;
     }
-
-
 }

+ 0 - 92
src/test/java/scot/carricksoftware/grants/controllers/places/countries/CountryFormControllerCleansingTest.java

@@ -1,92 +0,0 @@
-/*
- * Copyright (c)  19 Feb 2025, Andrew Grant of Carrick Software .
- * All rights reserved.
- */
-
-package scot.carricksoftware.grants.controllers.places.countries;
-
-
-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.places.countries.CountryCommand;
-import scot.carricksoftware.grants.converters.CapitalisationImpl;
-import scot.carricksoftware.grants.converters.places.countries.CountryCommandConverterImpl;
-import scot.carricksoftware.grants.converters.places.countries.CountryConverterImpl;
-import scot.carricksoftware.grants.services.places.countries.CountryService;
-import scot.carricksoftware.grants.validators.places.CountryCommandValidator;
-
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-
-@ExtendWith(MockitoExtension.class)
-public class CountryFormControllerCleansingTest {
-
-    @SuppressWarnings("unused")
-    private CountryFormControllerImpl countryController;
-
-    @Mock
-    private CountryService countryServiceMock;
-
-    @Mock
-    private CountryCommandConverterImpl countryCommandConverterMock;
-
-    @Mock
-    private CountryConverterImpl countryConverterMock;
-
-    @Mock
-    private CapitalisationImpl capitalisationMock;
-
-    @Mock
-    CountryCommand countryCommandMock;
-
-    @Mock
-    BindingResult bindingResultMock;
-
-    @Mock
-    CountryCommandValidator countryCommandValidatorMock;
-
-    @Mock
-    Model modelMock;
-
-
-    @BeforeEach
-    public void setUp() {
-        countryController = new CountryFormControllerImpl(countryServiceMock,
-                countryCommandConverterMock,
-                countryConverterMock,
-                capitalisationMock,
-                countryCommandValidatorMock);
-    }
-
-
-    @Test
-    public void saveOrUpdateCleansingTest() {
-        String name = "goat";
-        String uName = "Goat";
-        when(countryServiceMock.saveCountryCommand(any())).thenReturn(countryCommandMock);
-        when(countryCommandMock.getName()).thenReturn(name);
-        when(capitalisationMock.getCapitalisation(name)).thenReturn(uName);
-        countryController.saveOrUpdate(countryCommandMock, bindingResultMock, modelMock);
-        verify(countryCommandMock).setName(uName);
-    }
-
-    @Test
-    public void saveOrUpdateValidationTest() {
-        String name = "goat";
-        String uName = "Goat";
-        when(countryServiceMock.saveCountryCommand(any())).thenReturn(countryCommandMock);
-        when(countryCommandMock.getName()).thenReturn(name);
-        when(capitalisationMock.getCapitalisation(name)).thenReturn(uName);
-        countryController.saveOrUpdate(countryCommandMock, bindingResultMock, modelMock);
-        verify(countryCommandValidatorMock).validate(countryCommandMock, bindingResultMock);
-    }
-
-
-}

+ 3 - 17
src/test/java/scot/carricksoftware/grants/controllers/places/countries/CountryFormControllerSaveOrUpdateTest.java

@@ -12,9 +12,9 @@ import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.springframework.ui.Model;
 import org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.capitalisation.places.countries.CapitaliseCountry;
 import scot.carricksoftware.grants.commands.places.countries.CountryCommand;
 import scot.carricksoftware.grants.commands.places.countries.CountryCommandImpl;
-import scot.carricksoftware.grants.converters.Capitalisation;
 import scot.carricksoftware.grants.converters.places.countries.CountryCommandConverterImpl;
 import scot.carricksoftware.grants.converters.places.countries.CountryConverterImpl;
 import scot.carricksoftware.grants.services.places.countries.CountryService;
@@ -22,7 +22,6 @@ import scot.carricksoftware.grants.validators.places.CountryCommandValidator;
 
 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;
 
 
@@ -42,7 +41,7 @@ public class CountryFormControllerSaveOrUpdateTest {
     private CountryConverterImpl countryConverterMock;
 
     @Mock
-    private Capitalisation capitalisationMock;
+    private CapitaliseCountry capitaliseCountryMock;
 
     @Mock
     Model modelMock;
@@ -61,7 +60,7 @@ public class CountryFormControllerSaveOrUpdateTest {
         countryController = new CountryFormControllerImpl(countryServiceMock,
                 countryCommandConverterMock,
                 countryConverterMock,
-                capitalisationMock,
+                capitaliseCountryMock,
                 countryCommandValidatorMock);
         countryCommand = new CountryCommandImpl();
     }
@@ -81,17 +80,4 @@ public class CountryFormControllerSaveOrUpdateTest {
         when(bindingResultMock.hasErrors()).thenReturn(true);
         assertEquals("country/form", countryController.saveOrUpdate(countryCommand, bindingResultMock, modelMock));
     }
-
-    @Test
-    public void CleaningTakesPlaceTest() {
-        CountryCommand countryCommand = new CountryCommandImpl();
-        countryCommand.setId(4L);
-        countryCommand.setName("england");
-        when(bindingResultMock.hasErrors()).thenReturn(false);
-        when(countryServiceMock.saveCountryCommand(any(CountryCommand.class))).thenReturn(countryCommand);
-        countryController.saveOrUpdate(countryCommand, bindingResultMock, modelMock);
-        verify(capitalisationMock).getCapitalisation("england");
-    }
-
-
 }

+ 5 - 4
src/test/java/scot/carricksoftware/grants/controllers/places/countries/CountryFormControllerTest.java

@@ -13,9 +13,9 @@ import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.springframework.ui.Model;
+import scot.carricksoftware.grants.capitalisation.places.countries.CapitaliseCountry;
 import scot.carricksoftware.grants.commands.places.countries.CountryCommand;
 import scot.carricksoftware.grants.constants.AttributeConstants;
-import scot.carricksoftware.grants.converters.Capitalisation;
 import scot.carricksoftware.grants.converters.places.countries.CountryCommandConverterImpl;
 import scot.carricksoftware.grants.converters.places.countries.CountryConverterImpl;
 import scot.carricksoftware.grants.domains.places.Country;
@@ -46,7 +46,7 @@ public class CountryFormControllerTest {
     private CountryConverterImpl countryConverterMock;
 
     @Mock
-    private Capitalisation capitalisationMock;
+    private CapitaliseCountry capitaliseCountryMock;
 
     @Mock
     private Model modelMock;
@@ -57,10 +57,11 @@ public class CountryFormControllerTest {
 
     @BeforeEach
     public void setUp() {
-        countryController = new CountryFormControllerImpl(countryServiceMock,
+        countryController = new CountryFormControllerImpl(
+                countryServiceMock,
                 countryCommandConverterMock,
                 countryConverterMock,
-                capitalisationMock,
+                capitaliseCountryMock,
                 countryCommandValidatorMock);
     }