8af19190aeeb374bdb01a2d2f558bb16001f717a
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 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.policymodel.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
34
35 /**
36  * Test task parameters.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class TaskParameterTest {
41
42     @Test
43     public void testTaskParameter() {
44         assertNotNull(new AxTaskParameter());
45         assertNotNull(new AxTaskParameter(new AxReferenceKey()));
46         assertNotNull(new AxTaskParameter(new AxReferenceKey(), "DefaultValue"));
47
48         final AxTaskParameter par = new AxTaskParameter();
49
50         final AxReferenceKey parKey = new AxReferenceKey("ParParentName", "0.0.1", "PLN", "LN");
51         par.setKey(parKey);
52         assertEquals("ParParentName:0.0.1:PLN:LN", par.getKey().getId());
53         assertEquals("ParParentName:0.0.1:PLN:LN", par.getKeys().get(0).getId());
54
55         par.setDefaultValue("DefaultValue");
56         assertEquals("DefaultValue", par.getTaskParameterValue());
57
58         AxValidationResult result = new AxValidationResult();
59         result = par.validate(result);
60         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
61
62         par.setKey(AxReferenceKey.getNullKey());
63         result = new AxValidationResult();
64         result = par.validate(result);
65         assertEquals(ValidationResult.INVALID, result.getValidationResult());
66
67         par.setKey(parKey);
68         result = new AxValidationResult();
69         result = par.validate(result);
70         assertEquals(ValidationResult.VALID, result.getValidationResult());
71
72         assertThatThrownBy(() -> par.setDefaultValue(null))
73             .hasMessage("defaultValue may not be null");
74         par.setDefaultValue("");
75         result = new AxValidationResult();
76         result = par.validate(result);
77         assertEquals(ValidationResult.WARNING, result.getValidationResult());
78
79         par.setDefaultValue("DefaultValue");
80         result = new AxValidationResult();
81         result = par.validate(result);
82         assertEquals(ValidationResult.VALID, result.getValidationResult());
83
84         par.clean();
85
86         final AxTaskParameter clonedPar = new AxTaskParameter(par);
87         assertEquals("AxTaskParameter:(key=AxReferenceKey:(parentKeyName=ParParentName,parentKeyVersion=0.0.1,"
88                         + "parentLocalName=PLN,localName=LN),defaultValue=DefaultValue)", clonedPar.toString());
89
90         assertNotEquals(0, par.hashCode());
91         // disabling sonar because this code tests the equals() method
92         assertEquals(par, par); // NOSONAR
93         assertEquals(par, clonedPar);
94         assertNotNull(par);
95         assertNotEquals(par, "Hello");
96         assertNotEquals(par, new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue"));
97         assertNotEquals(par, new AxTaskParameter(parKey, "OtherDefaultValue"));
98         assertEquals(par, new AxTaskParameter(parKey, "DefaultValue"));
99
100         assertEquals(0, par.compareTo(par));
101         assertEquals(0, par.compareTo(clonedPar));
102         assertNotEquals(0, par.compareTo(new AxArtifactKey()));
103         assertNotEquals(0, par.compareTo(null));
104         assertNotEquals(0, par.compareTo(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue")));
105         assertNotEquals(0, par.compareTo(new AxTaskParameter(parKey, "OtherDefaultValue")));
106         assertEquals(0, par.compareTo(new AxTaskParameter(parKey, "DefaultValue")));
107
108         assertNotNull(par.getKeys());
109     }
110 }