2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.sdc.be.model.serialize;
23 import fj.data.Either;
24 import org.openecomp.sdc.be.model.ComponentInstance;
25 import org.openecomp.sdc.be.model.Resource;
26 import org.openecomp.sdc.common.util.SerializationUtils;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.ParameterizedType;
30 import java.lang.reflect.Type;
32 import java.util.stream.Collectors;
34 import static org.junit.Assert.*;
35 import static org.junit.Assert.fail;
37 public class TestResourceSerialization {
40 public void findAllClassesUsedByResource() {
42 Set<Class> classesWithoutSerialzable = new HashSet<>();
43 Set<String> classestoIgnore = new HashSet<>();
44 classestoIgnore.add("java.util.List");
45 classestoIgnore.add("java.util.Map");
46 classestoIgnore.add("long");
48 Set<Class> allClasses = new HashSet<>();
49 findAllClassesUsedByResource(Resource.class, allClasses);
51 for (Class clazz : allClasses) {
52 Class[] interfaces = clazz.getInterfaces();
53 if (interfaces != null) {
54 String collect = Arrays.stream(interfaces).map(Class::getName).collect(Collectors.joining("\n"));
56 Class orElse = Arrays.stream(interfaces).filter(p -> p.getName().equals("java.io.Serializable"))
57 .findAny().orElse(null);
59 classesWithoutSerialzable.add(clazz);
65 List<Class> collect = classesWithoutSerialzable.stream()
66 .filter(p -> !classestoIgnore.contains(p.getName())).collect(Collectors.toList());
68 if (collect != null) {
69 System.out.println(collect.stream().map(Class::getName).collect(Collectors.joining("\n")));
70 assertEquals("check all classes implements Serializable", 0, collect.size());
75 public void findAllClassesUsedByResource(Class clazz, Set<Class> allClasses) {
77 Class superclass = clazz.getSuperclass();
78 findAllClassesOfClass(clazz, allClasses);
80 if (superclass != null) {
81 findAllClassesOfClass(superclass, allClasses);
86 public void findAllClassesOfClass(Class clazz, Set<Class> allClasses) {
88 Field[] fields = clazz.getDeclaredFields();
90 for (Field field : fields) {
91 String name = field.getName();
92 Class type = field.getType();
94 if (type.toString().contains(".List")) {
95 ParameterizedType stringListType = (ParameterizedType) field.getGenericType();
96 Class<?> stringListClass = (Class<?>) stringListType.getActualTypeArguments()[0];
97 allClasses.add(stringListClass);
100 if (type.toString().contains("java.util.Map")) {
101 ParameterizedType stringListType = (ParameterizedType) field.getGenericType();
103 Type[] actualTypeArguments = stringListType.getActualTypeArguments();
104 if (actualTypeArguments != null) {
105 for (Type actualType : actualTypeArguments) {
107 String typeName = actualType.getTypeName();
108 // System.out.println("field " + name + "," +
111 if (typeName.startsWith("java.util.List<")) {
112 String internalClass = typeName.replace("java.util.List<", "").replace(">", "");
113 // create class from string
116 myClass = Class.forName(internalClass);
117 allClasses.add(myClass);
118 } catch (ClassNotFoundException e) {
120 fail("Failed to convert " + internalClass + " to class");
125 Class myClass = Class.forName(typeName);
126 allClasses.add(myClass);
127 } catch (ClassNotFoundException e) {
129 fail("Failed to convert " + typeName + " to class");
138 allClasses.add(type);
143 private boolean isClassImplementedSerialize(Class clazz) {
144 Type[] genericInterfaces = clazz.getGenericInterfaces();
145 if (genericInterfaces != null) {
146 Type orElse = Arrays.stream(genericInterfaces).filter(p -> p.getTypeName().equals("java.io.Serializable"))
147 .findAny().orElse(null);
148 if (orElse != null) {