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