199a37f59bc316b6714b42ff0e1643ca5383a8fd
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / testconcepts / DummyPfModel.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.ArrayList;
24 import java.util.List;
25
26 import javax.ws.rs.core.Response;
27
28 import lombok.Data;
29 import lombok.EqualsAndHashCode;
30
31 import org.onap.policy.common.utils.validation.Assertions;
32 import org.onap.policy.models.base.PfConcept;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfKey;
35 import org.onap.policy.models.base.PfModel;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.base.PfValidationResult;
38
39 @Data
40 @EqualsAndHashCode(callSuper = true)
41 public class DummyPfModel extends PfModel {
42     private static final long serialVersionUID = 8800599637708309945L;
43
44     private List<PfKey> keyList;
45
46     /**
47      * The Default Constructor creates a {@link DummyPfModel} object with a null concept key and
48      * creates an empty TOSCA model.
49      */
50     public DummyPfModel() {
51         super();
52         super.setKey(new PfConceptKey());
53         this.keyList = new ArrayList<PfKey>();
54     }
55
56     /**
57      * The Key Constructor creates a {@link DummyPfModel} object with the given concept key and
58      * creates an empty TOSCA model.
59      *
60      * @param key the TOSCA model key
61      */
62     public DummyPfModel(final PfConceptKey key) {
63         super(key);
64         this.keyList = new ArrayList<PfKey>();
65     }
66
67     /**
68      * Constructor that initiates a {@link ToscaModel} with all its fields.
69      *
70      * @param key the TOSCA model key
71      * @param keyList the service templates in the event model
72      */
73     public DummyPfModel(final PfConceptKey key, final List<PfKey> keyList) {
74         super(key);
75         this.keyList = keyList;
76     }
77
78     /**
79      * Copy constructor.
80      *
81      * @param copyConcept the concept to copy from
82      */
83     public DummyPfModel(final DummyPfModel copyConcept) {
84         super(copyConcept);
85     }
86
87     @Override
88     public void register() {
89     }
90
91     @Override
92     public List<PfKey> getKeys() {
93         final List<PfKey> listOfKeys = super.getKeys();
94
95         listOfKeys.addAll(keyList);
96
97         return listOfKeys;
98     }
99
100     @Override
101     public void clean() {
102         super.clean();
103         for (PfKey pfKey : keyList) {
104             pfKey.clean();
105         }
106     }
107
108     @Override
109     public PfValidationResult validate(final PfValidationResult resultIn) {
110         PfValidationResult result = super.validate(resultIn);
111
112         for (PfKey pfKey : keyList) {
113             result = pfKey.validate(result);
114         }
115
116         return result;
117     }
118
119     @Override
120     public int compareTo(final PfConcept otherConcept) {
121         if (super.compareTo(otherConcept) != 0) {
122             return super.compareTo(otherConcept);
123         }
124
125         if (otherConcept == null) {
126             return -1;
127         }
128
129         if (this == otherConcept) {
130             return 0;
131         }
132
133         if (getClass() != otherConcept.getClass()) {
134             return this.hashCode() - otherConcept.hashCode();
135         }
136
137         final DummyPfModel other = (DummyPfModel) otherConcept;
138         if (!super.equals(other)) {
139             return super.compareTo(other);
140         }
141
142         if (!keyList.equals(other.keyList)) {
143             return (keyList.hashCode() - other.keyList.hashCode());
144         }
145
146         return 0;
147     }
148
149     @Override
150     public PfConcept copyTo(final PfConcept targetObject) {
151         super.copyTo(targetObject);
152
153         Assertions.instanceOf(targetObject, DummyPfModel.class);
154
155         final DummyPfModel copy = ((DummyPfModel) targetObject);
156
157         final List<PfKey> newKeyList = new ArrayList<>();
158         for (final PfKey pfKey : keyList) {
159             PfKey newPfKey;
160             try {
161                 newPfKey = pfKey.getClass().newInstance();
162             } catch (final Exception e) {
163                 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
164                         "error copying concept key: " + e.getMessage(), e);
165             }
166             newPfKey.copyTo(pfKey);
167             newKeyList.add(newPfKey);
168         }
169         copy.setKeyList(newKeyList);
170
171
172         return copy;
173     }
174 }