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