Commit 7d934f7a authored by Aliaksei Darafeyeu's avatar Aliaksei Darafeyeu
Browse files

clean code&small refactoring

parent c7be36a8
Pipeline #30604 failed with stages
in 3 minutes and 12 seconds
......@@ -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.TopicsHelper;
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;
......@@ -61,7 +62,7 @@ public class SubscriptionRepository implements ISubscriptionRepository {
private CryptographyUtil cryptographyUtil;
@Autowired
private TopicsRepositoryImpl topicsRepository;
private TopicsHelper topicsHelper;
@Autowired
private PushSubscription pushSubscription;
......@@ -96,7 +97,7 @@ public class SubscriptionRepository implements ISubscriptionRepository {
@Override
public Subscription create(Subscription input) {
SubscriptionDoc doc = new SubscriptionDoc(input, dpsHeaders.getPartitionId());
doc.setTopic(topicsRepository.topicNameResolver(input.getTopic()));
doc.setTopic(topicsHelper.nameResolver(input.getTopic()));
doc.setSecretValue(cryptographyUtil.encryptData(input.getSecret().toString()));
doc.setKeyName(cryptographyUtil.getKeyName());
doc.setKeyVersion(cryptographyUtil.getKeyVersion());
......@@ -308,7 +309,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(), topicsHelper.extractDisplayName(doc.getTopic()), doc.getPushEndpoint(),
doc.getCreatedBy(), doc.getCreatedOnEpoch(), doc.getNotificationId(), secret);
}
}
......@@ -7,13 +7,14 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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.azure.util.TopicsHelper;
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;
......@@ -22,17 +23,15 @@ 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 final TopicsHelper topicsHelper;
private Map<String, List<Topic>> topicsMap = new HashMap<>();
private Map<String, String> topicNames = new HashMap<>();
public List<Topic> listMessages() {
final String partitionId = headers.getPartitionId();
......@@ -42,7 +41,7 @@ public class TopicsRepositoryImpl implements ITopicsRepository {
final Topic template = getTopicTemplate();
List<Topic> collect = partitionHelper.retrieveTopicNames().stream()
.map(this::extractTopicDisplayName)
.map(topicsHelper::extractDisplayName)
.map(s -> createTopic(template, s))
.collect(Collectors.toList());
......@@ -50,10 +49,6 @@ public class TopicsRepositoryImpl implements ITopicsRepository {
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> templates = new Gson().fromJson(FileUtil.readFile("topics.json", log), type);
......@@ -68,13 +63,4 @@ public class TopicsRepositoryImpl implements ITopicsRepository {
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;
}
}
package org.opengroup.osdu.register.provider.azure.util;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
@RequiredArgsConstructor
public class TopicsHelper {
private final DpsHeaders headers;
private Map<String, String> topicNames = new HashMap<>();
public String nameResolver(String name) {
return topicNames.get(formatKey(name));
}
//TODO: it will be ideal to adds {prifix} as value to keyvault and
// use for extracting displayName instead of hardcoded '-grid-'
public String extractDisplayName(String topicName) {
// topicName = `{prefix}-grid-{displayName}`
String displayName = topicName.split("-grid-")[1];
topicNames.putIfAbsent(formatKey(displayName), topicName);
return displayName;
}
private String formatKey(String name) {
return headers.getPartitionId() + "_" + name;
}
}
......@@ -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.TopicsHelper;
import org.opengroup.osdu.register.subscriber.model.GsaSecret;
import org.opengroup.osdu.register.subscriber.model.GsaSecretValue;
import org.opengroup.osdu.register.subscriber.model.HmacSecret;
......@@ -94,7 +95,7 @@ public class SubscriptionRepositoryTest {
private SubscriptionDoc subscriptionDoc;
@Mock
private TopicsRepositoryImpl topicsRepository;
private TopicsHelper topicsHelper;
@InjectMocks
private SubscriptionRepository repo;
......@@ -108,7 +109,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(topicsRepository.topicNameResolver(topicName)).thenReturn(topicName);
lenient().when(topicsHelper.nameResolver(topicName)).thenReturn(topicName);
lenient().when(topicsHelper.extractDisplayName(topicName)).thenReturn(topicName);
lenient().when(subscriptionDoc.getSecretType()).thenReturn(secretType);
lenient().when(subscriptionDoc.getSecretValue()).thenReturn(secretValue);
lenient().when(subscriptionDoc.getCreatedOnEpoch()).thenReturn(currentTime);
......
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.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
......@@ -10,11 +10,12 @@ 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.azure.util.TopicsHelper;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
......@@ -29,16 +30,22 @@ public class TopicsRepositoryImplTest {
@Mock
private PartitionHelper partitionHelper;
@InjectMocks
private TopicsRepositoryImpl topicsRepository;
@BeforeEach
void init() {
topicsRepository = new TopicsRepositoryImpl(log, headers, partitionHelper, new TopicsHelper(headers));
}
@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();
assertEquals(2, topics.size());
List<String> expected = Arrays.asList("recordstopic", "recordstopic-function");
assertArrayEquals(expected.toArray(), topics.stream().map(Topic::getName).toArray());
}
......
package org.opengroup.osdu.register.provider.azure.util;
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.model.http.DpsHeaders;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TopicsHelperTest {
@Mock
private DpsHeaders headers;
@InjectMocks
private TopicsHelper topicsHelper;
@Test
public void shouldExtractTopicName() {
when(headers.getPartitionId()).thenReturn("partitionId");
assertEquals("recordstopic", topicsHelper.extractDisplayName("rg-grid-recordstopic"));
}
}
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