Skip to content

Commit

Permalink
DATAGO-85868: Get the identifier key from attribute combinations if n…
Browse files Browse the repository at this point in the history
…ot found from attributes. (#16)

* Get the identifier key from attribute combinations if not found from attributes

* Add test to cover semp 2.39 and above

* Add comments
  • Loading branch information
evenjessie authored Dec 3, 2024
1 parent dd150e0 commit bcba0b9
Show file tree
Hide file tree
Showing 4 changed files with 36,660 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.solace.tools.solconfig.model;

import lombok.Data;

import java.util.Objects;

@Data
public class AttributeCombinationKey implements Comparable<AttributeCombinationKey>{
private String sempClassName;
private String attributeName;
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/com/solace/tools/solconfig/model/SempSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,39 @@ protected static String generateSpecPath(String path) {
}

public static String getTopResourceIdentifierKey(String resourceName) {
if (RES_ABBR.fullNames().contains(resourceName)){
return sempSpecMap.get("/"+resourceName).
getAttributeNames(AttributeType.IDENTIFYING).get(0);
}else{
if (RES_ABBR.fullNames().contains(resourceName)) {
List<String> identifyingAttributes = sempSpecMap.get("/"+resourceName)
.getAttributeNames(AttributeType.IDENTIFYING);
if (identifyingAttributes.isEmpty()) {
return getIdentifierKeyFromAttributeCombinationKeySempClassName(resourceName);
}
return identifyingAttributes.get(0);
} else {
throw new IllegalArgumentException(
String.format("%s is NOT one of [%s]!",
resourceName, RES_ABBR.fullNames()));
}
}

/* With semp v2 2.38 and up, after parsing SempSpec, the identifier is not returned in the attributes, for example
sempSpecMap ->
/clientCertAuthorities -> {SempSpec}
attributes -> All
attributeCombinations -> {AttributeCombinationKey} "AttributeCombinationKey{sempClassName='clientCertAuthorities', attributeName='certAuthorityName', type=Requires}"
So we get from AttributeCombinationKey instead
*/
private static String getIdentifierKeyFromAttributeCombinationKeySempClassName(String resourceName) {
Map<AttributeCombinationKey, List<String>> attributeCombinationKeyListMap = sempSpecMap.get("/"+resourceName).getAttributeCombinations();
if (attributeCombinationKeyListMap != null) {
return attributeCombinationKeyListMap.entrySet().stream().map(attributeCombinationKeyListEntry ->
attributeCombinationKeyListEntry.getKey()).collect(Collectors.toList()).stream().filter(r ->
AttributeCombinationKey.TYPE.Requires.equals(r.getType())).findAny().map(identifierKey -> identifierKey.getSempClassName()).orElseThrow(
() -> new IllegalArgumentException(String.format("No top resource identifier key found for %s", resourceName)));
} else {
throw new IllegalArgumentException(String.format("Cannot get top resource identifier key found for %s with no attribute combination key", resourceName));
}
}

protected static SempSpec get(String specPath) {
return sempSpecMap.get(specPath);
}
Expand Down
26 changes: 20 additions & 6 deletions src/test/java/com/solace/tools/solconfig/model/SempSpecTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.solace.tools.solconfig.model;

import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -20,10 +22,10 @@

public class SempSpecTest {
Logger log = Logger.getLogger(SempSpecTest.class.getName());
String defaultJsonPath = "/semp-v2-config-2.19.json";

@BeforeAll
static void setup() throws IOException {
var jsonString = Files.readString(Path.of(JsonSpecTest.class.getResource("/semp-v2-config-2.19.json").getPath()));
static void setup(String filePath) throws IOException {
var jsonString = Files.readString(Path.of(SempSpecTest.class.getResource(filePath).getPath()));
SempSpec.setupByString(jsonString);
}

Expand All @@ -33,28 +35,40 @@ static void setup() throws IOException {
"/msgVpns/{msgVpnName}/aclProfiles, /msgVpns/aclProfiles",
"'/msgVpns/{msgVpnName}/aclProfiles/{aclProfileName}/publishTopicExceptions/{publishTopicExceptionSyntax},{publishTopicException}', /msgVpns/aclProfiles/publishTopicExceptions"
})
@SneakyThrows
void testGenerateSpecPath(String path, String expected) {
setup(defaultJsonPath);
assertEquals(expected, SempSpec.generateSpecPath(path));
}


@Test
@SneakyThrows
void testOfJsonNode() {
setup(defaultJsonPath);
SempSpec.sempSpecMap.keySet().forEach(log::info);
}

@ParameterizedTest
@CsvSource({
"dmrClusters, dmrClusterName",
"msgVpns, msgVpnName"
"'/semp-v2-config-2.19.json', dmrClusters, dmrClusterName",
"'/semp-v2-config-2.19.json', msgVpns, msgVpnName",
"'/semp-v2-config-2.19.json', clientCertAuthorities, certAuthorityName",
"'/semp-v2-config-2.39.json', dmrClusters, dmrClusterName",
"'/semp-v2-config-2.39.json', msgVpns, msgVpnName",
"'/semp-v2-config-2.39.json', clientCertAuthorities, certAuthorityName"
})
void testGetTopResourceIdentifierKey(String topName, String expected) {
@SneakyThrows
void testGetTopResourceIdentifierKey(String filePath, String topName, String expected) {
setup(filePath);
assertEquals(expected, SempSpec.getTopResourceIdentifierKey(topName));
}

@ParameterizedTest
@MethodSource("testGetRequiresAttributeWithDefaultValueProvider")
@SneakyThrows
void testGetRequiresAttributeWithDefaultValue(String specPath, Set<String> attributes, Map<String, Object> expected){
setup(defaultJsonPath);
var sempSpec = SempSpec.sempSpecMap.get(specPath);
var result = sempSpec.getRequiresAttributeWithDefaultValue(attributes);
assertEquals(expected, result);
Expand Down
Loading

0 comments on commit bcba0b9

Please sign in to comment.