Refactor to authorative TOSCA serializtion
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base.testconcepts;
22
23 import java.util.List;
24
25 import javax.persistence.EmbeddedId;
26
27 import lombok.Data;
28 import lombok.EqualsAndHashCode;
29 import lombok.NonNull;
30
31 import org.apache.commons.lang3.ObjectUtils;
32 import org.onap.policy.common.utils.validation.Assertions;
33 import org.onap.policy.models.base.PfAuthorative;
34 import org.onap.policy.models.base.PfConcept;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfKey;
37 import org.onap.policy.models.base.PfValidationMessage;
38 import org.onap.policy.models.base.PfValidationResult;
39 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
40
41 @Data
42 @EqualsAndHashCode(callSuper = false)
43 public class DummyPfConcept extends PfConcept implements PfAuthorative<DummyAuthorativeConcept> {
44     private static final long serialVersionUID = 1L;
45     @EmbeddedId
46     private PfConceptKey key;
47
48     private String description;
49
50
51     @Override
52     public DummyAuthorativeConcept toAuthorative() {
53         DummyAuthorativeConcept dac = new DummyAuthorativeConcept();
54         dac.setName(key.getName());
55         dac.setVersion(key.getVersion());
56         dac.setDescription(description);
57
58         return dac;
59     }
60
61     @Override
62     public void fromAuthorative(DummyAuthorativeConcept dac) {
63         key.setName(dac.getName());
64         key.setVersion(dac.getVersion());
65         description = dac.getDescription();
66     }
67
68     /**
69      * The Default Constructor creates a {@link DummyPfConcept} object with a null key.
70      */
71     public DummyPfConcept() {
72         this(new PfConceptKey());
73     }
74
75     /**
76      * The Key Constructor creates a {@link DummyPfConcept} object with the given concept key.
77      *
78      * @param key the key
79      */
80     public DummyPfConcept(@NonNull final PfConceptKey key) {
81         this.key = key;
82     }
83
84     /**
85      * Copy constructor.
86      *
87      * @param copyConcept the concept to copy from
88      */
89     public DummyPfConcept(final DummyPfConcept copyConcept) {
90         super(copyConcept);
91     }
92
93     @Override
94     public List<PfKey> getKeys() {
95         final List<PfKey> keyList = getKey().getKeys();
96         return keyList;
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 }