summaryrefslogtreecommitdiffstats
path: root/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyTest.java
blob: 592933772cfb8169e92c15171a69d90072c77e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.model.basicmodel.concepts;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.lang.reflect.Field;
import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.concepts.AxKey.Compatibility;

public class AxKeyTest {

    @Test
    public void testArtifactKey() {
        try {
            new AxArtifactKey("some bad key id");
            fail("This test should throw an exception");
        } catch (IllegalArgumentException e) {
            assertEquals("parameter \"id\": value \"some bad key id\", "
                            + "does not match regular expression \"[A-Za-z0-9\\-_\\.]+:[0-9].[0-9].[0-9]\"",
                            e.getMessage());
        }

        AxArtifactKey someKey0 = new AxArtifactKey();
        assertEquals(AxArtifactKey.getNullKey(), someKey0);

        AxArtifactKey someKey1 = new AxArtifactKey("name", "0.0.1");
        AxArtifactKey someKey2 = new AxArtifactKey(someKey1);
        AxArtifactKey someKey3 = new AxArtifactKey(someKey1.getId());
        assertEquals(someKey1, someKey2);
        assertEquals(someKey1, someKey3);

        assertEquals(someKey2, someKey1.getKey());
        assertEquals(1, someKey1.getKeys().size());

        someKey0.setName("zero");
        someKey0.setVersion("0.0.2");

        someKey3.setVersion("0.0.2");

        AxArtifactKey someKey4 = new AxArtifactKey(someKey1);
        someKey4.setVersion("0.1.2");

        AxArtifactKey someKey5 = new AxArtifactKey(someKey1);
        someKey5.setVersion("1.2.2");

        AxArtifactKey someKey6 = new AxArtifactKey(someKey1);
        someKey6.setVersion("3");

        assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(new AxReferenceKey()));
        assertEquals(Compatibility.DIFFERENT, someKey0.getCompatibility(someKey1));
        assertEquals(Compatibility.IDENTICAL, someKey2.getCompatibility(someKey1));
        assertEquals(Compatibility.PATCH, someKey3.getCompatibility(someKey1));
        assertEquals(Compatibility.MINOR, someKey4.getCompatibility(someKey1));
        assertEquals(Compatibility.MAJOR, someKey5.getCompatibility(someKey1));
        assertEquals(Compatibility.MAJOR, someKey6.getCompatibility(someKey1));

        assertTrue(someKey1.isCompatible(someKey2));
        assertTrue(someKey1.isCompatible(someKey3));
        assertTrue(someKey1.isCompatible(someKey4));
        assertFalse(someKey1.isCompatible(someKey0));
        assertFalse(someKey1.isCompatible(someKey5));
        assertFalse(someKey1.isCompatible(new AxReferenceKey()));

        assertEquals(AxValidationResult.ValidationResult.VALID,
                        someKey0.validate(new AxValidationResult()).getValidationResult());
        assertEquals(AxValidationResult.ValidationResult.VALID,
                        someKey1.validate(new AxValidationResult()).getValidationResult());
        assertEquals(AxValidationResult.ValidationResult.VALID,
                        someKey2.validate(new AxValidationResult()).getValidationResult());
        assertEquals(AxValidationResult.ValidationResult.VALID,
                        someKey3.validate(new AxValidationResult()).getValidationResult());
        assertEquals(AxValidationResult.ValidationResult.VALID,
                        someKey4.validate(new AxValidationResult()).getValidationResult());
        assertEquals(AxValidationResult.ValidationResult.VALID,
                        someKey5.validate(new AxValidationResult()).getValidationResult());
        assertEquals(AxValidationResult.ValidationResult.VALID,
                        someKey6.validate(new AxValidationResult()).getValidationResult());

        someKey0.clean();
        assertNotNull(someKey0.toString());

        AxArtifactKey someKey7 = new AxArtifactKey(someKey1);
        assertEquals(150332875, someKey7.hashCode());
        assertEquals(0, someKey7.compareTo(someKey1));
        assertEquals(-12, someKey7.compareTo(someKey0));

        try {
            someKey0.compareTo(null);
        } catch (IllegalArgumentException e) {
            assertEquals("comparison object may not be null", e.getMessage());
        }

        assertEquals(0, someKey0.compareTo(someKey0));
        assertEquals(353602977, someKey0.compareTo(new AxReferenceKey()));

        assertFalse(someKey0.equals(null));
        assertTrue(someKey0.equals(someKey0));
        assertFalse(((AxKey) someKey0).equals(new AxReferenceKey()));

        AxArtifactKey nullKey0 = AxArtifactKey.getNullKey();
        assertTrue(nullKey0.isNullKey());
        AxArtifactKey nullKey1 = new AxArtifactKey();
        assertTrue(nullKey1.isNullKey());
        AxArtifactKey nullKey2 = new AxArtifactKey(AxKey.NULL_KEY_NAME,AxKey.NULL_KEY_VERSION);
        assertTrue(nullKey2.isNullKey());
        AxArtifactKey notnullKey = new AxArtifactKey("Blah",AxKey.NULL_KEY_VERSION);
        assertFalse(notnullKey.isNullKey());

    }


    @Test
    public void testValidation() {
        AxArtifactKey testKey = new AxArtifactKey("TheKey", "0.0.1");
        assertEquals("TheKey:0.0.1", testKey.getId());

        try {
            Field nameField = testKey.getClass().getDeclaredField("name");
            nameField.setAccessible(true);
            nameField.set(testKey, "Key Name");
            AxValidationResult validationResult = new AxValidationResult();
            testKey.validate(validationResult);
            nameField.set(testKey, "TheKey");
            nameField.setAccessible(false);
            assertEquals(
                "name invalid-parameter name with value Key Name "
                    + "does not match regular expression [A-Za-z0-9\\-_\\.]+",
                validationResult.getMessageList().get(0).getMessage());
        } catch (Exception validationException) {
            fail("test should not throw an exception");
        }

        try {
            Field versionField = testKey.getClass().getDeclaredField("version");
            versionField.setAccessible(true);
            versionField.set(testKey, "Key Version");
            AxValidationResult validationResult = new AxValidationResult();
            testKey.validate(validationResult);
            versionField.set(testKey, "0.0.1");
            versionField.setAccessible(false);
            assertEquals(
                "version invalid-parameter version with value Key Version "
                    + "does not match regular expression [A-Za-z0-9.]+",
                validationResult.getMessageList().get(0).getMessage());
        } catch (Exception validationException) {
            fail("test should not throw an exception");
        }
    }
}