fe059724f24371e14174e4d99980d8cd63f45345
[policy/apex-pdp.git] / model / policy-model / src / test / java / org / onap / policy / apex / model / policymodel / concepts / TaskParameterTest.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.policymodel.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 import static org.junit.Assert.fail;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
36 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
37
38 /**
39  * Test task parameters.
40  * 
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class TaskParameterTest {
44
45     @Test
46     public void testTaskParameter() {
47         assertNotNull(new AxTaskParameter());
48         assertNotNull(new AxTaskParameter(new AxReferenceKey()));
49         assertNotNull(new AxTaskParameter(new AxReferenceKey(), "DefaultValue"));
50
51         final AxTaskParameter par = new AxTaskParameter();
52
53         final AxReferenceKey parKey = new AxReferenceKey("ParParentName", "0.0.1", "PLN", "LN");
54         par.setKey(parKey);
55         assertEquals("ParParentName:0.0.1:PLN:LN", par.getKey().getId());
56         assertEquals("ParParentName:0.0.1:PLN:LN", par.getKeys().get(0).getId());
57
58         par.setDefaultValue("DefaultValue");
59         assertEquals("DefaultValue", par.getTaskParameterValue());
60
61         AxValidationResult result = new AxValidationResult();
62         result = par.validate(result);
63         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
64
65         par.setKey(AxReferenceKey.getNullKey());
66         result = new AxValidationResult();
67         result = par.validate(result);
68         assertEquals(ValidationResult.INVALID, result.getValidationResult());
69
70         par.setKey(parKey);
71         result = new AxValidationResult();
72         result = par.validate(result);
73         assertEquals(ValidationResult.VALID, result.getValidationResult());
74
75         try {
76             par.setDefaultValue(null);
77             fail("test should throw an exception here");
78         } catch (final Exception e) {
79             assertEquals("defaultValue may not be null", e.getMessage());
80         }
81
82         par.setDefaultValue("");
83         result = new AxValidationResult();
84         result = par.validate(result);
85         assertEquals(ValidationResult.WARNING, result.getValidationResult());
86
87         par.setDefaultValue("DefaultValue");
88         result = new AxValidationResult();
89         result = par.validate(result);
90         assertEquals(ValidationResult.VALID, result.getValidationResult());
91
92         par.clean();
93
94         final AxTaskParameter clonedPar = new AxTaskParameter(par);
95         assertEquals("AxTaskParameter:(key=AxReferenceKey:(parentKeyName=ParParentName,parentKeyVersion=0.0.1,"
96                         + "parentLocalName=PLN,localName=LN),defaultValue=DefaultValue)", clonedPar.toString());
97
98         assertFalse(par.hashCode() == 0);
99
100         assertTrue(par.equals(par));
101         assertTrue(par.equals(clonedPar));
102         assertFalse(par.equals(null));
103         assertFalse(par.equals((Object)"Hello"));
104         assertFalse(par.equals(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue")));
105         assertFalse(par.equals(new AxTaskParameter(parKey, "OtherDefaultValue")));
106         assertTrue(par.equals(new AxTaskParameter(parKey, "DefaultValue")));
107
108         assertEquals(0, par.compareTo(par));
109         assertEquals(0, par.compareTo(clonedPar));
110         assertNotEquals(0, par.compareTo(new AxArtifactKey()));
111         assertNotEquals(0, par.compareTo(null));
112         assertNotEquals(0, par.compareTo(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue")));
113         assertNotEquals(0, par.compareTo(new AxTaskParameter(parKey, "OtherDefaultValue")));
114         assertEquals(0, par.compareTo(new AxTaskParameter(parKey, "DefaultValue")));
115
116         assertNotNull(par.getKeys());
117     }
118 }