Merge "Fix simple sonar issues in models: errors to sim-pdp"
[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 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
26 import javax.persistence.EmbeddedId;
27
28 import lombok.Data;
29 import lombok.EqualsAndHashCode;
30 import lombok.NonNull;
31
32 import org.apache.commons.lang3.ObjectUtils;
33 import org.onap.policy.common.utils.validation.Assertions;
34 import org.onap.policy.models.base.PfAuthorative;
35 import org.onap.policy.models.base.PfConcept;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfValidationMessage;
39 import org.onap.policy.models.base.PfValidationResult;
40 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
41
42 @Data
43 @EqualsAndHashCode(callSuper = false)
44 public class DummyPfConcept extends PfConcept implements PfAuthorative<DummyAuthorativeConcept> {
45     private static final long serialVersionUID = 1L;
46     @EmbeddedId
47     private PfConceptKey key;
48
49     private String description;
50
51
52     /**
53      * The Default Constructor creates a {@link DummyPfConcept} object with a null key.
54      */
55     public DummyPfConcept() {
56         this(new PfConceptKey());
57     }
58
59     /**
60      * The Key Constructor creates a {@link DummyPfConcept} object with the given concept key.
61      *
62      * @param key the key
63      */
64     public DummyPfConcept(@NonNull final PfConceptKey key) {
65         this.key = key;
66     }
67
68     /**
69      * Copy constructor.
70      *
71      * @param copyConcept the concept to copy from
72      */
73     public DummyPfConcept(final DummyPfConcept copyConcept) {
74         super(copyConcept);
75     }
76
77     @Override
78     public DummyAuthorativeConcept toAuthorative() {
79         DummyAuthorativeConcept dac = new DummyAuthorativeConcept();
80         dac.setName(key.getName());
81         dac.setVersion(key.getVersion());
82         dac.setDescription(description);
83
84         return dac;
85     }
86
87     @Override
88     public void fromAuthorative(DummyAuthorativeConcept dac) {
89         key.setName(dac.getName());
90         key.setVersion(dac.getVersion());
91         description = dac.getDescription();
92     }
93
94     @Override
95     public List<PfKey> getKeys() {
96         return getKey().getKeys();
97     }
98
99     @Override
100     public void clean() {
101         key.clean();
102
103         description = (description != null ? description.trim() : null);
104     }
105
106     @Override
107     public PfValidationResult validate(PfValidationResult resultIn) {
108         PfValidationResult result = resultIn;
109
110         if (key.isNullKey()) {
111             result.addValidationMessage(
112                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
113         }
114
115         result = key.validate(result);
116
117         if (description != null && description.trim().length() == 0) {
118             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
119                     "property description may not be blank"));
120         }
121
122         return result;
123     }
124
125     @Override
126     public int compareTo(final PfConcept otherConcept) {
127         if (otherConcept == null) {
128             return -1;
129         }
130         if (this == otherConcept) {
131             return 0;
132         }
133         if (getClass() != otherConcept.getClass()) {
134             return this.hashCode() - otherConcept.hashCode();
135         }
136
137         final DummyPfConcept other = (DummyPfConcept) otherConcept;
138         if (!key.equals(other.key)) {
139             return key.compareTo(other.key);
140         }
141
142         return ObjectUtils.compare(description, other.description);
143     }
144
145     @Override
146     public PfConcept copyTo(@NonNull PfConcept target) {
147         final Object copyObject = target;
148         Assertions.instanceOf(copyObject, PfConcept.class);
149
150         final DummyPfConcept copy = ((DummyPfConcept) copyObject);
151         copy.setKey(new PfConceptKey(key));
152         copy.setDescription(description);
153
154         return copy;
155     }
156 }