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).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2());
109 MyJpa jpa2 = new MyJpa();
110 jpa2.setDescription(SOME_DESCRIPTION);
111 jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
112 assertThat(jpa).isEqualByComparingTo(jpa2);
114 jpa2.setProperties(Map.of(KEY1, STRING1));
115 assertThat(jpa).isNotEqualByComparingTo(jpa2);
119 public void testJpaToscaWithStringProperties() {
120 assertThat(jpa.getProperties()).isNull();
121 assertThat(jpa.getKey().isNullKey()).isTrue();
126 public void testJpaToscaWithStringPropertiesPfConceptKey() {
127 PfConceptKey key = new PfConceptKey("hello", "1.2.3");
129 jpa = new MyJpa(key);
130 assertEquals(key, jpa.getKey());
134 public void testJpaToscaWithStringPropertiesJpaToscaWithStringPropertiesOfT() {
135 jpa.setDescription(SOME_DESCRIPTION);
136 assertEquals(jpa, new MyJpa(jpa));
138 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
139 assertEquals(jpa, new MyJpa(jpa));
143 public void testJpaToscaWithStringPropertiesT() {
144 MyTosca tosca = new MyTosca();
145 tosca.setName("world");
146 tosca.setVersion("3.2.1");
147 tosca.setDescription(SOME_DESCRIPTION);
148 tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
150 jpa = new MyJpa(tosca);
151 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
152 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
153 assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
157 public void testValidateWithKey() {
158 // null key - should fail
159 jpa.setText("some text");
160 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
163 jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
164 assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
166 // null text - bean validator should fail
168 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
171 private static class MyTosca extends ToscaWithObjectProperties {
176 protected static class MyJpa extends JpaToscaWithStringProperties<MyTosca> {
177 private static final long serialVersionUID = 1L;
184 public MyJpa(MyJpa jpa) {
188 public MyJpa(PfConceptKey key) {
192 public MyJpa(MyTosca tosca) {
197 public MyTosca toAuthorative() {
198 this.setToscaEntity(new MyTosca());
199 return super.toAuthorative();
203 protected Object deserializePropertyValue(String propValue) {
204 return Integer.parseInt(propValue);
208 protected String serializePropertyValue(Object propValue) {
209 return propValue.toString();
213 private static class MyJpa2 extends MyJpa {
214 private static final long serialVersionUID = 1L;