Skip to content
Snippets Groups Projects
Commit 316ebeb4 authored by Riabokon Stanislav(EPAM)[GCP]'s avatar Riabokon Stanislav(EPAM)[GCP]
Browse files

Search not handling Policy error responses gracefully (GONRG-7105)

parent 515219f3
No related branches found
No related tags found
2 merge requests!537Draft: Update version of default branch to 0.22.0-SNAPSHOT,!505Search not handling Policy error responses gracefully (GONRG-7105)
......@@ -17,22 +17,19 @@ package org.opengroup.osdu.search.policy.service;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.policy.IPolicyFactory;
import org.opengroup.osdu.core.common.policy.IPolicyProvider;
import org.opengroup.osdu.core.common.policy.PolicyException;
import org.opengroup.osdu.search.policy.di.PolicyServiceConfiguration;
import org.opengroup.osdu.search.provider.interfaces.IProviderHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Service
@ConditionalOnProperty(value = "service.policy.enabled", havingValue = "true", matchIfMissing = false)
......@@ -64,9 +61,29 @@ public class PolicyServiceImpl implements IPolicyService {
IPolicyProvider serviceClient = this.policyFactory.create(this.headers);
String esQuery = serviceClient.getCompiledPolicy(searchPolicyRule, unknownsList, input);
return esQuery.substring(9, esQuery.length() - 1);
} catch (Exception e) {
String errorMessage = StringUtils.isBlank(e.getMessage()) ? "Error making request to Policy service" : e.getMessage();
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Policy service unavailable", errorMessage, "Error calling translate endpoint", e);
}
} catch (PolicyException e) {
HttpResponse httpResponse = e.getHttpResponse();
String errorMessage =
StringUtils.isBlank(e.getMessage())
? "Error making request to Policy service"
: e.getMessage();
throw new AppException(
httpResponse.getResponseCode(),
errorMessage,
httpResponse.getBody(),
"Error calling translate endpoint",
e);
} catch (Exception e) {
String errorMessage =
StringUtils.isBlank(e.getMessage())
? "Error making request to Policy service"
: e.getMessage();
throw new AppException(
HttpStatus.SC_INTERNAL_SERVER_ERROR,
"Policy service unavailable",
errorMessage,
"Error calling translate endpoint",
e);
}
}
}
package org.opengroup.osdu.search.service.policy.service;
import org.apache.http.HttpStatus;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -8,6 +9,8 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.http.HttpResponse;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.policy.IPolicyFactory;
import org.opengroup.osdu.core.common.policy.IPolicyProvider;
......@@ -15,15 +18,12 @@ import org.opengroup.osdu.core.common.policy.PolicyException;
import org.opengroup.osdu.search.policy.di.PolicyServiceConfiguration;
import org.opengroup.osdu.search.policy.service.PolicyServiceImpl;
import org.opengroup.osdu.search.provider.interfaces.IProviderHeaderService;
import org.opengroup.osdu.search.service.IEntitlementsExtensionService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import static org.mockito.ArgumentMatchers.any;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.*;
@RunWith(MockitoJUnitRunner.class)
public class PolicyServiceImplTest {
......@@ -84,4 +84,69 @@ public class PolicyServiceImplTest {
Mockito.when(serviceClient.getCompiledPolicy("data.osdu.partition[\"dpi\"].search.allow == true", unknownsList, input)).thenReturn(PolicyServiceResponse);
Assert.assertNotNull(sut.getCompiledPolicy(providerHeaderService));
}
@Test
public void throw_policy_exception() throws PolicyException {
List<String> groups = new ArrayList<>(Arrays.asList("AAA", "BBB"));
Map<String, Object> userData = new HashMap<>();
userData.put("groups", groups);
Map<String, Object> input = new HashMap<>();
input.put("groups", groups);
input.put("operation", "view");
ArrayList<String> unknownsList = new ArrayList<>();
unknownsList.add("input.record");
Map<String, String> dpsHeaders = new HashMap<>();
dpsHeaders.put("groups", "AAA,BBB");
Mockito.when(headers.getHeaders()).thenReturn(dpsHeaders);
Mockito.when(headers.getPartitionId()).thenReturn("dpi");
Mockito.when(providerHeaderService.getDataGroupsHeader())
.thenReturn("groups");
Mockito.when(policyFactory.create(any())).thenReturn(serviceClient);
String searchPolicyId = "osdu.instance.search";
Mockito.when(policyServiceConfiguration.getId()).thenReturn(searchPolicyId);
HttpResponse httpResponse = new HttpResponse();
httpResponse.setBody("{\\\"detail\\\":\\\"Translate API Error: expected query in format data.osdu.partition...\\\"}");
httpResponse.setResponseCode(HttpStatus.SC_BAD_REQUEST);
PolicyException policyException = new PolicyException("Error making request to Policy service", httpResponse);
Mockito.when(serviceClient.getCompiledPolicy(anyString(), anyList(), anyMap())).thenThrow(policyException);
AppException exception = assertThrows(AppException.class, () -> {
sut.getCompiledPolicy(providerHeaderService);
});
assertEquals(HttpStatus.SC_BAD_REQUEST, ((PolicyException)exception.getOriginalException()).getResponse().getResponseCode());
}
@Test
public void throw_any_exception() throws PolicyException {
List<String> groups = new ArrayList<>(Arrays.asList("AAA", "BBB"));
Map<String, Object> userData = new HashMap<>();
userData.put("groups", groups);
Map<String, Object> input = new HashMap<>();
input.put("groups", groups);
input.put("operation", "view");
ArrayList<String> unknownsList = new ArrayList<>();
unknownsList.add("input.record");
Map<String, String> dpsHeaders = new HashMap<>();
dpsHeaders.put("groups", "AAA,BBB");
Mockito.when(headers.getHeaders()).thenReturn(dpsHeaders);
Mockito.when(headers.getPartitionId()).thenReturn("dpi");
Mockito.when(providerHeaderService.getDataGroupsHeader())
.thenReturn("groups");
Mockito.when(policyFactory.create(any())).thenReturn(serviceClient);
String searchPolicyId = "osdu.instance.search";
Mockito.when(policyServiceConfiguration.getId()).thenReturn(searchPolicyId);
Mockito.when(serviceClient.getCompiledPolicy(anyString(), anyList(), anyMap())).thenThrow(new RuntimeException());
AppException exception = assertThrows(AppException.class, () -> {
sut.getCompiledPolicy(providerHeaderService);
});
assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, exception.getError().getCode());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment