Skip to content

Commit

Permalink
Merge branch 'feature/OPHKIOS-85' into feature/OPHKIOS-35
Browse files Browse the repository at this point in the history
  • Loading branch information
jrkkp committed Jun 7, 2024
2 parents 161e39f + c1a0884 commit 6928b69
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 2 deletions.
13 changes: 13 additions & 0 deletions backend/vkt/src/main/java/fi/oph/vkt/api/PublicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import fi.oph.vkt.service.PublicExamEventService;
import fi.oph.vkt.service.PublicPersonService;
import fi.oph.vkt.service.PublicReservationService;
import fi.oph.vkt.service.koski.KoskiService;
import fi.oph.vkt.service.koski.dto.KoskiResponseDTO;
import fi.oph.vkt.util.SessionUtil;
import fi.oph.vkt.util.UIRouteUtil;
import fi.oph.vkt.util.exception.APIException;
Expand Down Expand Up @@ -78,6 +80,9 @@ public class PublicController {
@Resource
private PublicReservationService publicReservationService;

@Resource
private KoskiService koskiService;

@Resource
private UIRouteUtil uiRouteUtil;

Expand Down Expand Up @@ -143,6 +148,14 @@ public PublicExamEventDTO getExamEventInfo(@PathVariable final long examEventId)
return publicExamEventService.getExamEvent(examEventId);
}

/**
* TODO: remove this
*/
@GetMapping(path = "/education")
public KoskiResponseDTO getEducation() {
return koskiService.findEducations("1.2.246.562.24.97984579806");
}

/**
* Returns info about enrollment when refreshing a page during an enrollment step
*/
Expand Down
16 changes: 16 additions & 0 deletions backend/vkt/src/main/java/fi/oph/vkt/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.web.reactive.function.client.WebClient;
Expand Down Expand Up @@ -66,6 +68,20 @@ public PaymentProvider paytrailPaymentProvider(final Environment environment) {
return new PaytrailPaymentProvider(webClient, paytrailConfig, uuidSource);
}

@Bean
public WebClient koskiClient(final Environment environment) {
return webClientBuilderWithCallerId()
.baseUrl(environment.getRequiredProperty("app.koski.url"))
.defaultHeaders(headers -> {
headers.setBasicAuth(
environment.getRequiredProperty("app.koski.user"),
environment.getRequiredProperty("app.koski.password")
);
headers.setContentType(MediaType.APPLICATION_JSON);
})
.build();
}

@Bean
public CasTicketValidator casTicketValidator(final Environment environment) {
final WebClient webClient = webClientBuilderWithCallerId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.time.LocalDateTime;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -40,7 +39,6 @@ public class PublicEnrollmentService extends AbstractEnrollmentService {
private final PublicReservationService publicReservationService;
private final ReservationRepository reservationRepository;
private final FreeEnrollmentRepository freeEnrollmentRepository;
private final Environment environment;

@Transactional
public PublicEnrollmentInitialisationDTO initialiseEnrollment(final long examEventId, final Person person) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package fi.oph.vkt.service.koski;

import com.fasterxml.jackson.databind.ObjectMapper;
import fi.oph.vkt.service.koski.dto.KoskiResponseDTO;
import fi.oph.vkt.service.koski.dto.RequestBody;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;

@Service
@RequiredArgsConstructor
public class KoskiService {

private static final Logger LOG = LoggerFactory.getLogger(KoskiService.class);

private final WebClient koskiClient;

public KoskiResponseDTO findEducations(final String oid) {
final ObjectMapper objectMapper = new ObjectMapper();
final RequestBody body = new RequestBody(oid);

try {
final String bodyJson = objectMapper.writeValueAsString(body);
final String response = koskiClient
.post()
.uri("/oid")
.bodyValue(bodyJson)
.exchangeToMono(clientResponse -> {
if (clientResponse.statusCode().isError()) {
return clientResponse.createException().flatMap(Mono::error);
}
return clientResponse.bodyToMono(String.class);
})
.block();

return objectMapper.readValue(response, KoskiResponseDTO.class);
} catch (final WebClientResponseException e) {
LOG.error(
"KOSKI returned error status {}\n response body: {}",
e.getStatusCode().value(),
e.getResponseBodyAsString()
);
throw new RuntimeException(e);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fi.oph.vkt.service.koski.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import lombok.extern.jackson.Jacksonized;

@Data
@Builder
@Jacksonized
@JsonIgnoreProperties(ignoreUnknown = true)
public class KoskiResponseDTO {

private List<OpiskeluoikeusDTO> opiskeluoikeudet;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fi.oph.vkt.service.koski.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Builder;
import lombok.Data;
import lombok.extern.jackson.Jacksonized;

@Data
@Builder
@Jacksonized
@JsonIgnoreProperties(ignoreUnknown = true)
public class OpiskeluoikeusDTO {

private TyyppiDTO tyyppi;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fi.oph.vkt.service.koski.dto;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.NonNull;

@Builder
public record RequestBody(@NonNull @NotNull String oid) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fi.oph.vkt.service.koski.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Builder;
import lombok.Data;
import lombok.extern.jackson.Jacksonized;

@Data
@Builder
@Jacksonized
@JsonIgnoreProperties(ignoreUnknown = true)
public class TyyppiDTO {

private String koodiarvo;
}
4 changes: 4 additions & 0 deletions backend/vkt/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ app:
email:
sending-enabled: ${email.sending-enabled:false}
service-url: ${email.service-url:null}
koski:
url: https://koski.testiopintopolku.fi/koski/api/vkt
user: ${koski.username:null}
password: ${koski.password:null}
payment:
paytrail:
url: https://services.paytrail.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ cas-oppija.validate-ticket-url={{opintopolku_baseurl}}/cas-oppija/serviceValidat
payment.paytrail.secret={{vkt_paytrail_secret}}
payment.paytrail.account={{vkt_paytrail_account}}

koski.url={{vkt_koski_url}}
koski.user={{vkt_koski_user}}
koski.password={{vkt_koski_password}}

feature-flags.free-enrollment-allowed={{vkt_feature_free_enrollment_allowed}}

0 comments on commit 6928b69

Please sign in to comment.