Commit 772deeea authored by Alok Joshi's avatar Alok Joshi
Browse files

query with cursor integration with policy

parent 6b7c951c
Pipeline #31784 failed with stage
in 2 minutes and 15 seconds
......@@ -35,6 +35,7 @@ import org.opengroup.osdu.search.cache.CursorCache;
import org.opengroup.osdu.search.logging.AuditLogger;
import org.opengroup.osdu.search.provider.interfaces.IScrollQueryService;
import org.opengroup.osdu.search.util.ElasticClientHandler;
import org.opengroup.osdu.search.util.QueryResponseUtil;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
......@@ -60,6 +61,8 @@ public class ScrollQueryServiceAwsImpl extends QueryBase implements IScrollQuery
private CursorCache cursorCache;
@Inject
private AuditLogger auditLogger;
@Inject
private QueryResponseUtil queryResponseUtil;
private final MessageDigest digest;
......@@ -128,7 +131,7 @@ public class ScrollQueryServiceAwsImpl extends QueryBase implements IScrollQuery
if (results != null) {
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.results(queryResponseUtil.getQueryResponseResults(results))
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
......
......@@ -36,6 +36,7 @@ import org.opengroup.osdu.search.cache.CursorCache;
import org.opengroup.osdu.search.logging.AuditLogger;
import org.opengroup.osdu.search.provider.interfaces.IScrollQueryService;
import org.opengroup.osdu.search.util.ElasticClientHandler;
import org.opengroup.osdu.search.util.QueryResponseUtil;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
......@@ -60,6 +61,8 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
private CursorCache cursorCache;
@Inject
private AuditLogger auditLogger;
@Inject
private QueryResponseUtil queryResponseUtil;
private final MessageDigest digest;
......@@ -125,7 +128,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
if (results != null) {
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.results(queryResponseUtil.getQueryResponseResults(results))
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
......
......@@ -38,6 +38,7 @@ import org.opengroup.osdu.core.common.model.search.CursorSettings;
import org.opengroup.osdu.core.common.model.search.Query;
import org.opengroup.osdu.search.provider.interfaces.IScrollQueryService;
import org.opengroup.osdu.search.util.CrossTenantUtils;
import org.opengroup.osdu.search.util.QueryResponseUtil;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
......@@ -67,6 +68,9 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
@Inject
private AuditLogger auditLogger;
@Inject
private QueryResponseUtil queryResponseUtil;
private final MessageDigest digest;
ScrollQueryServiceImpl() throws NoSuchAlgorithmException {
......@@ -130,7 +134,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
if (results != null) {
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.results(queryResponseUtil.getQueryResponseResults(results))
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
......
......@@ -36,6 +36,7 @@ import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.opengroup.osdu.search.util.QueryResponseUtil;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
......@@ -60,6 +61,8 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
private CursorCache cursorCache;
@Inject
private AuditLogger auditLogger;
@Inject
private QueryResponseUtil queryResponseUtil;
private final MessageDigest digest;
......@@ -124,7 +127,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
if (results != null) {
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.results(queryResponseUtil.getQueryResponseResults(results))
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
......
......@@ -66,6 +66,7 @@ import org.opengroup.osdu.search.logging.AuditLogger;
import org.opengroup.osdu.search.provider.interfaces.IProviderHeaderService;
import org.opengroup.osdu.search.util.CrossTenantUtils;
import org.opengroup.osdu.search.util.ElasticClientHandler;
import org.opengroup.osdu.search.util.QueryResponseUtil;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
......@@ -94,6 +95,8 @@ public class ScrollQueryServiceTest {
private IProviderHeaderService providerHeaderService;
@Mock
private SearchConfigurationProperties searchConfig;
@Mock
private QueryResponseUtil queryResponseUtil;
private RestHighLevelClient restHighLevelClient;
......@@ -146,6 +149,7 @@ public class ScrollQueryServiceTest {
elasticSearchResponse = createSearchResponse(1, null);
doReturn(elasticSearchResponse).when(this.sut).makeSearchRequest(any(), any());
doReturn(hits).when(this.sut).getHitsFromSearchResponse(any());
doReturn(hits).when(queryResponseUtil).getQueryResponseResults(any());
doReturn(null).when(this.sut).refreshCursorCache(any(), any());
CursorQueryResponse queryResponse = this.sut.queryIndex(cursorQueryRequest);
......
......@@ -34,6 +34,7 @@ import org.opengroup.osdu.search.cache.CursorCache;
import org.opengroup.osdu.search.logging.AuditLogger;
import org.opengroup.osdu.search.provider.interfaces.IScrollQueryService;
import org.opengroup.osdu.search.util.ElasticClientHandler;
import org.opengroup.osdu.search.util.QueryResponseUtil;
import org.springframework.stereotype.Service;
import com.google.common.base.Strings;
......@@ -52,6 +53,8 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
private CursorCache cursorCache;
@Inject
private AuditLogger auditLogger;
@Inject
private QueryResponseUtil queryResponseUtil;
private final MessageDigest digest;
......@@ -118,7 +121,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
if (results != null) {
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.results(queryResponseUtil.getQueryResponseResults(results))
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
......
......@@ -43,6 +43,7 @@ import org.opengroup.osdu.search.cache.CursorCache;
import org.opengroup.osdu.search.logging.AuditLogger;
import org.opengroup.osdu.search.provider.interfaces.IScrollQueryService;
import org.opengroup.osdu.search.util.ElasticClientHandler;
import org.opengroup.osdu.search.util.QueryResponseUtil;
import org.springframework.stereotype.Service;
@Service
......@@ -56,6 +57,8 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
private CursorCache cursorCache;
@Inject
private AuditLogger auditLogger;
@Inject
private QueryResponseUtil queryResponseUtil;
private final MessageDigest digest;
......@@ -116,7 +119,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
if (results != null) {
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.results(queryResponseUtil.getQueryResponseResults(results))
.totalCount(searchResponse.getHits().getTotalHits())
.build();
}
......
......@@ -24,7 +24,6 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.indexer.OperationType;
import org.opengroup.osdu.core.common.model.policy.BatchPolicyResponse;
import org.opengroup.osdu.core.common.model.policy.PolicyRequest;
import org.opengroup.osdu.core.common.model.policy.PolicyResponse;
import org.opengroup.osdu.core.common.model.storage.Record;
import org.opengroup.osdu.core.common.model.storage.RecordMetadata;
import org.opengroup.osdu.core.common.policy.IPolicyFactory;
......
package org.opengroup.osdu.search.service.policy.service;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.policy.PolicyStatus;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.opengroup.osdu.core.common.partition.Property;
import org.opengroup.osdu.search.policy.cache.PolicyCache;
import org.opengroup.osdu.search.policy.service.IPartitionService;
import org.opengroup.osdu.search.policy.service.PartitionPolicyStatusService;
import java.util.HashMap;
import java.util.Map;
@RunWith(MockitoJUnitRunner.class)
public class PartitionPolicyStatusServiceTest {
@Mock
private IPartitionService partitionService;
@Mock
private PolicyCache cache;
@InjectMocks
private PartitionPolicyStatusService sut;
private String dataPartition = "myPartition";
@Test
public void returnFalseIfPartitionServiceIsNull() {
PartitionPolicyStatusService partitionPolicyStatusService = new PartitionPolicyStatusService();
Assert.assertFalse(partitionPolicyStatusService.policyEnabled(dataPartition));
}
@Test
public void returnEnabledFlagFromCache() {
PolicyStatus policyStatus = PolicyStatus.builder().build();
policyStatus.setEnabled(true);
Mockito.when(cache.containsKey(dataPartition+"-policy")).thenReturn(true);
Mockito.when(cache.get(dataPartition+"-policy")).thenReturn(policyStatus);
Assert.assertTrue(sut.policyEnabled(dataPartition));
}
@Test
public void returneEnabledFlagFromPartitionInfo() {
PartitionInfo partitionInfo = PartitionInfo.builder().build();
Map<String, Property> properties = new HashMap<>();
Property policyProperty = Property.builder().build();
policyProperty.setSensitive(false);
policyProperty.setValue("true");
properties.put("policy-service-enabled", policyProperty);
partitionInfo.setProperties(properties);
Mockito.when(cache.containsKey(dataPartition+"-policy")).thenReturn(false);
Mockito.when(partitionService.getPartition("myPartition")).thenReturn(partitionInfo);
Assert.assertTrue(sut.policyEnabled(dataPartition));
}
}
package org.opengroup.osdu.search.service.policy.service;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.partition.IPartitionFactory;
import org.opengroup.osdu.core.common.partition.IPartitionProvider;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.opengroup.osdu.core.common.partition.PartitionInfo;
import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient;
import org.opengroup.osdu.search.policy.service.PartitionServiceImpl;
import static org.mockito.Matchers.any;
@RunWith(MockitoJUnitRunner.class)
public class PartitionServiceImplTest {
@Mock
private DpsHeaders headers;
@Mock
private IPartitionFactory factory;
@Mock
private IServiceAccountJwtClient tokenService;
@Spy
private IPartitionProvider partitionServiceClient;
@InjectMocks
private PartitionServiceImpl sut;
@Test
public void getPartitionInfoTest() throws PartitionException {
String partitionId = "myPartition";
PartitionInfo partitionInfo = PartitionInfo.builder().build();
Mockito.when(headers.getPartitionId()).thenReturn(partitionId);
Mockito.when(tokenService.getIdToken(any())).thenReturn("myToken");
Mockito.when(factory.create(any())).thenReturn(partitionServiceClient);
Mockito.when(partitionServiceClient.get(partitionId)).thenReturn(partitionInfo);
Assert.assertNotNull(sut.getPartition(partitionId));
}
}
package org.opengroup.osdu.search.service.policy.service;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.entitlements.Groups;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.indexer.OperationType;
import org.opengroup.osdu.core.common.model.policy.BatchPolicyResponse;
import org.opengroup.osdu.core.common.model.policy.BatchResult;
import org.opengroup.osdu.core.common.model.policy.PolicyRequest;
import org.opengroup.osdu.core.common.model.storage.Record;
import org.opengroup.osdu.core.common.model.storage.RecordMetadata;
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.policy.model.SearchPolicy;
import org.opengroup.osdu.search.policy.service.PolicyServiceImpl;
import org.opengroup.osdu.search.service.IEntitlementsExtensionService;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
@RunWith(MockitoJUnitRunner.class)
public class PolicyServiceImplTest {
@Mock
private DpsHeaders headers;
@Mock
private IPolicyFactory policyFactory;
@Mock
private IEntitlementsExtensionService entitlementsService;
@Mock
private PolicyServiceConfiguration policyServiceConfiguration;
@Spy
private IPolicyProvider serviceClient;
@InjectMocks
private PolicyServiceImpl sut;
@Test
public void evaluateBatchPolicyTest() throws PolicyException {
PolicyRequest policyRequest = new PolicyRequest();
BatchPolicyResponse batchPolicyResponse = new BatchPolicyResponse();
policyRequest.setPolicyId("search");
policyRequest.setInput(setPolicyInput());
Mockito.when(policyFactory.create(any())).thenReturn(serviceClient);
Mockito.when(serviceClient.evaluateBatchPolicy(policyRequest)).thenReturn(batchPolicyResponse);
Assert.assertNotNull(sut.evaluateBatchPolicy(policyRequest));
}
@Test
public void evaluateSearchDataAuthorizationPolicyTest() throws PolicyException{
List<RecordMetadata> recordMetadataList = new ArrayList<>();
RecordMetadata recordMetadata = new RecordMetadata();
recordMetadata.setId("id:123");
recordMetadata.setKind("kind:123");
recordMetadata.setModifyUser("user123");
recordMetadataList.add(recordMetadata);
Mockito.when(policyFactory.create(any())).thenReturn(serviceClient);
BatchPolicyResponse batchPolicyResponse = new BatchPolicyResponse();
BatchResult batchResult = new BatchResult();
List<String> allowedRecords = new ArrayList<>();
allowedRecords.add("id:123");
batchResult.setAllowed_records(allowedRecords);
batchPolicyResponse.setResult(batchResult);
Mockito.when(serviceClient.evaluateBatchPolicy(any())).thenReturn(batchPolicyResponse);
Groups groups = new Groups();
Mockito.when(entitlementsService.getGroups(any())).thenReturn(groups);
Mockito.when(policyServiceConfiguration.getPolicyId()).thenReturn("search");
List<String> result = sut.evaluateSearchDataAuthorizationPolicy(recordMetadataList, OperationType.view);
Assert.assertEquals(1, result.size());
Assert.assertEquals("id:123", result.get(0));
}
private JsonObject setPolicyInput() {
SearchPolicy searchPolicy = new SearchPolicy();
List<String> groups = new ArrayList<>();
groups.add("group1@abc");
searchPolicy.setGroups(groups);
searchPolicy.setOperation(OperationType.view);
List<Record> records = new ArrayList<>();
Record record = new Record();
record.setId("id:123");
record.setKind("kind:123");
records.add(record);
searchPolicy.setRecords(records);
return new JsonParser().parse(new Gson().toJson(searchPolicy)).getAsJsonObject();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment