Use ValidationResult for models v2.0
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / testconcepts / DummyPfConcept.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
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.models.base.testconcepts;
23
24 import java.util.List;
25 import javax.persistence.EmbeddedId;
26 import lombok.Data;
27 import lombok.EqualsAndHashCode;
28 import lombok.NonNull;
29 import org.apache.commons.lang3.ObjectUtils;
30 import org.onap.policy.common.parameters.BeanValidationResult;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.onap.policy.models.base.PfAuthorative;
33 import org.onap.policy.models.base.PfConcept;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfKey;
36
37 @Data
38 @EqualsAndHashCode(callSuper = false)
39 public class DummyPfConcept extends PfConcept implements PfAuthorative<DummyAuthorativeConcept> {
40     private static final long serialVersionUID = 1L;
41     @EmbeddedId
42     private PfConceptKey key;
43
44     private String description;
45
46
47     /**
48      * The Default Constructor creates a {@link DummyPfConcept} object with a null key.
49      */
50     public DummyPfConcept() {
51         this(new PfConceptKey());
52     }
53
54     /**
55      * The Key Constructor creates a {@link DummyPfConcept} object with the given concept key.
56      *
57      * @param key the key
58      */
59     public DummyPfConcept(@NonNull final PfConceptKey key) {
60         this.key = key;
61     }
62
63     /**
64      * Copy constructor.
65      *
66      * @param copyConcept the concept to copy from
67      */
68     public DummyPfConcept(final DummyPfConcept copyConcept) {
69         super(copyConcept);
70         this.key = new PfConceptKey(copyConcept.key);
71         this.description = copyConcept.description;
72     }
73
74     @Override
75     public DummyAuthorativeConcept toAuthorative() {
76         DummyAuthorativeConcept dac = new DummyAuthorativeConcept();
77         dac.setName(key.getName());
78         dac.setVersion(key.getVersion());
79         dac.setDescription(description);
80
81         return dac;
82     }
83
84     @Override
85     public void fromAuthorative(DummyAuthorativeConcept dac) {
86         key.setName(dac.getName());
87         key.setVersion(dac.getVersion());
88         description = dac.getDescription();
89     }
90
91     @Override
92     public List<PfKey> getKeys() {
93         return getKey().getKeys();
94     }
95
96     @Override
97     public void clean() {
98         key.clean();
99
100         description = (description != null ? description.trim() : null);
101     }
102
103     @Override
104     public ValidationResult validate(@NonNull String fieldName) {
105         BeanValidationResult result = new BeanValidationResult(fieldName, this);
106
107         result.addResult(validateKeyNotNull("key", key));
108         result.addResult(validateNotBlank("description", description, false));
109
110         return result;
111     }
112
113     @Override
114     public int compareTo(final PfConcept otherConcept) {
115         if (otherConcept == null) {
116             return -1;
117         }
118         if (this == otherConcept) {
119             return 0;
120         }
121         if (getClass() != otherConcept.getClass()) {
122             return this.hashCode() - otherConcept.hashCode();
123         }
124
125         final DummyPfConcept other = (DummyPfConcept) otherConcept;
126         if (!key.equals(other.key)) {
127             return key.compareTo(other.key);
128         }
129
130         return ObjectUtils.compare(description, other.description);
131     }
132 }