Commit 605e95f3 authored by neelesh thakur's avatar neelesh thakur
Browse files

Merge branch 'issue_26' into 'master'

use friendly topic name

See merge request !75
parents b4803fcb 0cc5541d
Pipeline #30736 passed with stages
in 26 minutes and 21 seconds
......@@ -30,7 +30,7 @@
<version>0.6.0-SNAPSHOT</version>
<properties>
<osdu.register-core.version>0.6.0-SNAPSHOT</osdu.register-core.version>
<osdu.corelibazure.version>0.0.60</osdu.corelibazure.version>
<osdu.corelibazure.version>0.6.1</osdu.corelibazure.version>
<junit-jupiter.version>5.6.0</junit-jupiter.version>
<mockito-junit-jupiter.version>2.23.0</mockito-junit-jupiter.version>
</properties>
......
......@@ -25,6 +25,7 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.di.CosmosContainerConfig;
import org.opengroup.osdu.register.provider.azure.util.CryptographyUtil;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.opengroup.osdu.register.provider.interfaces.subscriber.ISubscriptionRepository;
import org.opengroup.osdu.register.subscriber.model.GsaSecret;
import org.opengroup.osdu.register.subscriber.model.GsaSecretValue;
......@@ -60,9 +61,15 @@ public class SubscriptionRepository implements ISubscriptionRepository {
@Autowired
private CryptographyUtil cryptographyUtil;
@Autowired
private PartitionHelper partitionHelper;
@Autowired
private PushSubscription pushSubscription;
@Autowired
private TopicsRepositoryImpl topicsRepository;
/**
* @param input Subscription Object
* @return Subscription Object
......@@ -92,10 +99,9 @@ public class SubscriptionRepository implements ISubscriptionRepository {
@Override
public Subscription create(Subscription input) {
SubscriptionDoc doc = new SubscriptionDoc(input, dpsHeaders.getPartitionId());
String encryptedSecret = cryptographyUtil.encryptData(input.getSecret().toString());
doc.setSecretValue(encryptedSecret);
doc.setTopic(partitionHelper.getFullyQualifiedTopicName(input.getTopic()));
doc.setSecretValue(cryptographyUtil.encryptData(input.getSecret().toString()));
doc.setKeyName(cryptographyUtil.getKeyName());
doc.setKeyVersion(cryptographyUtil.getKeyVersion());
......@@ -306,7 +312,7 @@ public class SubscriptionRepository implements ISubscriptionRepository {
hmacSecret.setValue(secretValue);
secret = hmacSecret;
}
return new Subscription(doc.getId(), doc.getName(),doc.getDescription(), doc.getTopic(), doc.getPushEndpoint(),
return new Subscription(doc.getId(), doc.getName(),doc.getDescription(), topicsRepository.getTopicAliasFromFullyQualifiedTopicName(doc.getTopic()), doc.getPushEndpoint(),
doc.getCreatedBy(), doc.getCreatedOnEpoch(), doc.getNotificationId(), secret);
}
}
......@@ -6,13 +6,13 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.notification.Topic;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.opengroup.osdu.register.provider.interfaces.subscriber.ITopicsRepository;
import org.opengroup.osdu.register.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
......@@ -21,33 +21,43 @@ import com.google.gson.reflect.TypeToken;
@Repository
@Primary
@RequiredArgsConstructor
public class TopicsRepositoryImpl implements ITopicsRepository {
@Autowired
private JaxRsDpsLog log;
@Autowired
private DpsHeaders headers;
@Autowired
private PartitionHelper partitionHelper;
private final JaxRsDpsLog log;
private final DpsHeaders headers;
private final PartitionHelper partitionHelper;
private Map<String, String> fullyQualifiedNameToAliasMap;
private Map<String, List<Topic>> topicsMap = new HashMap<>();
public List<Topic> listMessages() {
String partitionId = headers.getPartitionId();
final String partitionId = headers.getPartitionId();
if (topicsMap.containsKey(partitionId)) {
return topicsMap.get(partitionId);
}
Type type = new TypeToken<ArrayList<Topic>>() {}.getType();
List<Topic> topics = new Gson().fromJson(FileUtil.readFile("topics.json", log), type);
topics.set(0, getTopic(topics));
topicsMap.put(partitionId, topics);
return topics;
final List<Topic> template = getTopicTemplate();
topicsMap.put(partitionId, template);
return template;
}
private Topic getTopic(List<Topic> topics) {
Topic topic = topics.get(0);
topic.setName(partitionHelper.retrieveTopicName());
return topic;
public String getTopicAliasFromFullyQualifiedTopicName(String fullyQualifiedTopicName) {
if (fullyQualifiedNameToAliasMap == null) {
fullyQualifiedNameToAliasMap = new HashMap<>();
List<Topic> topicList = listMessages();
for (Topic topicIterator : topicList) {
fullyQualifiedNameToAliasMap.put(partitionHelper.getFullyQualifiedTopicName(topicIterator.getName()), topicIterator.getName());
}
}
return fullyQualifiedNameToAliasMap.get(fullyQualifiedTopicName);
}
List<Topic> getTopicTemplate() {
Type type = new TypeToken<ArrayList<Topic>>() {
}.getType();
return new Gson().fromJson(FileUtil.readFile("topics.json", log), type);
}
}
......@@ -14,38 +14,55 @@
package org.opengroup.osdu.register.provider.azure.util;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.azure.partition.EventGridTopicPartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.azure.partition.PartitionServiceEventGridClient;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.partition.PartitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
public class PartitionHelper {
@Autowired
private PartitionServiceClient partitionService;
@Autowired
private DpsHeaders headers;
public String retrieveResourceGroup() {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getEventGridResourceGroup();
}
public String retrieveTopicName() {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getEventGridRecordsTopicEndpoint().split("\\.")[0].replace("https://", "");
}
public String retrieveKeyIdentifier() {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getCryptographyEncryptionKeyIdentifier();
}
public String retrieveScope(String topic) {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s",
partition.getAzureSubscriptionId(), partition.getEventGridResourceGroup(), topic);
}
@Autowired
private PartitionServiceClient partitionService;
@Autowired
private PartitionServiceEventGridClient partitionServiceEventGridClient;
@Autowired
private DpsHeaders headers;
public String retrieveResourceGroup() {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getEventGridResourceGroup();
}
public String getFullyQualifiedTopicName(String topicNameAlias) {
EventGridTopicPartitionInfoAzure eventGridTopicPartitionInfoAzure = null;
try {
eventGridTopicPartitionInfoAzure = partitionServiceEventGridClient.getEventGridTopicInPartition(headers.getPartitionId(), topicNameAlias);
} catch (PartitionException e) {
throw new AppException(
HttpStatus.SC_NOT_FOUND, "Not found", String.format("Invalid topic for subscription: %s, please use GET topics API for available topics", topicNameAlias), e);
}
return eventGridTopicPartitionInfoAzure.getTopicName().split("\\.")[0].replace("https://", "");
}
public String retrieveKeyIdentifier() {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getCryptographyEncryptionKeyIdentifier();
}
public String retrieveScope(String topic) {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return String.format("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.EventGrid/topics/%s",
partition.getAzureSubscriptionId(), partition.getEventGridResourceGroup(), topic);
}
}
[
{
"name": "topic-name",
"name": "recordstopic",
"description": "This notification is sent whenever a new record or record version is created, updated or deleted, and when a new schema is created in storage.",
"state": "ACTIVE",
"example": [
......@@ -27,4 +27,4 @@
}
]
}
]
\ No newline at end of file
]
......@@ -28,6 +28,7 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.di.CosmosContainerConfig;
import org.opengroup.osdu.register.provider.azure.util.CryptographyUtil;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import org.opengroup.osdu.register.subscriber.model.GsaSecret;
import org.opengroup.osdu.register.subscriber.model.GsaSecretValue;
import org.opengroup.osdu.register.subscriber.model.HmacSecret;
......@@ -39,20 +40,10 @@ import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
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 static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class SubscriptionRepositoryTest {
......@@ -93,6 +84,12 @@ public class SubscriptionRepositoryTest {
@Mock
private SubscriptionDoc subscriptionDoc;
@Mock
private PartitionHelper partitionHelper;
@Mock
private TopicsRepositoryImpl topicsRepository;
@InjectMocks
private SubscriptionRepository repo;
......@@ -105,6 +102,8 @@ public class SubscriptionRepositoryTest {
lenient().when(subscription.getSecret()).thenReturn(new HmacSecret(secretValue));
lenient().when(subscription.getNotificationId()).thenReturn(subscriptionId);
lenient().when(subscription.getTopic()).thenReturn(topicName);
lenient().when(partitionHelper.getFullyQualifiedTopicName(topicName)).thenReturn(topicName);
lenient().when(topicsRepository.getTopicAliasFromFullyQualifiedTopicName(topicName)).thenReturn(topicName);
lenient().when(subscriptionDoc.getSecretType()).thenReturn(secretType);
lenient().when(subscriptionDoc.getSecretValue()).thenReturn(secretValue);
lenient().when(subscriptionDoc.getCreatedOnEpoch()).thenReturn(currentTime);
......@@ -553,7 +552,6 @@ public class SubscriptionRepositoryTest {
private void verifyCallsToSubscriptionMock() {
verify(subscription, times(1)).getName();
verify(subscription, times(1)).getDescription();
verify(subscription, times(1)).getTopic();
verify(subscription, times(1)).getPushEndpoint();
verify(subscription, times(1)).getCreatedBy();
verify(subscription, times(1)).getNotificationId();
......
package org.opengroup.osdu.register.provider.azure.subscriber;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.notification.Topic;
import org.opengroup.osdu.register.provider.azure.util.PartitionHelper;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class TopicsRepositoryImplTest {
@Mock
private JaxRsDpsLog log;
@Mock
private DpsHeaders headers;
@Mock
private PartitionHelper partitionHelper;
private TopicsRepositoryImpl topicsRepository;
@BeforeEach
void init() {
topicsRepository = new TopicsRepositoryImpl(log, headers, partitionHelper);
}
@Test
public void shouldReturnUserFriendlyTopicName() {
when(headers.getPartitionId()).thenReturn("partitionId");
List<Topic> topics = topicsRepository.listMessages();
assertEquals(2, topics.size());
List<String> expected = Arrays.asList("recordstopic", "testtopic");
assertArrayEquals(expected.toArray(), topics.stream().map(Topic::getName).toArray());
}
}
[
{
"name": "recordstopic",
"description": "This notification is sent whenever a new record or record version is created, updated or deleted, and when a new schema is created in storage.",
"state": "ACTIVE",
"example": [
{
"id": "common:abc:123",
"kind": "common:petrel:regularheightfieldsurface:1.0.0",
"op" : "create",
"recordUpdated": "false"
},
{
"id": "common:abc:124",
"kind": "common:petrel:regularheightfieldsurface:1.0.0",
"op" : "create",
"recordUpdated": "true"
},
{
"kind": "common:petrel:regularheightfieldsurface:1.0.0",
"op" : "create_schema"
},
{
"id": "common:ghi:345",
"kind": "common:petrel:regularheightfieldsurface:1.0.0",
"op" : "delete"
}
]
},
{
"name": "testtopic",
"description": "This notification is sent whenever a new record or record version is created, updated or deleted, and when a new schema is created in storage.",
"state": "ACTIVE",
"example": [
{
"id": "common:abc:123",
"kind": "common:petrel:regularheightfieldsurface:1.0.0",
"op" : "create",
"recordUpdated": "false"
},
{
"id": "common:abc:124",
"kind": "common:petrel:regularheightfieldsurface:1.0.0",
"op" : "create",
"recordUpdated": "true"
},
{
"kind": "common:petrel:regularheightfieldsurface:1.0.0",
"op" : "create_schema"
},
{
"id": "common:ghi:345",
"kind": "common:petrel:regularheightfieldsurface:1.0.0",
"op" : "delete"
}
]
}
]
Markdown is supported
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