private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
static {
- GUARD_POLICY_TYPE_MAP.put("guard.frequency.scaleout",
+ GUARD_POLICY_TYPE_MAP.put("guard.frequency.",
new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
- GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout",
+ GUARD_POLICY_TYPE_MAP.put("guard.minmax.",
new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
- GUARD_POLICY_TYPE_MAP.put("guard.blacklist",
+ GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
}
@Override
public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
- PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId());
+ PfConceptKey guardPolicyType = getGuardPolicyType(legacyGuardPolicyInput);
if (guardPolicyType == null) {
String errorMessage =
"policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
return legacyGuardPolicyOutputMap;
}
+
+ private PfConceptKey getGuardPolicyType(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
+ final String policyId = legacyGuardPolicyInput.getPolicyId();
+ if (policyId == null) {
+ return null;
+ }
+
+ for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
+ if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
+ return guardPolicyTypeEntry.getValue();
+ }
+ }
+
+ return null;
+ }
}
assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
}
+ @Test
+ public void testPolicyCreateBad() throws Exception {
+ assertThatThrownBy(() -> {
+ new LegacyProvider().createGuardPolicy(null, null);
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput());
+ }).hasMessage("dao is marked @NonNull but is null");
+
+ assertThatThrownBy(() -> {
+ new LegacyProvider().createGuardPolicy(pfDao, null);
+ }).hasMessage("legacyGuardPolicy is marked @NonNull but is null");
+
+ createPolicyTypes();
+
+ LegacyGuardPolicyInput originalGip = standardCoder.decode(
+ ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"),
+ LegacyGuardPolicyInput.class);
+
+ assertNotNull(originalGip);
+
+ originalGip.setPolicyId("i.do.not.exist");
+
+ assertThatThrownBy(() -> {
+ new LegacyProvider().createGuardPolicy(pfDao, originalGip);
+ }).hasMessage("policy type for guard policy \"i.do.not.exist\" unknown");
+ }
+
@Test
public void testPolicyUpdate() throws Exception {
assertThatThrownBy(() -> {
}).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput();
- otherGip.setPolicyId("guard.blacklist");
+ otherGip.setPolicyId("guard.blacklist.b0");
otherGip.setPolicyVersion("1");
otherGip.setContent(new LegacyGuardPolicyContent());