Skip to content
Snippets Groups Projects
Commit 70e51d08 authored by Rostislav Vatolin [SLB]'s avatar Rostislav Vatolin [SLB]
Browse files

Merge branch 'rem-acc-id' into 'master'

Remove account-id header

Closes #16

See merge request !158
parents f43ad52b bd731ad1
No related branches found
No related tags found
1 merge request!158Remove account-id header
Pipeline #79070 passed with warnings
Showing
with 131 additions and 67 deletions
......@@ -25,10 +25,10 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/jobs")
public class LegalTagStatusJobApi {
@Inject
private RequestInfo requestInfo;
@Inject
private LegalTagStatusJob legalTagStatusJob;
......@@ -37,7 +37,7 @@ public class LegalTagStatusJobApi {
@Inject
private AuditLogger auditLogger;
@Inject
private JaxRsDpsLog log;
......@@ -50,21 +50,20 @@ public class LegalTagStatusJobApi {
boolean allPassed = true;
for (TenantInfo tenantInfo : tenantsInfo) {
convertedHeaders.put(DpsHeaders.ACCOUNT_ID, tenantInfo.getName());
boolean result = runJob(convertedHeaders, tenantInfo, legalTagStatusJob);
if (allPassed)
if (allPassed) {
allPassed = result;
}
}
HttpStatus status = allPassed ? HttpStatus.NO_CONTENT : HttpStatus.INTERNAL_SERVER_ERROR;
return new ResponseEntity<HttpStatus>(status);
return new ResponseEntity<>(status);
}
private boolean runJob(DpsHeaders convertedHeaders, TenantInfo tenantInfo, LegalTagStatusJob legalTagStatusJob) {
boolean success = true;
try {
String projectId = requestInfo.getTenantInfo().getProjectId();
StatusChangedTags result = legalTagStatusJob.run(projectId, convertedHeaders, tenantInfo.getName());
StatusChangedTags result = legalTagStatusJob.run(tenantInfo.getProjectId(), convertedHeaders, tenantInfo.getName());
auditLogger.legalTagJobRanSuccess(singletonList(result.toString()));
} catch (Exception e) {
success = false;
......
......@@ -42,7 +42,7 @@ public class LegalTagCountriesTenantRepositories {
private AppException invalidTenantGivenException(String tenantName){
log.warning(String.format("Requested tenantname does not exist in list of tenants %s", tenantName));
return new AppException(403, "Forbidden", String.format("You do not have access to the %s value given %s",
DpsHeaders.ACCOUNT_ID, tenantName));
return new AppException(403, "Forbidden", String.format("You do not have access to the %s, value given %s",
DpsHeaders.DATA_PARTITION_ID, tenantName));
}
}
package org.opengroup.osdu.legal.api;
import org.junit.Assert;
import org.mockito.Mockito;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTag;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.legal.jobs.LegalTagCompliance;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.opengroup.osdu.legal.jobs.LegalTagStatusJob;
import org.opengroup.osdu.core.common.model.legal.StatusChangedTags;
import org.opengroup.osdu.legal.logging.AuditLogger;
import org.opengroup.osdu.legal.tags.LegalTagService;
import org.opengroup.osdu.core.common.model.http.RequestInfo;
import org.opengroup.osdu.core.common.model.legal.ServiceConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -20,23 +22,15 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Collections;
@RunWith(MockitoJUnitRunner.class)
public class LegalTagStatusJobApiTests {
@Mock
private RequestInfo requestInfo;
public class LegalTagStatusJobApiTest {
public static final DpsHeaders dpsHeaders = new DpsHeaders();
@Mock
private LegalTagService legalTagService;
private RequestInfo requestInfo;
@Mock
private LegalTagStatusJob legalTagStatusJob;
......@@ -55,48 +49,36 @@ public class LegalTagStatusJobApiTests {
@Before
public void setup() {
when(tenantStorageFactory.listTenantInfo()).thenReturn(new ArrayList<TenantInfo>() {{
add(new TenantInfo());
}});
when(requestInfo.getHeaders()).thenReturn(new DpsHeaders());
lenient().when(requestInfo.getUser()).thenReturn(ServiceConfig.LEGAL_CRON);
when(requestInfo.getTenantInfo()).thenReturn(new TenantInfo());
}
@Test
public void should_return200_when_checkUpdateStatusSucceeds() throws Exception {
when(legalTagStatusJob.run(any(), any(), any())).thenReturn(new StatusChangedTags());
ResponseEntity<HttpStatus> result = sut.checkLegalTagStatusChanges();
assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode());
dpsHeaders.put("data-partition-id", "common");
Mockito.when(requestInfo.getHeaders()).thenReturn(dpsHeaders);
TenantInfo tenantInfo = new TenantInfo();
tenantInfo.setName("tenantName");
tenantInfo.setProjectId("projectId");
Mockito.when(tenantStorageFactory.listTenantInfo()).thenReturn(Collections.singletonList(tenantInfo));
}
@Test
public void should_logAudit_when_checkUpdateStatusSucceeds() throws Exception {
when(legalTagStatusJob.run(any(), any(), any())).thenReturn(new StatusChangedTags());
public void shouldReturn200WhenCheckUpdateStatusSucceeds() throws Exception {
StatusChangedTag statusChangedTag = new StatusChangedTag("testTag", LegalTagCompliance.incompliant);
StatusChangedTags statusChangedTags = new StatusChangedTags();
statusChangedTags.getStatusChangedTags().add(statusChangedTag);
Mockito.when(legalTagStatusJob.run("projectId", dpsHeaders, "tenantName")).thenReturn(statusChangedTags);
sut.checkLegalTagStatusChanges();
ResponseEntity<HttpStatus> result = sut.checkLegalTagStatusChanges();
verify(auditLogger, times(1)).legalTagJobRanSuccess(singletonList(new StatusChangedTags().toString()));
Assert.assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode());
Mockito.verify(auditLogger).legalTagJobRanSuccess(Collections.singletonList(statusChangedTags.toString()));
}
@Test
public void should_return500_when_checkUpdateStatusThrowsAnErrorOnAnyTenant() throws Exception {
when(legalTagStatusJob.run(any(), any(), any())).thenThrow(new Exception()).thenReturn(new StatusChangedTags());
public void shouldReturn500WhenCheckUpdateStatusThrowsAnError() throws Exception {
Exception exception = new Exception("error occurred");
Mockito.when(legalTagStatusJob.run("projectId", dpsHeaders, "tenantName")).thenThrow(exception);
ResponseEntity<HttpStatus> result = sut.checkLegalTagStatusChanges();
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode());
}
@Test
public void should_return200_when_isNotACronButIsUsingHttps() throws Exception {
// when(requestInfo.getUser()).thenReturn("anotheruser");
// when(requestInfo.isHttps()).thenReturn(true);
when(legalTagStatusJob.run(any(), any(), any())).thenReturn(new StatusChangedTags());
ResponseEntity<HttpStatus> result = sut.checkLegalTagStatusChanges();
assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode());
Assert.assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode());
Mockito.verify(log).error("Error running check LegalTag compliance job on tenant common", exception);
}
}
......@@ -424,7 +424,6 @@ public class LegalTagServiceTests {
Map<String, String> headers = new HashMap<>();
headers.put(DpsHeaders.USER_EMAIL, "ash");
headers.put(DpsHeaders.CORRELATION_ID, "123");
headers.put(DpsHeaders.ACCOUNT_ID, "SIS-INTERNAL-HQ");
DpsHeaders standardHeaders = DpsHeaders.createFromMap(headers);
......
......@@ -61,9 +61,6 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
final int BATCH_SIZE = 50;
// attributes
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();
messageAttributes.put(DpsHeaders.ACCOUNT_ID, new MessageAttributeValue()
.withDataType("String")
.withStringValue(headers.getPartitionIdWithFallbackToAccountId()));
messageAttributes.put(DpsHeaders.DATA_PARTITION_ID, new MessageAttributeValue()
.withDataType("String")
.withStringValue(headers.getPartitionIdWithFallbackToAccountId()));
......
......@@ -45,6 +45,6 @@ public class LegalTagRepositoryFactoryAwsImpl implements ILegalTagRepositoryFact
AppException invalidTenantGivenException(String tenantName) {
return new AppException(403, "Forbidden",
String.format("You do not have access to the %s value given %s",
DpsHeaders.ACCOUNT_ID, tenantName));
DpsHeaders.DATA_PARTITION_ID, tenantName));
}
}
......@@ -61,14 +61,14 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
private String serviceBusTopic;
@Override
public void publish(String projectId, DpsHeaders headers, StatusChangedTags tags) throws Exception {
publishToServiceBus(projectId, headers, tags);
public void publish(String projectId, DpsHeaders headers, StatusChangedTags tags) {
publishToServiceBus(headers, tags);
if (eventGridConfig.isPublishingToEventGridEnabled()) {
publishToEventGrid(headers, tags);
}
}
private void publishToServiceBus(String projectId, DpsHeaders headers, StatusChangedTags tags) {
private void publishToServiceBus(DpsHeaders headers, StatusChangedTags tags) {
Message message = createMessage(headers, tags);
try {
logger.debug("Storage publishes message " + headers.getCorrelationId());
......@@ -85,8 +85,7 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
HashMap<String, Object> data = new HashMap<>();
List<EventGridEvent> eventsList = new ArrayList<>();
data.put("data", tags);
data.put(DpsHeaders.ACCOUNT_ID, headers.getPartitionIdWithFallbackToAccountId());
data.put(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionIdWithFallbackToAccountId());
data.put(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionId());
data.put(DpsHeaders.CORRELATION_ID, headers.getCorrelationId());
data.put(DpsHeaders.USER_EMAIL, headers.getUserEmail());
String messageId = UUID.randomUUID().toString();
......@@ -113,8 +112,7 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
Map<String, Object> properties = new HashMap<>();
// properties
properties.put(DpsHeaders.ACCOUNT_ID, headers.getPartitionIdWithFallbackToAccountId());
properties.put(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionIdWithFallbackToAccountId());
properties.put(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionId());
headers.addCorrelationIdIfMissing();
properties.put(DpsHeaders.CORRELATION_ID, headers.getCorrelationId());
properties.put(DpsHeaders.USER_EMAIL, headers.getUserEmail());
......@@ -124,8 +122,7 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
// add all to body {"message": {"data":[], "id":...}}
JsonObject jo = new JsonObject();
jo.add("data", gson.toJsonTree(tags));
jo.addProperty(DpsHeaders.ACCOUNT_ID, headers.getPartitionIdWithFallbackToAccountId());
jo.addProperty(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionIdWithFallbackToAccountId());
jo.addProperty(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionId());
jo.addProperty(DpsHeaders.CORRELATION_ID, headers.getCorrelationId());
jo.addProperty(DpsHeaders.USER_EMAIL, headers.getUserEmail());
JsonObject jomsg = new JsonObject();
......
......@@ -21,12 +21,14 @@ import com.microsoft.azure.servicebus.Message;
import com.microsoft.azure.servicebus.MessageBody;
import com.microsoft.azure.servicebus.TopicClient;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.azure.eventgrid.EventGridTopicStore;
import org.opengroup.osdu.azure.servicebus.ITopicClientFactory;
......@@ -38,13 +40,9 @@ import org.opengroup.osdu.legal.azure.di.EventGridConfig;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class LegalTagPublisherImplTest {
private static final String DATA_PARTITION_WITH_FALLBACK_ACCOUNT_ID = "data-partition-account-id";
private static final String CORRELATION_ID = "correlation-id";
private static final String USER_EMAIL = "user@email.com";
private static final String PARTITION_ID = "partition-id";
......@@ -72,53 +70,51 @@ public class LegalTagPublisherImplTest {
@Before
public void init() throws ServiceBusException, InterruptedException {
doReturn(DATA_PARTITION_WITH_FALLBACK_ACCOUNT_ID).when(headers).getPartitionIdWithFallbackToAccountId();
doReturn(CORRELATION_ID).when(headers).getCorrelationId();
doReturn(USER_EMAIL).when(headers).getUserEmail();
doReturn(PARTITION_ID).when(headers).getPartitionId();
doReturn(topicClient).when(topicClientFactory).getClient(eq(PARTITION_ID), any());
Mockito.doReturn(CORRELATION_ID).when(headers).getCorrelationId();
Mockito.doReturn(USER_EMAIL).when(headers).getUserEmail();
Mockito.doReturn(PARTITION_ID).when(headers).getPartitionId();
Mockito.doReturn(topicClient).when(topicClientFactory).getClient(Mockito.eq(PARTITION_ID), Mockito.any());
}
@Test
public void should_publishToEventGrid_WhenFlagIsSet() throws Exception {
public void shouldPublishToEventGridWhenFlagIsSet() throws Exception {
StatusChangedTags tags = new StatusChangedTags();
ArgumentCaptor<String> partitionNameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> topicNameArgumentCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<List<EventGridEvent>> listEventGridEventArgumentCaptor = ArgumentCaptor.forClass(List.class);
doNothing().when(this.eventGridTopicStore).publishToEventGridTopic(
Mockito.doNothing().when(this.eventGridTopicStore).publishToEventGridTopic(
partitionNameCaptor.capture(), topicNameArgumentCaptor.capture(), listEventGridEventArgumentCaptor.capture()
);
when(this.eventGridConfig.isPublishingToEventGridEnabled()).thenReturn(true);
when(this.eventGridConfig.getTopicName()).thenReturn("legaltagschangedtopic");
Mockito.when(this.eventGridConfig.isPublishingToEventGridEnabled()).thenReturn(true);
Mockito.when(this.eventGridConfig.getTopicName()).thenReturn("legaltagschangedtopic");
sut.publish("project-id", headers, tags);
verify(this.eventGridTopicStore, times(1)).publishToEventGridTopic(any(), any(), anyList());
Mockito.verify(this.eventGridTopicStore, Mockito.times(1))
.publishToEventGridTopic(Mockito.any(), Mockito.any(), Mockito.anyList());
assertEquals(1, listEventGridEventArgumentCaptor.getValue().size());
assertEquals(topicNameArgumentCaptor.getValue(), "legaltagschangedtopic");
assertEquals(partitionNameCaptor.getValue(), PARTITION_ID);
Assert.assertEquals(1, listEventGridEventArgumentCaptor.getValue().size());
Assert.assertEquals(topicNameArgumentCaptor.getValue(), "legaltagschangedtopic");
Assert.assertEquals(partitionNameCaptor.getValue(), PARTITION_ID);
}
@Test
public void testPublishLegalTag() throws Exception {
public void shouldPublishLegalTag() throws Exception {
StatusChangedTags tags = new StatusChangedTags();
sut.publish("project-id", headers, tags);
ArgumentCaptor<Message> msg = ArgumentCaptor.forClass(Message.class);
ArgumentCaptor<String> log = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Exception> exception = ArgumentCaptor.forClass(Exception.class);
verify(logger).debug(log.capture());
assertEquals("Storage publishes message " + CORRELATION_ID, log.getValue());
Mockito.verify(logger).debug(log.capture());
Assert.assertEquals("Storage publishes message " + CORRELATION_ID, log.getValue());
verify(topicClient).send(msg.capture());
Mockito.verify(topicClient).send(msg.capture());
Map<String, Object> properties = msg.getValue().getProperties();
assertEquals(DATA_PARTITION_WITH_FALLBACK_ACCOUNT_ID, properties.get(DpsHeaders.ACCOUNT_ID));
assertEquals(DATA_PARTITION_WITH_FALLBACK_ACCOUNT_ID, properties.get(DpsHeaders.DATA_PARTITION_ID));
assertEquals(CORRELATION_ID, properties.get(DpsHeaders.CORRELATION_ID));
assertEquals(USER_EMAIL, properties.get(DpsHeaders.USER_EMAIL));
Assert.assertEquals(PARTITION_ID, properties.get(DpsHeaders.DATA_PARTITION_ID));
Assert.assertEquals(CORRELATION_ID, properties.get(DpsHeaders.CORRELATION_ID));
Assert.assertEquals(USER_EMAIL, properties.get(DpsHeaders.USER_EMAIL));
MessageBody messageBody = msg.getValue().getMessageBody();
Gson gson = new Gson();
......@@ -126,10 +122,9 @@ public class LegalTagPublisherImplTest {
String dataKey = "data";
JsonObject jsonObjectMessage = gson.fromJson(new String(messageBody.getBinaryData().get(0)), JsonObject.class);
JsonObject jsonObject = (JsonObject) jsonObjectMessage.get(messageKey);
assertEquals(DATA_PARTITION_WITH_FALLBACK_ACCOUNT_ID, jsonObject.get(DpsHeaders.ACCOUNT_ID).getAsString());
assertEquals(DATA_PARTITION_WITH_FALLBACK_ACCOUNT_ID, jsonObject.get(DpsHeaders.DATA_PARTITION_ID).getAsString());
assertEquals(CORRELATION_ID, jsonObject.get(DpsHeaders.CORRELATION_ID).getAsString());
assertEquals(USER_EMAIL, jsonObject.get(DpsHeaders.USER_EMAIL).getAsString());
assertEquals(gson.toJsonTree(tags), jsonObject.get(dataKey));
Assert.assertEquals(PARTITION_ID, jsonObject.get(DpsHeaders.DATA_PARTITION_ID).getAsString());
Assert.assertEquals(CORRELATION_ID, jsonObject.get(DpsHeaders.CORRELATION_ID).getAsString());
Assert.assertEquals(USER_EMAIL, jsonObject.get(DpsHeaders.USER_EMAIL).getAsString());
Assert.assertEquals(gson.toJsonTree(tags), jsonObject.get(dataKey));
}
}
......@@ -74,7 +74,6 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
ByteString statusChangedTagsData = ByteString.copyFromUtf8(statusChangedTagsJson.toString());
PubsubMessage.Builder builder = PubsubMessage.newBuilder();
builder.putAttributes(DpsHeaders.ACCOUNT_ID, headers.getPartitionIdWithFallbackToAccountId());
builder.putAttributes(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionIdWithFallbackToAccountId());
builder.putAttributes(DpsHeaders.CORRELATION_ID, headers.getCorrelationId());
builder.putAttributes(DpsHeaders.USER_EMAIL, headers.getUserEmail());
......
......@@ -74,7 +74,7 @@ public class LegalTagRepositoryFactoryGcpImpl implements ILegalTagRepositoryFact
AppException invalidTenantGivenException(String tenantName) {
return new AppException(403, "Forbidden",
String.format("You do not have access to the %s value given %s",
DpsHeaders.ACCOUNT_ID, tenantName));
String.format("You do not have access to the %s, value given %s",
DpsHeaders.DATA_PARTITION_ID, tenantName));
}
}
......@@ -39,7 +39,7 @@ public class LegalTagPublisherImplTests {
headers.put(DpsHeaders.USER_EMAIL, "ash");
headers.put(DpsHeaders.CORRELATION_ID, "123");
headers.put(DpsHeaders.ACCOUNT_ID, "tenant1");
headers.put(DpsHeaders.DATA_PARTITION_ID, "tenant1");
}
@Test
......@@ -52,10 +52,6 @@ public class LegalTagPublisherImplTests {
String data = capturedMessage.toString();
assertEquals("data: \"{\\\"statusChangedTags\\\":[]}\"\n" +
"attributes {\n" +
" key: \"account-id\"\n" +
" value: \"tenant1\"\n" +
"}\n" +
"attributes {\n" +
" key: \"data-partition-id\"\n" +
" value: \"tenant1\"\n" +
......
......@@ -41,7 +41,6 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
statusChangedTags.appendField("statusChangedTags", batch);
String json = gson.toJson(statusChangedTags);
message.put("data", json);
message.put(DpsHeaders.ACCOUNT_ID, headers.getPartitionIdWithFallbackToAccountId());
message.put(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionIdWithFallbackToAccountId());
headers.addCorrelationIdIfMissing();
message.put(DpsHeaders.CORRELATION_ID, headers.getCorrelationId());
......
......@@ -52,7 +52,6 @@ public class LegalTagPublisherImpl implements ILegalTagPublisher {
Math.min(tags.getStatusChangedTags().size(), i + BATCH_SIZE));
String json = gson.toJson(batch);
message.put("data", json);
message.put(DpsHeaders.ACCOUNT_ID, headers.getPartitionIdWithFallbackToAccountId());
message.put(DpsHeaders.DATA_PARTITION_ID, headers.getPartitionIdWithFallbackToAccountId());
headers.addCorrelationIdIfMissing();
message.put(DpsHeaders.CORRELATION_ID, headers.getCorrelationId());
......
......@@ -57,7 +57,7 @@ public class LegalTagRepositoryFactoryMongoImpl implements ILegalTagRepositoryFa
private AppException invalidTenantGivenException(String tenantName) {
return new AppException(403, "Forbidden",
String.format("You do not have access to the %s value given %s",
DpsHeaders.ACCOUNT_ID, tenantName));
String.format("You do not have access to the %s, value given %s",
DpsHeaders.DATA_PARTITION_ID, tenantName));
}
}
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