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