Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
chore: use constructor injection (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
annibalsilva authored Feb 28, 2023
1 parent 2d3cb93 commit ca830b4
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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",
Expand All @@ -93,8 +97,9 @@ public List<VegetationCode> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
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}. */
@Repository
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<VegetationCode> findByCode(String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ca830b4

Please sign in to comment.