2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
26 import java.util.List;
29 import lombok.NoArgsConstructor;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.parameters.annotations.NotNull;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithObjectProperties;
37 public class JpaToscaWithStringPropertiesTest {
38 private static final String SOME_DESCRIPTION = "some description";
39 private static final String KEY1 = "abc";
40 private static final String KEY2 = "def";
41 private static final String STRING1 = "10";
42 private static final String STRING2 = "20";
43 private static final int INT1 = 10;
44 private static final int INT2 = 20;
54 public void testGetKeys() {
55 PfConceptKey key = new PfConceptKey("bye", "9.8.7");
58 jpa.setDescription(SOME_DESCRIPTION);
59 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
61 // properties should be ignored
62 assertThat(jpa.getKeys()).isEqualTo(List.of(key));
66 public void testClean() {
67 jpa.setDescription(" some description ");
68 jpa.setProperties(Map.of(KEY1, "10 ", KEY2, " 20"));
71 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
72 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
76 public void testToAuthorative() {
77 jpa.setDescription(SOME_DESCRIPTION);
78 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
80 MyTosca tosca = jpa.toAuthorative();
81 assertEquals(SOME_DESCRIPTION, tosca.getDescription());
82 assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, INT1, KEY2, INT2));
86 public void testFromAuthorative() {
87 MyTosca tosca = new MyTosca();
88 tosca.setDescription(SOME_DESCRIPTION);
90 jpa.fromAuthorative(tosca);
91 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
92 assertThat(jpa.getProperties()).isNull();
94 tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
97 jpa.fromAuthorative(tosca);
98 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
99 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
103 public void testCompareTo() {
104 jpa.setDescription(SOME_DESCRIPTION);
105 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
107 assertThat(jpa.compareTo(null)).isNegative();
108 assertThat(jpa.compareTo(jpa)).isZero();
109 assertThat(jpa.compareTo(new PfConceptKey())).isNotZero();
111 MyJpa jpa2 = new MyJpa();
112 jpa2.setDescription(SOME_DESCRIPTION);
113 jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
114 assertThat(jpa.compareTo(jpa2)).isZero();
116 jpa2.setProperties(Map.of(KEY1, STRING1));
117 assertThat(jpa.compareTo(jpa2)).isNotZero();
121 public void testJpaToscaWithStringProperties() {
122 assertThat(jpa.getProperties()).isNull();
123 assertThat(jpa.getKey().isNullKey()).isTrue();
128 public void testJpaToscaWithStringPropertiesPfConceptKey() {
129 PfConceptKey key = new PfConceptKey("hello", "1.2.3");
131 jpa = new MyJpa(key);
132 assertEquals(key, jpa.getKey());
136 public void testJpaToscaWithStringPropertiesJpaToscaWithStringPropertiesOfT() {
137 jpa.setDescription(SOME_DESCRIPTION);
138 assertEquals(jpa, new MyJpa(jpa));
140 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
141 assertEquals(jpa, new MyJpa(jpa));
145 public void testJpaToscaWithStringPropertiesT() {
146 MyTosca tosca = new MyTosca();
147 tosca.setName("world");
148 tosca.setVersion("3.2.1");
149 tosca.setDescription(SOME_DESCRIPTION);
150 tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
152 jpa = new MyJpa(tosca);
153 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
154 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
155 assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
159 public void testValidateWithKey() {
160 // null key - should fail
161 jpa.setText("some text");
162 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
165 jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
166 assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
168 // null text - bean validator should fail
170 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
173 private static class MyTosca extends ToscaWithObjectProperties {
178 protected static class MyJpa extends JpaToscaWithStringProperties<MyTosca> {
179 private static final long serialVersionUID = 1L;
186 public MyJpa(MyJpa jpa) {
190 public MyJpa(PfConceptKey key) {
194 public MyJpa(MyTosca tosca) {
199 public MyTosca toAuthorative() {
200 this.setToscaEntity(new MyTosca());
201 return super.toAuthorative();
205 protected Object deserializePropertyValue(String propValue) {
206 return Integer.parseInt(propValue);
210 protected String serializePropertyValue(Object propValue) {
211 return propValue.toString();