Skip to content

Commit

Permalink
fix(configuration): fixed configuration issues (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj authored Oct 29, 2024
1 parent 9b4db98 commit 2cded86
Show file tree
Hide file tree
Showing 18 changed files with 295 additions and 421 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public class ExternalApiConfiguration {
/**
* Creates a RestClient bean for the Forest Client API.
*
* @param providersConfiguration the configuration properties for providers
* @param silvaConfiguration the configuration properties for providers
* @return the configured RestClient instance for the Forest Client API
*/
@Bean
public RestClient forestClientApi(ProvidersConfiguration providersConfiguration) {
public RestClient forestClientApi(SilvaConfiguration silvaConfiguration) {
return RestClient
.builder()
.baseUrl(providersConfiguration.getForestClientBaseUri())
.defaultHeader("X-API-KEY", providersConfiguration.getForestClientApiKey())
.baseUrl(silvaConfiguration.getForestClientApi().getAddress())
.defaultHeader("X-API-KEY", silvaConfiguration.getForestClientApi().getKey())
.defaultHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.build();
}
Expand All @@ -34,10 +34,10 @@ public RestClient forestClientApi(ProvidersConfiguration providersConfiguration)
* @return the configured RestClient instance for the Open Maps API
*/
@Bean
public RestClient openMapsApi() {
public RestClient openMapsApi(SilvaConfiguration silvaConfiguration) {
return RestClient
.builder()
.baseUrl("https://openmaps.gov.bc.ca/geo/ows")
.baseUrl(silvaConfiguration.getOpenMaps().getAddress())
.defaultHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.build();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors(Customizer.withDefaults())
.csrf(
customize ->
customize.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()))
customize.csrfTokenRepository(new CookieCsrfTokenRepository()))
.authorizeHttpRequests(
customize ->
customize
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ca.bc.gov.restapi.results.common.configuration;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
* This class contains configurations for all external APIs like address and keys.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@With
@Builder
@Configuration
@Component
@ConfigurationProperties("ca.bc.gov.nrs")
public class SilvaConfiguration {

private List<String> dashboardJobUsers;
private List<String> wmsWhitelist;
private List<String> orgUnits;

@NestedConfigurationProperty
private ExternalApiAddress forestClientApi;
@NestedConfigurationProperty
private ExternalApiAddress openMaps;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ExternalApiAddress {

private String address;
private String key;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ca.bc.gov.restapi.results.common.endpoint;

import ca.bc.gov.restapi.results.common.configuration.SilvaConfiguration;
import ca.bc.gov.restapi.results.common.dto.WmsLayersWhitelistUserDto;
import java.util.List;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Value;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -13,10 +13,10 @@
*/
@RestController
@RequestMapping("/api/secrets")
@RequiredArgsConstructor
public class SecretsServiceEndpoint {

@Value("${nr.results.config.wms-layers.whitelist}")
private String[] wmsLayersWhitelistUsers;
private final SilvaConfiguration silvaConfiguration;

/**
* Get all users allowed to see the WMS layers information.
Expand All @@ -25,6 +25,10 @@ public class SecretsServiceEndpoint {
*/
@GetMapping("/wms-layers-whitelist")
public List<WmsLayersWhitelistUserDto> getWmsLayersWhitelistUsers() {
return Stream.of(wmsLayersWhitelistUsers).map(WmsLayersWhitelistUserDto::new).toList();
return silvaConfiguration
.getWmsWhitelist()
.stream()
.map(WmsLayersWhitelistUserDto::new)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ca.bc.gov.restapi.results.oracle.service.OpeningService;
import ca.bc.gov.restapi.results.oracle.service.OrgUnitService;
import jakarta.validation.Valid;
import java.util.Arrays;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -150,6 +151,6 @@ public List<OrgUnitEntity> getOpeningOrgUnits() {
public List<OrgUnitEntity> getOpeningOrgUnitsByCode(
@RequestParam(value = "orgUnitCodes", required = true)
String[] codes) {
return orgUnitService.findAllOrgUnitsByCode(codes);
return orgUnitService.findAllOrgUnitsByCode(Arrays.asList(codes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,29 @@
import ca.bc.gov.restapi.results.oracle.enums.OpeningStatusEnum;
import ca.bc.gov.restapi.results.oracle.util.PaginationUtil;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Query;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/** This class represents the Openings Search repository database access. */
@Slf4j
@Component
@AllArgsConstructor
public class OpeningSearchRepository {

private final EntityManager em;

public OpeningSearchRepository(@Qualifier("oracleEntityManagerFactory") EntityManagerFactory emf) {
this.em = emf.createEntityManager();
}

/**
* Search Opening with filters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
/** This interface provides methods to get, save, and manage org unit data in the database. */
public interface OrgUnitRepository extends JpaRepository<OrgUnitEntity, Long> {

List<OrgUnitEntity> findAllByOrgUnitCodeIn(String[] orgUnitCodes);
List<OrgUnitEntity> findAllByOrgUnitCodeIn(List<String> orgUnitCodes);
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package ca.bc.gov.restapi.results.oracle.service;

import ca.bc.gov.restapi.results.common.configuration.SilvaConfiguration;
import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity;
import ca.bc.gov.restapi.results.oracle.repository.OrgUnitRepository;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/** This class contains methods to handle Org Units. */
/**
* This class contains methods to handle Org Units.
*/
@Slf4j
@Service
@AllArgsConstructor
@RequiredArgsConstructor
public class OrgUnitService {

private final OrgUnitRepository orgUnitRepository;

@Value("${nr.results.config.opening-search.org-units}")
private String[] orgUnitsFromProps;
private final SilvaConfiguration silvaConfiguration;

/**
* Find all Org Units for the Openings Search.
Expand All @@ -29,12 +29,15 @@ public class OrgUnitService {
public List<OrgUnitEntity> findAllOrgUnits() {
log.info("Getting all org units for the search openings");

if (Objects.isNull(orgUnitsFromProps) || orgUnitsFromProps.length == 0) {
if (Objects.isNull(silvaConfiguration.getOrgUnits())
|| silvaConfiguration.getOrgUnits().isEmpty()
) {
log.warn("No Org Units from the properties file.");
return List.of();
}

List<OrgUnitEntity> orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(orgUnitsFromProps);
List<OrgUnitEntity> orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(
silvaConfiguration.getOrgUnits());

log.info("Found {} org units by codes", orgUnits.size());
return orgUnits;
Expand All @@ -46,8 +49,8 @@ public List<OrgUnitEntity> findAllOrgUnits() {
* @param orgUnitCodes Org Unit codes to search for.
* @return List of {@link OrgUnitEntity} with found categories.
*/
public List<OrgUnitEntity> findAllOrgUnitsByCode(String[] orgUnitCodes) {
log.info("Getting all org units by codes: {}", Arrays.toString(orgUnitCodes));
public List<OrgUnitEntity> findAllOrgUnitsByCode(List<String> orgUnitCodes) {
log.info("Getting all org units by codes: {}", orgUnitCodes);

List<OrgUnitEntity> orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(orgUnitCodes);
log.info("Found {} org units by codes", orgUnits.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
@Getter
@Setter
@Configuration
@ConfigurationProperties("nr.results")
@ConfigurationProperties("ca.bc.gov.nrs")
public class DashboardUserManagerConfiguration {

private String[] dashboardJobUsers;
private List<String> dashboardJobUsers;

/**
* Gets the users in a list format.
Expand All @@ -27,6 +27,6 @@ public List<String> getUserList() {
if (Objects.isNull(dashboardJobUsers)) {
return List.of();
}
return List.of(dashboardJobUsers);
return dashboardJobUsers;
}
}
29 changes: 8 additions & 21 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ management:
health:
show-details: always

# Native Cloud
springdoc:
enable-native-support: true

# https://github.com/quarkusio/quarkus/wiki/Migration-Guide-3.2
quarkus:
native:
Expand All @@ -93,28 +89,19 @@ ca:
bc:
gov:
nrs:
dashboard-job-users: ${DASHBOARD_JOB_IDIR_USERS:NONE}
wms-whitelist: ${WMS_LAYERS_WHITELIST_USERS:NONE}
org-units: ${OPENING_SEARCH_ORG_UNITS:DCK,DSQ,DVA,DKM,DSC,DFN,DSI,DCR,DMK,DQC,DKA,DCS,DOS,DSE,DCC,DMH,DQU,DNI,DND,DRM,DPG,DSS,DPC}
forest-client-api:
address: ${FORESTCLIENTAPI_ADDRESS:https://nr-forest-client-api-prod.api.gov.bc.ca/api}
key: ${FORESTCLIENTAPI_KEY:placeholder-api-key}
open-maps:
address: ${OPENMAPS_URL:https://openmaps.gov.bc.ca/geo/ows}
oracle:
keystore: ${ORACLEDB_KEYSTORE:jssecacerts-path}
secret: ${ORACLEDB_SECRET:changeit}
host: ${DATABASE_HOST:nrcdb03.bcgov}

# Users allowed to manually trigger the Dashboard extraction job
nr:
results:
dashboard-job-users: ${DASHBOARD_JOB_IDIR_USERS}

# Users allowed to see and download WMS layers information
config:
wms-layers:
whitelist: ${WMS_LAYERS_WHITELIST_USERS:NONE}
opening-search:
org-units: ${OPENING_SEARCH_ORG_UNITS:-"DCK,DSQ,DVA,DKM,DSC,DFN,DSI,DCR,DMK,DQC,DKA,DCS,DOS,DSE,DCC,DMH,DQU,DNI,DND,DRM,DPG,DSS,DPC"}

# Forest Client API
forest-client-api:
address: ${FORESTCLIENTAPI_ADDRESS:https://nr-forest-client-api-prod.api.gov.bc.ca/api}
key: ${FORESTCLIENTAPI_KEY:placeholder-api-key}

# Logging
logging:
level:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import ca.bc.gov.restapi.results.extensions.AbstractTestContainerIntegrationTest;
import ca.bc.gov.restapi.results.extensions.WithMockJwt;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(SecretsServiceEndpoint.class)
@WithMockUser(roles = "user_read")
class SecretsServiceEndpointTest {

@Autowired private MockMvc mockMvc;
@WithMockJwt
@AutoConfigureMockMvc
class SecretsServiceEndpointTest extends AbstractTestContainerIntegrationTest {

@Autowired
private MockMvc mockMvc;

@Test
@DisplayName("Get WMS layers whitelist users happy path should succeed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void getOpeningOrgUnitsByCode_happyPath_shouldSucceed() throws Exception {

List<OrgUnitEntity> orgUnitEntityList = List.of(orgUnit);

when(orgUnitService.findAllOrgUnitsByCode(new String[]{"DAS"})).thenReturn(orgUnitEntityList);
when(orgUnitService.findAllOrgUnitsByCode(List.of("DAS"))).thenReturn(orgUnitEntityList);

mockMvc
.perform(
Expand Down Expand Up @@ -287,7 +287,7 @@ void getOpeningOrgUnitsByCode_happyPath_shouldSucceed() throws Exception {
@Test
@DisplayName("Get Opening Org Units By Code not Found should Succeed")
void getOpeningOrgUnitsByCode_notFound_shouldSucceed() throws Exception {
when(orgUnitService.findAllOrgUnitsByCode(new String[]{"DAS"})).thenReturn(List.of());
when(orgUnitService.findAllOrgUnitsByCode(List.of("DAS"))).thenReturn(List.of());

mockMvc
.perform(
Expand Down
Loading

0 comments on commit 2cded86

Please sign in to comment.