63906add2f529f714371195aaddfd57d1bf5ada0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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.model.policymodel.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
35 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
36
37 /**
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class TestTaskParameter {
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         try {
73             par.setDefaultValue(null);
74             fail("test should throw an exception here");
75         } catch (final Exception e) {
76             assertEquals("defaultValue may not be null", e.getMessage());
77         }
78
79         par.setDefaultValue("");
80         result = new AxValidationResult();
81         result = par.validate(result);
82         assertEquals(ValidationResult.WARNING, result.getValidationResult());
83
84         par.setDefaultValue("DefaultValue");
85         result = new AxValidationResult();
86         result = par.validate(result);
87         assertEquals(ValidationResult.VALID, result.getValidationResult());
88
89         par.clean();
90
91         final AxTaskParameter clonedPar = new AxTaskParameter(par);
92         assertEquals(
93                 "AxTaskParameter:(key=AxReferenceKey:(parentKeyName=ParParentName,parentKeyVersion=0.0.1,parentLocalName=PLN,localName=LN),defaultValue=DefaultValue)",
94                 clonedPar.toString());
95
96         assertFalse(par.hashCode() == 0);
97
98         assertTrue(par.equals(par));
99         assertTrue(par.equals(clonedPar));
100         assertFalse(par.equals(null));
101         assertFalse(par.equals("Hello"));
102         assertFalse(par.equals(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue")));
103         assertFalse(par.equals(new AxTaskParameter(parKey, "OtherDefaultValue")));
104         assertTrue(par.equals(new AxTaskParameter(parKey, "DefaultValue")));
105
106         assertEquals(0, par.compareTo(par));
107         assertEquals(0, par.compareTo(clonedPar));
108         assertNotEquals(0, par.compareTo(new AxArtifactKey()));
109         assertNotEquals(0, par.compareTo(null));
110         assertNotEquals(0, par.compareTo(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue")));
111         assertNotEquals(0, par.compareTo(new AxTaskParameter(parKey, "OtherDefaultValue")));
112         assertEquals(0, par.compareTo(new AxTaskParameter(parKey, "DefaultValue")));
113
114         assertNotNull(par.getKeys());
115     }
116 }