From ca830b4f832aea82075f16a366f21ea6fdb9f907 Mon Sep 17 00:00:00 2001 From: Annibal Silva <105941480+annibalsilva@users.noreply.github.com> Date: Tue, 28 Feb 2023 11:06:37 -0300 Subject: [PATCH] chore: use constructor injection (#80) --- .github/badges/jacoco.svg | 2 +- docker-compose.yml | 1 + .../endpoint/VegetationCodeEndpoint.java | 17 +++++++++++------ .../VegetationCodeRepositoryImpl.java | 8 ++++++-- .../endpoint/CheckEndpointTest.java | 7 +++++-- .../endpoint/UserEndpointTest.java | 8 ++++---- .../endpoint/VegetationCodeEndpointTest.java | 7 +++++-- .../VegetationCodeRepositoryTest.java | 7 ++++++- 8 files changed, 39 insertions(+), 18 deletions(-) diff --git a/.github/badges/jacoco.svg b/.github/badges/jacoco.svg index 0a979ea..0591fe9 100644 --- a/.github/badges/jacoco.svg +++ b/.github/badges/jacoco.svg @@ -1 +1 @@ -coverage91.1% \ No newline at end of file +coverage91.2% \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 7f81a4f..f9fdeee 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,6 +14,7 @@ services: DATABASE_USER: ${DATABASE_USER} DATABASE_PASSWORD: ${DATABASE_PASSWORD} KEYCLOAK_REALM_URL: ${KEYCLOAK_REALM_URL} + JAVA_OPTS: ${JAVA_OPTS} healthcheck: test: ["CMD", "java", "HealthCheck"] interval: 1m30s diff --git a/src/main/java/ca/bc/gov/backendstartapi/endpoint/VegetationCodeEndpoint.java b/src/main/java/ca/bc/gov/backendstartapi/endpoint/VegetationCodeEndpoint.java index 77cb4b6..e47f8c5 100644 --- a/src/main/java/ca/bc/gov/backendstartapi/endpoint/VegetationCodeEndpoint.java +++ b/src/main/java/ca/bc/gov/backendstartapi/endpoint/VegetationCodeEndpoint.java @@ -13,7 +13,6 @@ import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.util.MimeTypeUtils; import org.springframework.validation.annotation.Validated; @@ -34,7 +33,11 @@ @Tag(name = "vegetationCode", description = "Codes describing various vegetation species") public class VegetationCodeEndpoint { - @Autowired private VegetationCodeRepository vegetationCodeRepository; + private final VegetationCodeRepository vegetationCodeRepository; + + VegetationCodeEndpoint(VegetationCodeRepository vegetationCodeRepository) { + this.vegetationCodeRepository = vegetationCodeRepository; + } /** * Fetch information about a single vegetation code. @@ -79,8 +82,9 @@ public VegetationCode findByCode( @Operation( summary = "Search for valid vegetation codes by their identifier or description", description = - "Search for valid vegetation codes (ones which `effectiveDate` ≤ today < `expiryDate`) " - + "with identifier or description matching `search`.", + """ + Search for valid vegetation codes (ones which `effectiveDate` ≤ today < `expiryDate`) + with identifier or description matching `search`.""", responses = { @ApiResponse( responseCode = "200", @@ -93,8 +97,9 @@ public List findEffectiveByCodeOrDescription( @RequestParam(name = "search", defaultValue = "") @Parameter( description = - "A string to be matched against the codes' identifier or description. Not " - + "providing a value matches everything.") + """ + A string to be matched against the codes' identifier or description. Not + providing a value matches everything.""") String search, @Valid PaginationParameters paginationParameters) { return vegetationCodeRepository.findValidByCodeOrDescription( diff --git a/src/main/java/ca/bc/gov/backendstartapi/implementation/VegetationCodeRepositoryImpl.java b/src/main/java/ca/bc/gov/backendstartapi/implementation/VegetationCodeRepositoryImpl.java index e5c2d7a..af2af45 100644 --- a/src/main/java/ca/bc/gov/backendstartapi/implementation/VegetationCodeRepositoryImpl.java +++ b/src/main/java/ca/bc/gov/backendstartapi/implementation/VegetationCodeRepositoryImpl.java @@ -7,7 +7,6 @@ import jakarta.persistence.PersistenceContext; import java.util.List; import java.util.Optional; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** An implementation of {@link VegetationCodeRepository}. */ @@ -15,7 +14,12 @@ public class VegetationCodeRepositoryImpl implements VegetationCodeRepository { @PersistenceContext private EntityManager em; - @Autowired private VegetationCodeJpaRepository jpaRepository; + + private final VegetationCodeJpaRepository jpaRepository; + + VegetationCodeRepositoryImpl(VegetationCodeJpaRepository jpaRepository) { + this.jpaRepository = jpaRepository; + } @Override public Optional findByCode(String code) { diff --git a/src/test/java/ca/bc/gov/backendstartapi/endpoint/CheckEndpointTest.java b/src/test/java/ca/bc/gov/backendstartapi/endpoint/CheckEndpointTest.java index 657ad51..ba5e04b 100644 --- a/src/test/java/ca/bc/gov/backendstartapi/endpoint/CheckEndpointTest.java +++ b/src/test/java/ca/bc/gov/backendstartapi/endpoint/CheckEndpointTest.java @@ -10,7 +10,6 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.http.MediaType; @@ -26,7 +25,11 @@ class CheckEndpointTest { private MockMvc mockMvc; - @Autowired private WebApplicationContext webApplicationContext; + private final WebApplicationContext webApplicationContext; + + CheckEndpointTest(WebApplicationContext webApplicationContext) { + this.webApplicationContext = webApplicationContext; + } @BeforeEach public void setup() { diff --git a/src/test/java/ca/bc/gov/backendstartapi/endpoint/UserEndpointTest.java b/src/test/java/ca/bc/gov/backendstartapi/endpoint/UserEndpointTest.java index 960cebf..1d55088 100644 --- a/src/test/java/ca/bc/gov/backendstartapi/endpoint/UserEndpointTest.java +++ b/src/test/java/ca/bc/gov/backendstartapi/endpoint/UserEndpointTest.java @@ -11,7 +11,6 @@ import ca.bc.gov.backendstartapi.dto.UserDto; import ca.bc.gov.backendstartapi.exception.UserExistsException; import ca.bc.gov.backendstartapi.exception.UserNotFoundException; -import ca.bc.gov.backendstartapi.repository.UserRepository; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; @@ -25,7 +24,6 @@ import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.http.HttpStatus; @@ -48,9 +46,11 @@ class UserEndpointTest { private MockMvc mockMvc; - @Autowired UserRepository userRepository; + private final WebApplicationContext webApplicationContext; - @Autowired private WebApplicationContext webApplicationContext; + UserEndpointTest(WebApplicationContext webApplicationContext) { + this.webApplicationContext = webApplicationContext; + } @BeforeEach public void setup() { diff --git a/src/test/java/ca/bc/gov/backendstartapi/endpoint/VegetationCodeEndpointTest.java b/src/test/java/ca/bc/gov/backendstartapi/endpoint/VegetationCodeEndpointTest.java index 416a1d9..363eff3 100644 --- a/src/test/java/ca/bc/gov/backendstartapi/endpoint/VegetationCodeEndpointTest.java +++ b/src/test/java/ca/bc/gov/backendstartapi/endpoint/VegetationCodeEndpointTest.java @@ -16,7 +16,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.mock.mockito.MockBean; @@ -34,7 +33,11 @@ class VegetationCodeEndpointTest { @MockBean private VegetationCodeRepositoryImpl vegetationCodeRepository; - @Autowired private WebApplicationContext webApplicationContext; + private final WebApplicationContext webApplicationContext; + + VegetationCodeEndpointTest(WebApplicationContext webApplicationContext) { + this.webApplicationContext = webApplicationContext; + } @BeforeEach public void setup() { diff --git a/src/test/java/ca/bc/gov/backendstartapi/repository/VegetationCodeRepositoryTest.java b/src/test/java/ca/bc/gov/backendstartapi/repository/VegetationCodeRepositoryTest.java index f9f0936..6723de8 100644 --- a/src/test/java/ca/bc/gov/backendstartapi/repository/VegetationCodeRepositoryTest.java +++ b/src/test/java/ca/bc/gov/backendstartapi/repository/VegetationCodeRepositoryTest.java @@ -16,7 +16,12 @@ @Transactional class VegetationCodeRepositoryTest { - @Autowired private VegetationCodeRepository vegetationCodeRepository; + private final VegetationCodeRepository vegetationCodeRepository; + + @Autowired + VegetationCodeRepositoryTest(VegetationCodeRepository vegetationCodeRepository) { + this.vegetationCodeRepository = vegetationCodeRepository; + } @Test void findById_valid() {