ee14a0606c3730c5da967f900095b0eec93ceb6f
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / concepts / AxKeyInfoTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.concepts;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.UUID;
31
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
36
37 public class AxKeyInfoTest {
38
39     @Test
40     public void testAxKeyInfo() {
41         assertNotNull(new AxKeyInfo());
42         assertNotNull(new AxKeyInfo(new AxArtifactKey()));
43         assertNotNull(new AxKeyInfo(new AxArtifactKey(), UUID.randomUUID(), "Key description"));
44
45         AxKeyInfo testKeyInfo = new AxKeyInfo();
46         testKeyInfo.setKey((new AxArtifactKey("PN", "0.0.1")));
47         assertEquals("PN:0.0.1", testKeyInfo.getKey().getId());
48         assertTrue(testKeyInfo.matchesId("PN:0.0.1"));
49
50         AxArtifactKey key = new AxArtifactKey("key", "0.0.1");
51         testKeyInfo.setKey(key);
52         assertEquals(key, testKeyInfo.getKey());
53
54         UUID uuid = UUID.randomUUID();
55         testKeyInfo.setUuid(uuid);
56         assertEquals(uuid, testKeyInfo.getUuid());
57         testKeyInfo.setDescription("Key Description");
58         assertEquals("Key Description", testKeyInfo.getDescription());
59
60         AxKeyInfo clonedReferenceKey = new AxKeyInfo(testKeyInfo);
61         assertTrue(clonedReferenceKey.toString()
62                         .startsWith("AxKeyInfo:(artifactId=AxArtifactKey:(name=key,version=0.0.1),uuid="));
63
64         assertFalse(testKeyInfo.hashCode() == 0);
65
66         assertTrue(testKeyInfo.equals(testKeyInfo));
67         assertTrue(testKeyInfo.equals(clonedReferenceKey));
68         assertFalse(testKeyInfo.equals(null));
69         assertFalse(testKeyInfo.equals((Object)new AxArtifactKey()));
70         assertFalse(testKeyInfo.equals(new AxKeyInfo(new AxArtifactKey())));
71         assertFalse(testKeyInfo.equals(new AxKeyInfo(key, UUID.randomUUID(), "Some Description")));
72         assertFalse(testKeyInfo.equals(new AxKeyInfo(key, uuid, "Some Description")));
73         assertTrue(testKeyInfo.equals(new AxKeyInfo(key, uuid, "Key Description")));
74
75         assertEquals(0, testKeyInfo.compareTo(testKeyInfo));
76         assertEquals(0, testKeyInfo.compareTo(clonedReferenceKey));
77         assertNotEquals(0, testKeyInfo.compareTo(null));
78         assertNotEquals(0, testKeyInfo.compareTo(new AxArtifactKey()));
79         assertNotEquals(0, testKeyInfo.compareTo(new AxKeyInfo(new AxArtifactKey())));
80         assertNotEquals(0, testKeyInfo.compareTo(new AxKeyInfo(key, UUID.randomUUID(), "Some Description")));
81         assertNotEquals(0, testKeyInfo.compareTo(new AxKeyInfo(key, uuid, "Some Description")));
82         assertEquals(0, testKeyInfo.compareTo(new AxKeyInfo(key, uuid, "Key Description")));
83
84         assertNotNull(testKeyInfo.getKeys());
85
86         AxValidationResult result = new AxValidationResult();
87         result = testKeyInfo.validate(result);
88         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
89
90         testKeyInfo.setDescription("");
91         result = testKeyInfo.validate(result);
92         assertEquals(AxValidationResult.ValidationResult.OBSERVATION, result.getValidationResult());
93
94         testKeyInfo.setUuid(new UUID(0, 0));
95         result = testKeyInfo.validate(result);
96         assertEquals(AxValidationResult.ValidationResult.WARNING, result.getValidationResult());
97
98         testKeyInfo.setKey(AxArtifactKey.getNullKey());
99         result = testKeyInfo.validate(result);
100         assertEquals(AxValidationResult.ValidationResult.INVALID, result.getValidationResult());
101
102         assertNotNull(AxKeyInfo.generateReproducibleUuid(null));
103         assertNotNull(AxKeyInfo.generateReproducibleUuid("SeedString"));
104
105         testKeyInfo.clean();
106         assertNotNull(testKeyInfo);
107     }
108 }