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

[VAS] Bug/vas 10897 manage access contracts errors #1306

Merged
merged 1 commit into from
Apr 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
*/
public final class RulesUpdateCommonService {

private RulesUpdateCommonService() {

}

public static void deleteAttributesFromObjectNode(ObjectNode dslRequest, String... attributes) {
if (dslRequest != null && attributes.length > 0) {
Arrays.stream(attributes).forEach(dslRequest::remove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
@NoArgsConstructor
@ToString
public class ArchiveUnitsDto extends IdDto implements Serializable {
private static final long serialVersionUID = 19051220419502517L;
private VitamUIArchiveUnitResponseDto archives;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ public class RulesUpdateCommonServiceTest {
"data/expected_search_UA_query_without_filter_and_projection.json";
public final String EXPECTED_SEARCH_UA_QUERY_WITHOUT_FILTER =
"data/expected_search_UA_query_without_filter.json";
private final RulesUpdateCommonService rulesUpdateCommonService = new RulesUpdateCommonService();

@Test
void search_query_without_filter_field() throws FileNotFoundException, InvalidParseOperationException {
JsonNode search_query_json = JsonHandler.getFromFile(PropertiesUtils.findFile(SEARCH_UA_QUERY));
JsonNode expected_search_query_json = JsonHandler.getFromFile(PropertiesUtils
.findFile(EXPECTED_SEARCH_UA_QUERY_WITHOUT_FILTER));
rulesUpdateCommonService.deleteAttributesFromObjectNode((ObjectNode) search_query_json, "$filter");
RulesUpdateCommonService.deleteAttributesFromObjectNode((ObjectNode) search_query_json, "$filter");
assertThat(search_query_json).isEqualTo(expected_search_query_json);
}

Expand All @@ -68,7 +67,7 @@ void search_query_without_filter_and_projection_fields()
JsonNode search_query_json = JsonHandler.getFromFile(PropertiesUtils.findFile(SEARCH_UA_QUERY));
JsonNode expected_search_query_json = JsonHandler.getFromFile(PropertiesUtils
.findFile(EXPECTED_SEARCH_UA_QUERY_WITHOUT_FILTER_AND_PROJECTION));
rulesUpdateCommonService.deleteAttributesFromObjectNode((ObjectNode) search_query_json, "$filter",
RulesUpdateCommonService.deleteAttributesFromObjectNode((ObjectNode) search_query_json, "$filter",
"$projection");
assertThat(search_query_json).isEqualTo(expected_search_query_json);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
import com.fasterxml.jackson.databind.JsonNode;
import fr.gouv.vitamui.archives.search.common.dto.ArchiveUnitsDto;
import fr.gouv.vitamui.archives.search.common.dto.ExportDipCriteriaDto;
import fr.gouv.vitamui.commons.api.dtos.OntologyDto;
import fr.gouv.vitamui.archives.search.common.dto.ReclassificationCriteriaDto;
import fr.gouv.vitamui.archives.search.common.dto.RuleSearchCriteriaDto;
import fr.gouv.vitamui.archives.search.common.dto.TransferRequestDto;
import fr.gouv.vitamui.archives.search.common.dto.UnitDescriptiveMetadataDto;
import fr.gouv.vitamui.archives.search.common.rest.RestApi;
import fr.gouv.vitamui.commons.api.CommonConstants;
import fr.gouv.vitamui.commons.api.domain.PaginatedValuesDto;
import fr.gouv.vitamui.commons.api.dtos.OntologyDto;
import fr.gouv.vitamui.commons.api.dtos.SearchCriteriaDto;
import fr.gouv.vitamui.commons.api.logger.VitamUILogger;
import fr.gouv.vitamui.commons.api.logger.VitamUILoggerFactory;
Expand All @@ -55,7 +55,6 @@
import org.springframework.web.util.UriComponentsBuilder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


Expand Down Expand Up @@ -93,7 +92,7 @@ public String getPathUrl() {

public ArchiveUnitsDto searchArchiveUnitsByCriteria(ExternalHttpContext context, SearchCriteriaDto query) {
LOGGER.debug("Calling search archives units by criteria");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);

final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(query, headers);
final ResponseEntity<ArchiveUnitsDto> response =
Expand All @@ -105,7 +104,7 @@ public ArchiveUnitsDto searchArchiveUnitsByCriteria(ExternalHttpContext context,

public VitamUISearchResponseDto getFilingHoldingScheme(ExternalHttpContext context) {
LOGGER.debug("Calling get filing holding scheme");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);

final HttpEntity<Void> request = new HttpEntity<>(headers);
final ResponseEntity<VitamUISearchResponseDto> response = restTemplate
Expand All @@ -115,20 +114,6 @@ public VitamUISearchResponseDto getFilingHoldingScheme(ExternalHttpContext conte
return response.getBody();
}

protected MultiValueMap<String, String> buildSearchHeaders(final ExternalHttpContext context) {
final MultiValueMap<String, String> headers = buildHeaders(context);
String accessContract = null;
if (context instanceof ExternalHttpContext) {
final ExternalHttpContext externalCallContext = context;
accessContract = externalCallContext.getAccessContract();
}

if (accessContract != null) {
headers.put(CommonConstants.X_ACCESS_CONTRACT_ID_HEADER, Collections.singletonList(accessContract));
}
return headers;
}

public ResponseEntity<ResultsDto> findUnitById(String id, ExternalHttpContext context) {
final UriComponentsBuilder uriBuilder =
UriComponentsBuilder.fromHttpUrl(getUrl() + RestApi.ARCHIVE_UNIT_INFO + CommonConstants.PATH_ID);
Expand All @@ -147,7 +132,7 @@ public ResponseEntity<ResultsDto> findObjectById(String id, ExternalHttpContext
public ResponseEntity<Resource> exportCsvArchiveUnitsByCriteria(SearchCriteriaDto query,
ExternalHttpContext context) {
LOGGER.debug("Calling export to csv search archives units by criteria");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);

final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(query, headers);
return restTemplate.exchange(getUrl() + RestApi.EXPORT_CSV_SEARCH_PATH, HttpMethod.POST,
Expand All @@ -157,7 +142,7 @@ public ResponseEntity<Resource> exportCsvArchiveUnitsByCriteria(SearchCriteriaDt
public ResponseEntity<String> exportDIPCriteria(ExportDipCriteriaDto exportDipCriteriaDto,
ExternalHttpContext context) {
LOGGER.debug("Calling export DIP by criteria");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);
final HttpEntity<ExportDipCriteriaDto> request = new HttpEntity<>(exportDipCriteriaDto, headers);
return restTemplate.exchange(getUrl() + RestApi.EXPORT_DIP, HttpMethod.POST,
request, String.class);
Expand All @@ -166,22 +151,22 @@ public ResponseEntity<String> exportDIPCriteria(ExportDipCriteriaDto exportDipCr
public ResponseEntity<String> transferRequest(TransferRequestDto transferRequestDto,
ExternalHttpContext context) {
LOGGER.debug("Calling transfer request");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);
final HttpEntity<TransferRequestDto> request = new HttpEntity<>(transferRequestDto, headers);
return restTemplate.exchange(getUrl() + RestApi.TRANSFER_REQUEST, HttpMethod.POST, request, String.class);
}

public ResponseEntity<JsonNode> startEliminationAnalysis(ExternalHttpContext context, SearchCriteriaDto query) {
LOGGER.debug("Calling elimination analysis by criteria");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);
final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(query, headers);
return restTemplate.exchange(getUrl() + RestApi.ELIMINATION_ANALYSIS, HttpMethod.POST,
request, JsonNode.class);
}

public ResponseEntity<JsonNode> startEliminationAction(ExternalHttpContext context, SearchCriteriaDto query) {
LOGGER.debug("Calling elimination action by using criteria {}", query);
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);
final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(query, headers);
return restTemplate.exchange(getUrl() + RestApi.ELIMINATION_ACTION, HttpMethod.POST,
request, JsonNode.class);
Expand All @@ -190,7 +175,7 @@ public ResponseEntity<JsonNode> startEliminationAction(ExternalHttpContext conte
public ResponseEntity<String> updateArchiveUnitsRules(RuleSearchCriteriaDto ruleSearchCriteriaDto,
ExternalHttpContext context) {
LOGGER.debug("Calling updateArchiveUnitsRules by criteria");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);
final HttpEntity<RuleSearchCriteriaDto> request = new HttpEntity<>(ruleSearchCriteriaDto, headers);
return restTemplate.exchange(getUrl() + RestApi.MASS_UPDATE_UNITS_RULES, HttpMethod.POST,
request, String.class);
Expand All @@ -199,7 +184,7 @@ public ResponseEntity<String> updateArchiveUnitsRules(RuleSearchCriteriaDto rule
public ResponseEntity<String> computedInheritedRules(SearchCriteriaDto searchCriteriaDto,
ExternalHttpContext context) {
LOGGER.debug("Calling computed inherited rules by criteria");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);
final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(searchCriteriaDto, headers);
return restTemplate.exchange(getUrl() + RestApi.COMPUTED_INHERITED_RULES, HttpMethod.POST,
request, String.class);
Expand All @@ -209,7 +194,7 @@ public ResponseEntity<String> computedInheritedRules(SearchCriteriaDto searchCri
public ResponseEntity<ResultsDto> selectUnitWithInheritedRules(ExternalHttpContext context,
SearchCriteriaDto query) {
LOGGER.debug("Calling select Unit With Inherited Rules by criteria");
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);

final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(query, headers);
final ResponseEntity<ResultsDto> response =
Expand All @@ -222,7 +207,7 @@ public ResponseEntity<ResultsDto> selectUnitWithInheritedRules(ExternalHttpConte
public ResponseEntity<String> reclassification(final ReclassificationCriteriaDto reclassificationCriteriaDto,
final ExternalHttpContext context) {
LOGGER.debug("Calling reclassification with query {} ", reclassificationCriteriaDto);
MultiValueMap<String, String> headers = buildSearchHeaders(context);
MultiValueMap<String, String> headers = buildHeaders(context);
final HttpEntity<ReclassificationCriteriaDto> request = new HttpEntity<>(reclassificationCriteriaDto, headers);
final ResponseEntity<String> response =
restTemplate.exchange(getUrl() + RestApi.RECLASSIFICATION, HttpMethod.POST,
Expand All @@ -239,8 +224,9 @@ public ResponseEntity<String> updateUnitById(String id, UnitDescriptiveMetadataD
return restTemplate.exchange(uriBuilder.build(id), HttpMethod.PUT, request, String.class);
}

public List<OntologyDto> getExternalOntologiesList(ExternalHttpContext context) {
final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(getUrl() + CommonConstants.EXTERNAL_ONTOLOGIES_LIST);
public List<OntologyDto> getExternalOntologiesList(ExternalHttpContext context) {
final UriComponentsBuilder uriBuilder =
UriComponentsBuilder.fromHttpUrl(getUrl() + CommonConstants.EXTERNAL_ONTOLOGIES_LIST);
final HttpEntity<?> request = new HttpEntity<>(buildHeaders(context));
return restTemplate.exchange(uriBuilder.build().toUri(), HttpMethod.GET, request, ArrayList.class).getBody();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
Expand Down Expand Up @@ -89,12 +88,10 @@ public void sampleArchiveTest() {
public void when_searchArchiveUnitsByCriteria_rest_template_ok_should_return_ok() {
ExternalHttpContext context = new ExternalHttpContext(9, "", "", "");
SearchCriteriaDto query = new SearchCriteriaDto();
MultiValueMap<String, String> headers = archiveSearchExternalRestClient.buildSearchHeaders(context);
final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(query, headers);
final ArchiveUnitsDto responseEntity = new ArchiveUnitsDto();

when(restTemplate
.exchange(anyString(), eq(HttpMethod.POST), eq(request), eq(ArchiveUnitsDto.class)))
.exchange(anyString(), eq(HttpMethod.POST), any(HttpEntity.class), eq(ArchiveUnitsDto.class)))
.thenReturn(new ResponseEntity<>(responseEntity, HttpStatus.OK));

ArchiveUnitsDto response =
Expand All @@ -106,12 +103,10 @@ public void when_searchArchiveUnitsByCriteria_rest_template_ok_should_return_ok(
@Test
public void whenGetFilingHoldingSChemeRestTemplateOKThenShouldReturnOK() {
ExternalHttpContext context = new ExternalHttpContext(9, "", "", "");
MultiValueMap<String, String> headers = archiveSearchExternalRestClient.buildSearchHeaders(context);
final HttpEntity<Void> request = new HttpEntity<>(headers);
final VitamUISearchResponseDto responseEntity = new VitamUISearchResponseDto();

when(restTemplate
.exchange(anyString(), eq(HttpMethod.GET), eq(request), eq(VitamUISearchResponseDto.class)))
.exchange(anyString(), eq(HttpMethod.GET), any(HttpEntity.class), eq(VitamUISearchResponseDto.class)))
.thenReturn(new ResponseEntity<>(responseEntity, HttpStatus.OK));

VitamUISearchResponseDto response =
Expand All @@ -124,16 +119,15 @@ public void whenGetFilingHoldingSChemeRestTemplateOKThenShouldReturnOK() {
@Test
public void whenGetexportCsvArchiveUnitsByCriteria_Srvc_ok_ThenShouldReturnOK() throws IOException {
ExternalHttpContext context = new ExternalHttpContext(9, "", "", "");
MultiValueMap<String, String> headers = archiveSearchExternalRestClient.buildSearchHeaders(context);
SearchCriteriaDto query = new SearchCriteriaDto();
final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(query, headers);
final HttpEntity<SearchCriteriaDto> request = new HttpEntity<>(query);

Resource resource = new ByteArrayResource(
Objects.requireNonNull(ArchiveSearchExternalRestClientTest.class.getClassLoader()
.getResourceAsStream(ARCHIVE_UNITS_RESULTS_CSV)).readAllBytes());

when(restTemplate.exchange(anyString(), eq(HttpMethod.POST),
eq(request), eq(Resource.class))).thenReturn(new ResponseEntity<>(resource, HttpStatus.OK));
any(HttpEntity.class), eq(Resource.class))).thenReturn(new ResponseEntity<>(resource, HttpStatus.OK));

ResponseEntity<Resource> response =
archiveSearchExternalRestClient.exportCsvArchiveUnitsByCriteria(query, context);
Expand All @@ -144,10 +138,9 @@ public void whenGetexportCsvArchiveUnitsByCriteria_Srvc_ok_ThenShouldReturnOK()
@Test
public void transferRequest_should_return_OK() {
ExternalHttpContext context = new ExternalHttpContext(9, "", "", "");
MultiValueMap<String, String> headers = archiveSearchExternalRestClient.buildSearchHeaders(context);
TransferRequestDto transferRequestDto = new TransferRequestDto();
final HttpEntity<TransferRequestDto> request = new HttpEntity<>(transferRequestDto, headers);
when(restTemplate.exchange(anyString(), eq(HttpMethod.POST), eq(request), any(Class.class)))
final HttpEntity<TransferRequestDto> request = new HttpEntity<>(transferRequestDto);
when(restTemplate.exchange(anyString(), eq(HttpMethod.POST), any(HttpEntity.class), any(Class.class)))
.thenReturn(new ResponseEntity<>("OK", HttpStatus.OK));
// When
ResponseEntity<String> response = archiveSearchExternalRestClient.transferRequest(transferRequestDto, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@
import fr.gouv.vitamui.iam.security.service.ExternalSecurityService;
import fr.gouv.vitamui.security.client.ContextRestClient;
import fr.gouv.vitamui.security.client.SecurityRestClientFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;

@Configuration
Expand Down
Loading