2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
27 import java.util.List;
30 import lombok.NoArgsConstructor;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.annotations.NotNull;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
38 public class JpaToscaWithStringPropertiesTest {
39 private static final String SOME_DESCRIPTION = "some description";
40 private static final String KEY1 = "abc";
41 private static final String KEY2 = "def";
42 private static final String STRING1 = "10";
43 private static final String STRING2 = "20";
44 private static final int INT1 = 10;
45 private static final int INT2 = 20;
55 public void testGetKeys() {
56 PfConceptKey key = new PfConceptKey("bye", "9.8.7");
57 PfConceptKey typeKey = new PfConceptKey("type", "6.5.4");
59 jpa = new MyJpa(key, typeKey);
60 jpa.setDescription(SOME_DESCRIPTION);
61 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
63 // properties should be ignored
64 assertThat(jpa.getKeys()).isEqualTo(List.of(key, typeKey));
68 public void testClean() {
69 jpa.setDescription(" some description ");
70 jpa.setProperties(Map.of(KEY1, "10 ", KEY2, " 20"));
73 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
74 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
78 public void testToAuthorative() {
79 jpa.setDescription(SOME_DESCRIPTION);
80 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
82 MyTosca tosca = jpa.toAuthorative();
83 assertEquals(SOME_DESCRIPTION, tosca.getDescription());
84 assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, INT1, KEY2, INT2));
88 public void testFromAuthorative() {
89 MyTosca tosca = new MyTosca();
90 tosca.setType("type");
91 tosca.setTypeVersion("1.2.3");
92 tosca.setDescription(SOME_DESCRIPTION);
94 jpa.fromAuthorative(tosca);
95 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
96 assertThat(jpa.getProperties()).isNull();
98 tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
101 jpa.fromAuthorative(tosca);
102 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
103 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
107 public void testCompareTo() {
108 jpa.setDescription(SOME_DESCRIPTION);
109 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
111 assertThat(jpa).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2());
113 MyJpa jpa2 = new MyJpa();
114 jpa2.setDescription(SOME_DESCRIPTION);
115 jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
116 assertThat(jpa).isEqualByComparingTo(jpa2);
118 jpa2.setProperties(Map.of(KEY1, STRING1));
119 assertThat(jpa).isNotEqualByComparingTo(jpa2);
123 public void testJpaToscaWithStringProperties() {
124 assertThat(jpa.getProperties()).isNull();
125 assertThat(jpa.getKey().isNullKey()).isTrue();
130 public void testJpaToscaWithStringPropertiesPfConceptKey() {
131 PfConceptKey key = new PfConceptKey("hello", "1.2.3");
133 jpa = new MyJpa(key);
134 assertEquals(key, jpa.getKey());
138 public void testJpaToscaWithStringPropertiesJpaToscaWithStringPropertiesOfT() {
139 jpa.setDescription(SOME_DESCRIPTION);
140 assertEquals(jpa, new MyJpa(jpa));
142 jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
143 assertEquals(jpa, new MyJpa(jpa));
147 public void testJpaToscaWithStringPropertiesT() {
148 MyTosca tosca = new MyTosca();
149 tosca.setName("world");
150 tosca.setVersion("3.2.1");
151 tosca.setType("planet");
152 tosca.setTypeVersion("6.5.4");
153 tosca.setDescription(SOME_DESCRIPTION);
154 tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
156 jpa = new MyJpa(tosca);
157 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
158 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
159 assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
160 assertEquals(new PfConceptKey("planet", "6.5.4"), jpa.getType());
164 public void testValidateWithKey() {
165 // null key - should fail
166 jpa.setText("some text");
167 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
169 // not valid, type is not set
170 jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
171 assertThat(jpa.validateWithKey("fieldB").isValid()).isFalse();
173 // valid, type is set
174 jpa.setType(new PfConceptKey("uvw", "5.6.7"));
175 assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
177 // null text - bean validator should fail
179 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
182 private static class MyTosca extends ToscaWithTypeAndObjectProperties {
186 protected static class MyJpa extends JpaToscaWithTypeAndStringProperties<MyTosca> {
187 private static final long serialVersionUID = 1L;
194 public MyJpa(MyJpa jpa) {
198 public MyJpa(PfConceptKey key) {
202 public MyJpa(PfConceptKey key, PfConceptKey type) {
206 public MyJpa(MyTosca tosca) {
211 public MyTosca toAuthorative() {
212 this.setToscaEntity(new MyTosca());
213 return super.toAuthorative();
217 protected Object deserializePropertyValue(String propValue) {
218 return Integer.parseInt(propValue);
222 protected String serializePropertyValue(Object propValue) {
223 return propValue.toString();
227 private static class MyJpa2 extends MyJpa {
228 private static final long serialVersionUID = 1L;