486252517b75d1d4ea6897997327ae2406cdd0bc
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.entities;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.Test;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
32
33 /**
34  * Test the AxArtifactKey test entity.
35  *
36  */
37 public class ArtifactKeyTestEntityTest {
38
39     @Test
40     public void testTestEntity() {
41         ArtifactKeyTestEntity testEntity = new ArtifactKeyTestEntity();
42
43         ArtifactKeyTestEntity testEntityCopy = new ArtifactKeyTestEntity();
44         assertEquals(120390253, testEntityCopy.hashCode());
45
46         testEntity.setKey(null);
47         testEntity.copyTo(testEntityCopy);
48         assertTrue(testEntity.equals((testEntityCopy)));
49         assertFalse(testEntity.checkSetKey());
50         AxArtifactKey key = new AxArtifactKey("TestKey", "0.0.1");
51
52         testEntity.setKey(key);
53         testEntity.clean();
54         AxValidationResult result = testEntity.validate(new AxValidationResult());
55         assertEquals(ValidationResult.VALID, result.getValidationResult());
56         assertEquals(key, testEntity.getKey());
57         assertEquals(key, testEntity.getKeys().get(0));
58         assertEquals(key.getId(), testEntity.getId());
59         assertEquals((Double) 0.0, (Double) testEntity.getDoubleValue());
60         assertTrue(testEntity.checkSetKey());
61         assertEquals((Double) 0.0, (Double) testEntity.getDoubleValue());
62         testEntity.setDoubleValue(3.14);
63         assertEquals((Double) 3.14, (Double) testEntity.getDoubleValue());
64         assertTrue(testEntity.checkSetKey());
65         assertEquals("ArtifactKeyTestEntity [key=AxArtifactKey:(name=TestKey,version=0.0.1), doubleValue=3.14]",
66                         testEntity.toString());
67         ArtifactKeyTestEntity testEntityClone = new ArtifactKeyTestEntity();
68         testEntity.copyTo(testEntityClone);
69         assertTrue(testEntity.equals(testEntity));
70         assertTrue(testEntity.equals(testEntityClone));
71         ArtifactKeyTestEntity testEntityNew = null;
72         testEntityNew = (ArtifactKeyTestEntity) testEntity.copyTo(testEntityNew);
73         assertTrue(testEntityNew.equals(testEntityNew));
74         assertTrue(testEntity.equals(testEntityNew));
75         ReferenceKeyTestEntity testEntityBad = new ReferenceKeyTestEntity();
76         testEntityBad = (ReferenceKeyTestEntity) testEntity.copyTo(testEntityBad);
77         assertNull(testEntityBad);
78         
79         testEntityBad = new ReferenceKeyTestEntity();
80         assertEquals(-1036664728, testEntity.hashCode());
81         assertFalse(testEntity.equals(null));
82         assertEquals(-1, testEntity.compareTo(null));
83         assertTrue(testEntity.equals(testEntity));
84         assertEquals(0, testEntity.compareTo(testEntity));
85         assertFalse(testEntity.equals(testEntityBad));
86         assertEquals(-1, testEntity.compareTo(testEntityBad));
87         assertFalse(testEntityCopy.equals(testEntity));
88         assertEquals(1, testEntityCopy.compareTo(testEntity));
89         testEntityClone.setKey(key);
90         testEntityNew.setKey(AxArtifactKey.getNullKey());
91         assertFalse(testEntityNew.equals(testEntityClone));
92         assertEquals(-6, testEntityNew.compareTo(testEntityClone));
93         testEntityClone.setKey(null);
94         testEntityNew.setKey(null);
95         assertTrue(testEntityNew.equals(testEntityClone));
96         assertEquals(0, testEntityNew.compareTo(testEntityClone));
97         testEntityCopy.setKey(AxArtifactKey.getNullKey());
98         assertFalse(testEntityCopy.equals(testEntity));
99         assertEquals(-6, testEntityCopy.compareTo(testEntity));
100         testEntityClone.setKey(key);
101         testEntityClone.setDoubleValue(1.23);
102         assertFalse(testEntity.equals(testEntityClone));
103         assertEquals(1, testEntity.compareTo(testEntityClone));
104         
105         ArtifactKeyTestEntity entity2 = new ArtifactKeyTestEntity(3.14);
106         assertEquals((Double)3.14, (Double)entity2.getDoubleValue());
107         ArtifactKeyTestEntity entity3 = new ArtifactKeyTestEntity(key, 3.14);
108         assertEquals(key, entity3.getKey());
109         
110         entity3.setKey(null);
111         assertEquals(31, entity3.hashCode());
112     }
113 }