Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend buffer #287

Merged
merged 7 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.2</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -44,9 +46,16 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) {

@Bean
public WebClient webClient() {
//extend buffer to 50MB
Integer CODEC_50_MB_SIZE = 50 * 1024 * 1024;
HttpClient client = HttpClient.create();
client.warmup().block();
return WebClient.builder().build();
return WebClient.builder().codecs(clientCodecConfigurer -> {
var codec = new Jackson2JsonDecoder();
codec.setMaxInMemorySize(CODEC_50_MB_SIZE);
clientCodecConfigurer.customCodecs().register(codec);
clientCodecConfigurer.customCodecs().register(new Jackson2JsonEncoder());
}).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,13 @@ public ResponseEntity<List<School>> getSchoolsByParams(
public ResponseEntity<Boolean> checkSchoolExists(@PathVariable String minCode) {
return response.GET(schoolService.existsSchool(minCode));
}

@GetMapping(EducGradTraxApiConstants.GET_SCHOOLS_BY_SCHOOL_CATEGORY_MAPPING)
@PreAuthorize(PermissionsConstants.READ_SCHOOL_DATA)
@Operation(summary = "Check school existence by Mincode", description = "Check school existence by Mincode", tags = { "School" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "204", description = "NO CONTENT")})
public ResponseEntity<List<School>> getSchoolsBySchoolCategory(@RequestParam(required = false) String schoolCategory, @RequestHeader(name="Authorization") String accessToken) {
return response.GET(schoolService.getSchoolsBySchoolCategory(schoolCategory, accessToken.replace(BEARER, "")));
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package ca.bc.gov.educ.api.trax.model.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PastOrPresent;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.PastOrPresent;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.UUID;
Expand Down Expand Up @@ -91,7 +92,12 @@ public String getEventPayload() {
* @param eventPayload the event payload
*/
public void setEventPayload(String eventPayload) {
setEventPayloadBytes(eventPayload.getBytes(StandardCharsets.UTF_8));
setEventPayloadBytes(nullSafeEventPayload(eventPayload).getBytes(StandardCharsets.UTF_8));
}

private static String nullSafeEventPayload(String eventPayload) {
if(StringUtils.isBlank(eventPayload)) eventPayload = "{}";
return eventPayload;
}

/**
Expand All @@ -110,7 +116,7 @@ public static class TraxUpdatedPubEventBuilder {
* @return the student event . student event builder
*/
public TraxUpdatedPubEventBuilder eventPayload(String eventPayload) {
this.eventPayloadBytes = eventPayload.getBytes(StandardCharsets.UTF_8);
this.eventPayloadBytes = nullSafeEventPayload(eventPayload).getBytes(StandardCharsets.UTF_8);
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ public boolean existsSchool(String minCode) {
return schoolRepository.countTabSchools(minCode) > 0L;
}

public List<School> getSchoolsBySchoolCategory(String schoolCategoryCode, String accessToken) {
List<School> result = new ArrayList<>();
if(StringUtils.isBlank(schoolCategoryCode)) {
return adaptSchools(getCommonSchools(accessToken));
} else {
List<CommonSchool> schools = getCommonSchools(accessToken);
for (CommonSchool s : schools) {
if (StringUtils.equalsIgnoreCase(schoolCategoryCode, s.getSchoolCategoryCode())) {
result.add(adaptSchool(s));
}
}
}
return result;
}

public CommonSchool getCommonSchool(String accessToken, String mincode) {
if(StringUtils.isBlank(mincode)) {
return null;
Expand Down Expand Up @@ -178,6 +193,22 @@ public List<CommonSchool> getCommonSchools(String accessToken) {
}
}

private School adaptSchool(CommonSchool commonSchool) {
School school = new School();
school.setMinCode(commonSchool.getDistNo()+commonSchool.getSchlNo());
school.setSchoolName(commonSchool.getSchoolName());
school.setSchoolCategory(commonSchool.getSchoolCategoryCode());
return school;
}

private List<School> adaptSchools(List<CommonSchool> commonSchools) {
List<School> result = new ArrayList<>();
for(CommonSchool sch: commonSchools) {
result.add(adaptSchool(sch));
}
return result;
}

private void adaptSchool(School school, CommonSchool commonSchool) {
if(commonSchool == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class EducGradTraxApiConstants {

public static final String GET_DISTRICT_BY_DISTNO_MAPPING = "/{distCode}";
public static final String GET_DISTRICTS_BY_SCHOOL_CATEGORY_MAPPING = "/schoolCategories";
public static final String GET_SCHOOLS_BY_SCHOOL_CATEGORY_MAPPING = "/schoolCategories";

public static final String GET_TRANSCRIPT_DEMOG_BY_PEN_MAPPING = "/tran-demog/{pen}";
public static final String GET_TRANSCRIPT_COURSE_BY_PEN_MAPPING = "/tran-courses/{pen}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public void testGetSchoolsByParams() {
Mockito.verify(schoolService).getSchoolsByParams("1234567", "123", null, "accessToken");
}

@Test
public void testGetSchoolsBySchoolCategoryCode() {
final School school = new School();
school.setMinCode("1234567");
school.setSchoolName("Test School");
Mockito.when(schoolService.getSchoolsBySchoolCategory("01", "accessToken")).thenReturn(Arrays.asList(school));
schoolController.getSchoolsBySchoolCategory("01", "accessToken");
Mockito.verify(schoolService).getSchoolsBySchoolCategory("01", "accessToken");
}

@Test
public void testCheckSchoolExists() {
Mockito.when(schoolService.existsSchool("1234567")).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,19 @@ void testGetSchoolsByParams() {
assertThat(result).isNotNull();

}

@Test
void testGetSchoolsBySchoolCategoryCode() {
mockCommonSchools();
var result = schoolService.getSchoolsBySchoolCategory("02", "accessToken");
assertThat(result).isNotNull().isNotEmpty();
result = schoolService.getSchoolsBySchoolCategory("01", "accessToken");
assertThat(result).isNotNull().isEmpty();
result = schoolService.getSchoolsBySchoolCategory("", "accessToken");
assertThat(result).isNotNull().isNotEmpty();

}

@Test
void testGetSchoolsByParams_params() {
// School
Expand Down Expand Up @@ -468,4 +481,18 @@ void mockCommonSchool(String minCode, String schoolName) {
Mockito.when(this.responseMock.bodyToMono(new ParameterizedTypeReference<List<CommonSchool>>() {
})).thenReturn(Mono.just(List.of(commonSchool)));
}

void mockCommonSchools() {
CommonSchool commonSchool = new CommonSchool();
commonSchool.setSchlNo("1234567");
commonSchool.setSchoolName("Test School");
commonSchool.setSchoolCategoryCode("02");

Mockito.when(this.webClient.get()).thenReturn(this.requestHeadersUriMock);
Mockito.when(this.requestHeadersUriMock.uri(constants.getAllSchoolSchoolApiUrl())).thenReturn(this.requestHeadersMock);
Mockito.when(this.requestHeadersMock.headers(any(Consumer.class))).thenReturn(this.requestHeadersMock);
Mockito.when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock);
Mockito.when(this.responseMock.bodyToMono(new ParameterizedTypeReference<List<CommonSchool>>() {
})).thenReturn(Mono.just(List.of(commonSchool)));
}
}
Loading