Commit c7be36a8 authored by Aliaksei Darafeyeu's avatar Aliaksei Darafeyeu
Browse files

use friendly topic name

parent b4803fcb
Pipeline #29908 failed with stages
in 53 seconds
......@@ -60,6 +60,9 @@ public class SubscriptionRepository implements ISubscriptionRepository {
@Autowired
private CryptographyUtil cryptographyUtil;
@Autowired
private TopicsRepositoryImpl topicsRepository;
@Autowired
private PushSubscription pushSubscription;
......@@ -92,10 +95,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(topicsRepository.topicNameResolver(input.getTopic()));
doc.setSecretValue(cryptographyUtil.encryptData(input.getSecret().toString()));
doc.setKeyName(cryptographyUtil.getKeyName());
doc.setKeyVersion(cryptographyUtil.getKeyVersion());
......
......@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
......@@ -31,23 +32,49 @@ public class TopicsRepositoryImpl implements ITopicsRepository {
private PartitionHelper partitionHelper;
private Map<String, List<Topic>> topicsMap = new HashMap<>();
private Map<String, String> topicNames = new HashMap<>();
public List<Topic> listMessages() {
String partitionId = headers.getPartitionId();
final String partitionId = headers.getPartitionId();
if (topicsMap.containsKey(partitionId)) {
return topicsMap.get(partitionId);
}
final Topic template = getTopicTemplate();
List<Topic> collect = partitionHelper.retrieveTopicNames().stream()
.map(this::extractTopicDisplayName)
.map(s -> createTopic(template, s))
.collect(Collectors.toList());
topicsMap.put(partitionId, collect);
return collect;
}
public String topicNameResolver(String name) {
return topicNames.get(headers.getPartitionId() + "_" + name);
}
private Topic getTopicTemplate() {
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;
List<Topic> templates = new Gson().fromJson(FileUtil.readFile("topics.json", log), type);
return templates.get(0);
}
private Topic getTopic(List<Topic> topics) {
Topic topic = topics.get(0);
topic.setName(partitionHelper.retrieveTopicName());
private Topic createTopic(Topic template, String topicName) {
Topic topic = new Topic();
topic.setName(topicName);
topic.setDescription(template.getDescription());
topic.setExample(template.getExample());
topic.setState(template.getState());
return topic;
}
private String extractTopicDisplayName(String topicName) {
// topicName = `{prefix}-grid-{displayName}`
//TODO: it will be ideal to adds {prifix} as value to keyvault and
// use for extracting displayName instead of hardcoded '-grid-'
String displayName = topicName.split("-grid-")[1];
topicNames.put(headers.getPartitionId() + "_" + displayName, topicName);
return displayName;
}
}
......@@ -20,6 +20,9 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
public class PartitionHelper {
......@@ -33,9 +36,10 @@ public class PartitionHelper {
return partition.getEventGridResourceGroup();
}
public String retrieveTopicName() {
public List<String> retrieveTopicNames() {
PartitionInfoAzure partition = partitionService.getPartition(headers.getPartitionId());
return partition.getEventGridRecordsTopicEndpoint().split("\\.")[0].replace("https://", "");
String topicName = partition.getEventGridRecordsTopicEndpoint().split("\\.")[0].replace("https://", "");
return Arrays.asList(topicName);
}
public String retrieveKeyIdentifier() {
......
......@@ -27,4 +27,4 @@
}
]
}
]
\ No newline at end of file
]
......@@ -93,6 +93,9 @@ public class SubscriptionRepositoryTest {
@Mock
private SubscriptionDoc subscriptionDoc;
@Mock
private TopicsRepositoryImpl topicsRepository;
@InjectMocks
private SubscriptionRepository repo;
......@@ -105,6 +108,7 @@ 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(topicsRepository.topicNameResolver(topicName)).thenReturn(topicName);
lenient().when(subscriptionDoc.getSecretType()).thenReturn(secretType);
lenient().when(subscriptionDoc.getSecretValue()).thenReturn(secretValue);
lenient().when(subscriptionDoc.getCreatedOnEpoch()).thenReturn(currentTime);
......@@ -553,7 +557,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.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
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.assertArrayEquals;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class TopicsRepositoryImplTest {
@Mock
private JaxRsDpsLog log;
@Mock
private DpsHeaders headers;
@Mock
private PartitionHelper partitionHelper;
@InjectMocks
private TopicsRepositoryImpl topicsRepository;
@Test
public void shouldReturnUserFriendlyTopicName() {
when(headers.getPartitionId()).thenReturn("partitionId");
when(partitionHelper.retrieveTopicNames())
.thenReturn(Arrays.asList("rg1-grid-recordstopic", "rg1-grid-recordstopic-function"));
List<Topic> topics = topicsRepository.listMessages();
List<String> expected = Arrays.asList("recordstopic", "recordstopic-function");
assertArrayEquals(expected.toArray(), topics.stream().map(Topic::getName).toArray());
}
}
[
{
"name": "topic-name",
"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