Finish unit test on policy-models
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintLogical.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.tosca.simple.concepts;
22
23 import javax.persistence.Column;
24
25 import lombok.EqualsAndHashCode;
26 import lombok.Getter;
27 import lombok.NonNull;
28 import lombok.ToString;
29
30 import org.apache.commons.lang3.ObjectUtils;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
32
33 /**
34  * This class represents a logical TOSCA constraint: =,>,>=,<,<=.
35  */
36 @EqualsAndHashCode(callSuper = false)
37 @ToString
38 public class JpaToscaConstraintLogical extends JpaToscaConstraint {
39     private static final long serialVersionUID = -2730203215911880756L;
40
41     @Column
42     @NonNull
43     @Getter
44     private JpaToscaConstraintOperation operation;
45
46     @Column
47     @NonNull
48     @Getter
49     private String compareTo;
50
51     /**
52      * Constructor to set operation.
53      *
54      * @param operation the operation to set
55      * @param compareTo the string to compare to
56      */
57     public JpaToscaConstraintLogical(@NonNull final JpaToscaConstraintOperation operation,
58             @NonNull final String compareTo) {
59         this.operation = operation;
60         this.compareTo = compareTo;
61     }
62
63     /**
64      * Authorative constructor.
65      *
66      * @param authorativeConcept the authorative concept to copy from
67      */
68     public JpaToscaConstraintLogical(final ToscaConstraint authorativeConcept) {
69         super(authorativeConcept);
70     }
71
72     @Override
73     public ToscaConstraint toAuthorative() {
74         ToscaConstraint toscaConstraint = new ToscaConstraint();
75
76         if (operation == null) {
77             return null;
78         }
79
80         switch (operation) {
81             case EQ:
82                 toscaConstraint.setEqual(compareTo);
83                 break;
84
85             case GT:
86                 toscaConstraint.setGreaterThan(compareTo);
87                 break;
88
89             case GE:
90                 toscaConstraint.setGreaterOrEqual(compareTo);
91                 break;
92
93             case LT:
94                 toscaConstraint.setLessThan(compareTo);
95                 break;
96
97             case LE:
98                 toscaConstraint.setLessOrEqual(compareTo);
99                 break;
100
101             default:
102                 // Can't happen
103         }
104
105         return toscaConstraint;
106     }
107
108     @Override
109     public void fromAuthorative(final ToscaConstraint toscaConstraint) {
110         // @formatter:off
111         if (toscaConstraint.getEqual() != null) {
112             operation = JpaToscaConstraintOperation.EQ;
113             compareTo = toscaConstraint.getEqual();
114         }
115         else if (toscaConstraint.getGreaterThan() != null) {
116             operation = JpaToscaConstraintOperation.GT;
117             compareTo = toscaConstraint.getGreaterThan();
118         }
119         else if (toscaConstraint.getGreaterOrEqual() != null) {
120             operation = JpaToscaConstraintOperation.GE;
121             compareTo = toscaConstraint.getGreaterOrEqual();
122         }
123         else if (toscaConstraint.getLessThan() != null) {
124             operation = JpaToscaConstraintOperation.LT;
125             compareTo = toscaConstraint.getLessThan();
126         }
127         else if (toscaConstraint.getLessOrEqual() != null) {
128             operation = JpaToscaConstraintOperation.LE;
129             compareTo = toscaConstraint.getLessOrEqual();
130         }
131         // @formatter:on
132     }
133
134     @Override
135     public int compareTo(JpaToscaConstraint otherConstraint) {
136         if (otherConstraint == null) {
137             return -1;
138         }
139         if (this == otherConstraint) {
140             return 0;
141         }
142         if (getClass() != otherConstraint.getClass()) {
143             return this.hashCode() - otherConstraint.hashCode();
144         }
145
146         final JpaToscaConstraintLogical other = (JpaToscaConstraintLogical) otherConstraint;
147
148         int result = ObjectUtils.compare(operation, other.operation);
149         if (result != 0) {
150             return result;
151         }
152
153         return ObjectUtils.compare(compareTo, other.compareTo);
154     }
155 }