Skip to content

Commit

Permalink
Fix moog extraction (#100)
Browse files Browse the repository at this point in the history
* fix: moog extraction slow (n+1 requests)

* refactor: sonar issues

* chore: update spring boot version

* refactor: sonar issues on tests

* fix: sonar issues

* refactor: review changes
  • Loading branch information
BettyB979 authored Jan 8, 2025
1 parent 596b8c1 commit 38df44c
Show file tree
Hide file tree
Showing 19 changed files with 443 additions and 461 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mv) {
// no need to posthandle things for this interceptor

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,31 @@ public class OpenAPIConfiguration {

@Bean public OpenAPI customOpenAPI() {

switch(applicationConfig.getAuthType()) {

case AuthConstants.OIDC:

OAuthFlows flows = new OAuthFlows();
OAuthFlow flow = new OAuthFlow();

flow.setAuthorizationUrl(applicationConfig.getKeyCloakUrl() + "/realms/" + applicationConfig.getKeycloakRealm() + "/protocol/openid-connect/auth");
flow.setTokenUrl(applicationConfig.getKeyCloakUrl() + "/realms/" + applicationConfig.getKeycloakRealm() + "/protocol/openid-connect/token");
Scopes scopes = new Scopes();
// scopes.addString("global", "accessEverything");
flow.setScopes(scopes);
flows = flows.authorizationCode(flow);

return new OpenAPI()
.components(
new Components().addSecuritySchemes("oauth2", new SecurityScheme().type(SecurityScheme.Type.OAUTH2).flows(flows)))
.info(new Info().title(buildProperties.getName()).version(buildProperties.getVersion()))
.addSecurityItem(new SecurityRequirement().addList("oauth2", Arrays.asList("read", "write")));

default:
return new OpenAPI()
.info(new Info().title(buildProperties.getName()).version(buildProperties.getVersion()));

if (applicationConfig.getAuthType().equals(AuthConstants.OIDC)) {
OAuthFlows flows = getoAuthFlows();

return new OpenAPI()
.components(
new Components().addSecuritySchemes("oauth2", new SecurityScheme().type(SecurityScheme.Type.OAUTH2).flows(flows)))
.info(new Info().title(buildProperties.getName()).version(buildProperties.getVersion()))
.addSecurityItem(new SecurityRequirement().addList("oauth2", Arrays.asList("read", "write")));
}
return new OpenAPI()
.info(new Info().title(buildProperties.getName()).version(buildProperties.getVersion()));

}

private OAuthFlows getoAuthFlows() {
OAuthFlows flows = new OAuthFlows();
OAuthFlow flow = new OAuthFlow();

flow.setAuthorizationUrl(applicationConfig.getKeyCloakUrl() + "/realms/" + applicationConfig.getKeycloakRealm() + "/protocol/openid-connect/auth");
flow.setTokenUrl(applicationConfig.getKeyCloakUrl() + "/realms/" + applicationConfig.getKeycloakRealm() + "/protocol/openid-connect/token");
Scopes scopes = new Scopes();
flow.setScopes(scopes);
flows = flows.authorizationCode(flow);
return flows;
}


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void handleContextRefresh(ContextRefreshedEvent event) {
log.info("================================ Properties ================================");
final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
StreamSupport.stream(sources.spliterator(), false)
.filter(ps -> ps instanceof EnumerablePropertySource)
.filter(EnumerablePropertySource.class::isInstance)
.map(ps -> ((EnumerablePropertySource<?>) ps).getPropertyNames())
.flatMap(Arrays::stream)
.distinct()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ void putAddressCreateUpdate() throws Exception {
assertEquals(contactAfterUpdate.getAddress().getStreetName(), addressUpdated.getStreetName());
assertEquals(contactAfterUpdate.getAddress().getCountryName(), addressUpdated.getCountryName());

// back to before
contact.setAddress(addressBefore);
addressService.saveAddress(addressBefore);
contactService.saveContact(contact);
assertEquals(contact.getAddress().getCityName(), addressBefore.getCityName());
assertEquals(contact.getAddress().getStreetName(), addressBefore.getStreetName());
assertEquals(contact.getAddress().getCountryName(), addressBefore.getCountryName());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ void putContactCreateUpdateDelete() throws Exception {
assertEquals(contact.getFirstName(), contactFound.getFirstName());
assertEquals(contact.getEmail(), contactFound.getEmail());
List<ContactEvent> list = new ArrayList<>(contactEventService.findContactEventsByContact(contactFound));
// List<ContactEvent> list = new ArrayList<>(contactFound.getContactEvents());
assertEquals(1, list.size());
assertEquals(ContactEventTypeEnum.create, list.get(0).getType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ private void initMetadata() throws ParseException {
Date openingDate = sdf.parse("01/01/" + year);
Date closingDate = sdf.parse("31/12/" + year);
Date returnDate = sdf.parse("01/06/" + year);
Date today = sdf.parse("31/12/" + year);

part.setOpeningDate(openingDate);
part.setClosingDate(closingDate);
Expand Down Expand Up @@ -313,7 +312,6 @@ private void initQuestionning(Faker faker) {
QuestioningAccreditation accreditation;
Set<QuestioningAccreditation> questioningAccreditations;
String fakeSiren;
Random qeRan = new Random();

for (Long i = surveyUnitRepository.count(); i < 10; i++) {
SurveyUnit su = new SurveyUnit();
Expand All @@ -326,7 +324,6 @@ private void initQuestionning(Faker faker) {
}
for (Long i = nbExistingQuestionings; i < 10; i++) {
qu = new Questioning();
qe = new QuestioningEvent();
List<QuestioningEvent> qeList = new ArrayList<>();
questioningAccreditations = new HashSet<>();

Expand All @@ -344,7 +341,6 @@ private void initQuestionning(Faker faker) {
// questioning events
// everybody in INITLA
Optional<Partitioning> part = partitioningRepository.findById(qu.getIdPartitioning());
Date eventDate = today;

qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
import fr.insee.survey.datacollectionmanagement.constants.AuthorityRoleEnum;
import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.query.dto.MyQuestioningDto;
import fr.insee.survey.datacollectionmanagement.query.service.CheckHabilitationService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -29,11 +27,7 @@
class MyQuestioningsControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private CheckHabilitationService checkAccreditationService;

MockMvc mockMvc;

@Test
void myQuestioningsContactNotExist() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.springframework.test.web.servlet.MockMvc;

import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Expand Down Expand Up @@ -54,7 +54,7 @@ void init() {
void getQuestioningAccreditationOk() throws Exception {
Questioning questioning = questioningService.findBySurveyUnitIdSu("100000001").stream().findFirst().get();
Long identifier = questioning.getQuestioningAccreditations().stream().findFirst().get().getId();
String json = createJsonQuestioningAcreditation(identifier);
String json = createJsonQuestioningAcreditation();
this.mockMvc.perform(get(Constants.API_QUESTIONINGS_ID_QUESTIONING_ACCREDITATIONS, identifier)).andDo(print()).andExpect(status().isOk())
.andExpect(content().json(json, false));
}
Expand Down Expand Up @@ -109,12 +109,12 @@ void postAccreditationCreateUpdate() throws Exception {
post(Constants.API_QUESTIONINGS_ID_QUESTIONING_ACCREDITATIONS, idQuestioning)
.content(jsonAccreditation).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(content().json(jsonAccreditation.toString(), false));
Questioning questioning = questioningService.findbyId((long) idQuestioning);
.andExpect(content().json(jsonAccreditation, false));
Questioning questioning = questioningService.findbyId(idQuestioning);
Set<QuestioningAccreditation> setAccreditationFound = questioning.getQuestioningAccreditations();
QuestioningAccreditation accreditationFound = setAccreditationFound.stream()
.filter(acc -> acc.getIdContact().equals(idContact))
.collect(Collectors.toList()).get(0);
.toList().getFirst();
assertEquals(accreditationFound.getCreationAuthor(), accreditation.getCreationAuthor());
assertEquals(accreditationFound.getIdContact(), accreditation.getIdContact());

Expand All @@ -125,12 +125,12 @@ void postAccreditationCreateUpdate() throws Exception {
post(Constants.API_QUESTIONINGS_ID_QUESTIONING_ACCREDITATIONS, idQuestioning)
.content(jsonAccreditationUpdate).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(jsonAccreditationUpdate.toString(), false));
.andExpect(content().json(jsonAccreditationUpdate, false));

QuestioningAccreditation accreditationFoundAfterUpdate = questioningService.findbyId((long) idQuestioning)
QuestioningAccreditation accreditationFoundAfterUpdate = questioningService.findbyId(idQuestioning)
.getQuestioningAccreditations().stream().filter(acc -> acc.getIdContact().equals(idContact))
.toList().get(0);
assertEquals(true, accreditationFoundAfterUpdate.isMain());
.toList().getFirst();
assertTrue(accreditationFoundAfterUpdate.isMain());

}

Expand All @@ -150,7 +150,7 @@ private String createJson(QuestioningAccreditation accreditation) throws JSONExc
return jo.toString();
}

private String createJsonQuestioningAcreditation(Long identifier) throws JSONException {
private String createJsonQuestioningAcreditation() throws JSONException {
JSONObject jo1 = new JSONObject();
jo1.put("idContact", "CONT1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ void getQuestioningsBySurveyUnit() throws Exception {

}

private JSONObject createJson(Long id) throws JSONException {
JSONObject jo = new JSONObject();
jo.put("id", id);
return jo;
}

private String createJsonQuestionings(String id) throws JSONException {
JSONObject jo = new JSONObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void init() {
void getQuestioningEventOk() throws Exception {
Questioning questioning = questioningService.findBySurveyUnitIdSu("100000001").stream().findFirst().get();
Long id = questioning.getQuestioningAccreditations().stream().findFirst().get().getId();
String json = createJsonQuestioningEvent(id);
String json = createJsonQuestioningEvent();
this.mockMvc.perform(get(Constants.API_QUESTIONING_ID_QUESTIONING_EVENTS, id)).andDo(print()).andExpect(status().isOk())
.andExpect(content().json(json, false));
}
Expand All @@ -58,7 +58,7 @@ void getQuestioningEventNotFound() throws Exception {

}

private String createJsonQuestioningEvent(Long identifier) throws JSONException {
private String createJsonQuestioningEvent() throws JSONException {
JSONObject joEventInitla = new JSONObject();
joEventInitla.put("type", "INITLA");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import fr.insee.survey.datacollectionmanagement.configuration.AuthenticationUserProvider;
import fr.insee.survey.datacollectionmanagement.constants.AuthorityRoleEnum;
import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.user.domain.UserEvent;
import fr.insee.survey.datacollectionmanagement.user.enums.UserEventTypeEnum;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,4 @@ public class Address {
private String countryCode;
private String countryName;

public String toStringMoog() {
return (streetNumber!=null?streetNumber:"") +
" "+ (repetitionIndex!=null?repetitionIndex:"")+
" " +(streetType!=null?streetType:"")+
"" + (streetName!=null?streetName:"")+
" " +(addressSupplement!=null?addressSupplement:"") +
" " + (cityName!=null?cityName:"")+
" " + (zipCode!=null?zipCode:"") +
" " + (cedexCode!=null?cedexCode:"") +
" " + (cedexName!=null?cedexName:"") +
" " + (specialDistribution!=null?specialDistribution:"") +
" " + (countryCode!=null?countryCode:"") +
" " + (countryName!=null?countryName:"");
}
}
Loading

0 comments on commit 38df44c

Please sign in to comment.