re base code
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / serialize / TestResourceSerialization.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.serialize;
22
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;
27
28 import java.lang.reflect.Field;
29 import java.lang.reflect.ParameterizedType;
30 import java.lang.reflect.Type;
31 import java.util.*;
32 import java.util.stream.Collectors;
33
34 import static org.junit.Assert.*;
35 import static org.junit.Assert.fail;
36
37 public class TestResourceSerialization {
38
39     // @Test
40     public void findAllClassesUsedByResource() {
41
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");
47
48         Set<Class> allClasses = new HashSet<>();
49         findAllClassesUsedByResource(Resource.class, allClasses);
50         ArrayList l;
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"));
55
56                 Class orElse = Arrays.stream(interfaces).filter(p -> p.getName().equals("java.io.Serializable"))
57                         .findAny().orElse(null);
58                 if (orElse == null) {
59                     classesWithoutSerialzable.add(clazz);
60                 }
61
62             }
63         }
64
65         List<Class> collect = classesWithoutSerialzable.stream()
66                                                        .filter(p -> !classestoIgnore.contains(p.getName())).collect(Collectors.toList());
67
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());
71         }
72
73     }
74
75     public void findAllClassesUsedByResource(Class clazz, Set<Class> allClasses) {
76
77         Class superclass = clazz.getSuperclass();
78         findAllClassesOfClass(clazz, allClasses);
79
80         if (superclass != null) {
81             findAllClassesOfClass(superclass, allClasses);
82         }
83
84     }
85
86     public void findAllClassesOfClass(Class clazz, Set<Class> allClasses) {
87
88         Field[] fields = clazz.getDeclaredFields();
89         if (fields != null) {
90             for (Field field : fields) {
91                 String name = field.getName();
92                 Class type = field.getType();
93
94                 if (type.toString().contains(".List")) {
95                     ParameterizedType stringListType = (ParameterizedType) field.getGenericType();
96                     Class<?> stringListClass = (Class<?>) stringListType.getActualTypeArguments()[0];
97                     allClasses.add(stringListClass);
98                 }
99
100                 if (type.toString().contains("java.util.Map")) {
101                     ParameterizedType stringListType = (ParameterizedType) field.getGenericType();
102
103                     Type[] actualTypeArguments = stringListType.getActualTypeArguments();
104                     if (actualTypeArguments != null) {
105                         for (Type actualType : actualTypeArguments) {
106
107                             String typeName = actualType.getTypeName();
108                             // System.out.println("field " + name + "," +
109                             // typeName);
110
111                             if (typeName.startsWith("java.util.List<")) {
112                                 String internalClass = typeName.replace("java.util.List<", "").replace(">", "");
113                                 // create class from string
114                                 Class myClass;
115                                 try {
116                                     myClass = Class.forName(internalClass);
117                                     allClasses.add(myClass);
118                                 } catch (ClassNotFoundException e) {
119                                     e.printStackTrace();
120                                     fail("Failed to convert " + internalClass + " to class");
121                                 }
122
123                             } else {
124                                 try {
125                                     Class myClass = Class.forName(typeName);
126                                     allClasses.add(myClass);
127                                 } catch (ClassNotFoundException e) {
128                                     e.printStackTrace();
129                                     fail("Failed to convert " + typeName + " to class");
130                                 }
131
132                             }
133                         }
134                     }
135
136                 }
137
138                 allClasses.add(type);
139             }
140         }
141
142     }
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) {
149                 return true;
150             }
151         }
152         return false;
153     }
154 }