/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 Nordix Foundation.
+ * Copyright (C) 2019-2021 Nordix Foundation.
* Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
package org.onap.policy.models.base;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.lang.reflect.Field;
import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationResult;
public class PfReferenceKeyTest {
private static final String VERSION001 = "0.0.1";
@Test
- public void testPfReferenceKey() {
+ public void testPfReferenceKeyNotNull() {
assertNotNull(new PfReferenceKey());
assertNotNull(new PfReferenceKey(new PfConceptKey()));
assertNotNull(new PfReferenceKey(new PfConceptKey(), LOCAL_NAME));
assertThatThrownBy(() -> new PfReferenceKey(new PfConceptKey(), null))
.hasMessage("parameter \"localName\" is null");
+ }
+ @Test
+ public void testPfReferenceKey() {
PfReferenceKey testReferenceKey = new PfReferenceKey();
testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
assertEquals("PN:0.0.1", testReferenceKey.getParentConceptKey().getId());
assertEquals(PfKey.Compatibility.DIFFERENT, testReferenceKey.getCompatibility(PfReferenceKey.getNullKey()));
assertEquals(PfKey.Compatibility.IDENTICAL, testReferenceKey.getCompatibility(testReferenceKey));
- PfValidationResult result = new PfValidationResult();
- result = testReferenceKey.validate(result);
- assertEquals(PfValidationResult.ValidationResult.VALID, result.getValidationResult());
+ assertTrue(testReferenceKey.validate("").isValid());
+ }
+ @Test
+ public void testMultiplePfReferenceKey() {
+ PfReferenceKey testReferenceKey = setTestReferenceKey();
testReferenceKey.clean();
PfReferenceKey clonedReferenceKey = new PfReferenceKey(testReferenceKey);
Field parentNameField = testReferenceKey.getClass().getDeclaredField("parentKeyName");
parentNameField.setAccessible(true);
parentNameField.set(testReferenceKey, "Parent Name");
- PfValidationResult validationResult = new PfValidationResult();
- testReferenceKey.validate(validationResult);
+ ValidationResult validationResult = testReferenceKey.validate("");
parentNameField.set(testReferenceKey, "ParentName");
parentNameField.setAccessible(false);
- assertEquals(
- "parentKeyName invalid-parameter parentKeyName with value Parent Name "
- + "does not match regular expression " + PfKey.NAME_REGEXP,
- validationResult.getMessageList().get(0).getMessage());
+ assertThat(validationResult.getResult()).contains("\"parentKeyName\"")
+ .contains("does not match regular expression " + PfKey.NAME_REGEXP);
Field parentVersionField = testReferenceKey.getClass().getDeclaredField("parentKeyVersion");
parentVersionField.setAccessible(true);
parentVersionField.set(testReferenceKey, "Parent Version");
- PfValidationResult validationResult2 = new PfValidationResult();
- testReferenceKey.validate(validationResult2);
+ ValidationResult validationResult2 = testReferenceKey.validate("");
parentVersionField.set(testReferenceKey, VERSION001);
parentVersionField.setAccessible(false);
- assertEquals(
- "parentKeyVersion invalid-parameter parentKeyVersion with value Parent Version "
- + "does not match regular expression " + PfKey.VERSION_REGEXP,
- validationResult2.getMessageList().get(0).getMessage());
+ assertThat(validationResult2.getResult()).contains("\"parentKeyVersion\"")
+ .contains("does not match regular expression " + PfKey.VERSION_REGEXP);
Field parentLocalNameField = testReferenceKey.getClass().getDeclaredField("parentLocalName");
parentLocalNameField.setAccessible(true);
parentLocalNameField.set(testReferenceKey, "Parent Local Name");
- PfValidationResult validationResult3 = new PfValidationResult();
- testReferenceKey.validate(validationResult3);
+ ValidationResult validationResult3 = testReferenceKey.validate("");
parentLocalNameField.set(testReferenceKey, PARENT_LOCAL_NAME);
parentLocalNameField.setAccessible(false);
- assertEquals(
- "parentLocalName invalid-parameter parentLocalName with value "
- + "Parent Local Name does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
- validationResult3.getMessageList().get(0).getMessage());
+ assertThat(validationResult3.getResult()).contains("\"parentLocalName\"")
+ .contains("does not match regular expression [A-Za-z0-9\\-_\\.]+|^$");
Field localNameField = testReferenceKey.getClass().getDeclaredField("localName");
localNameField.setAccessible(true);
localNameField.set(testReferenceKey, "Local Name");
- PfValidationResult validationResult4 = new PfValidationResult();
- testReferenceKey.validate(validationResult4);
+ ValidationResult validationResult4 = testReferenceKey.validate("");
localNameField.set(testReferenceKey, LOCAL_NAME);
localNameField.setAccessible(false);
- assertEquals(
- "localName invalid-parameter localName with value Local Name "
- + "does not match regular expression [A-Za-z0-9\\-_\\.]+|^$",
- validationResult4.getMessageList().get(0).getMessage());
+ assertThat(validationResult4.getResult()).contains("\"localName\"")
+ .contains("does not match regular expression [A-Za-z0-9\\-_\\.]+|^$");
+ }
+
+ private PfReferenceKey setTestReferenceKey() {
+ PfReferenceKey testReferenceKey = new PfReferenceKey();
+ testReferenceKey.setParentConceptKey(new PfConceptKey("PN", VERSION001));
+ testReferenceKey.setParentReferenceKey(new PfReferenceKey("PN", VERSION001, "LN"));
+ testReferenceKey.setParentKeyName("NPKN");
+ testReferenceKey.setParentKeyVersion(VERSION001);
+ testReferenceKey.setParentLocalName(NPKLN);
+ testReferenceKey.setLocalName("NLN");
+
+ return testReferenceKey;
}
}