fba4525e43df23c7a29c555f346fdfd727f29d45
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / introspection / generator / CreateExample.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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.aai.introspection.generator;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Random;
26
27 import org.openecomp.aai.exceptions.AAIException;
28 import org.openecomp.aai.introspection.Introspector;
29 import org.openecomp.aai.introspection.IntrospectorWalker;
30 import org.openecomp.aai.introspection.Loader;
31 import org.openecomp.aai.introspection.PropertyPredicates;
32 import org.openecomp.aai.introspection.Wanderer;
33
34 public class CreateExample implements Wanderer {
35
36         private Random rand = new Random();
37         private final long range = 100000000L;
38         private Loader loader = null;
39         private Introspector result = null;
40         private String objectName = null;
41         private List<String> blacklist = null;
42         
43         /**
44          * Instantiates a new creates the example.
45          *
46          * @param loader the loader
47          * @param objectName the object name
48          */
49         public CreateExample(Loader loader, String objectName) {
50
51                 this.loader = loader;
52                 this.objectName = objectName;
53                 this.blacklist = new ArrayList<>();
54                 
55         }
56
57         /**
58          * Gets the example object.
59          *
60          * @return the example object
61          * @throws AAIException 
62          */
63         public Introspector getExampleObject() throws AAIException {
64                 result = loader.introspectorFromName(objectName);
65                 blacklist = new ArrayList<>();
66                 blacklist.add("any");
67                 blacklist.add("relationship-list");
68                 if (!result.isContainer()) {
69                         blacklist.add("resource-version");
70                 }
71                 IntrospectorWalker walker = new IntrospectorWalker(this, PropertyPredicates.includeInExamples());
72                 
73                 walker.preventCycles(true);
74                 walker.setBlacklist(blacklist);
75                 walker.walk(result);
76                 //this.getExampleObject(result);
77                 
78                 return result;
79         }
80         
81         /**
82          * Gets the value.
83          *
84          * @param property the property
85          * @param type the type
86          * @param suffix the suffix
87          * @return the value
88          */
89         private Object getValue(String property, String type, String suffix) {
90                 long randLong = (long)(rand.nextDouble()*range);
91                 Integer randInt = rand.nextInt(100000);
92                 Integer randShrt = rand.nextInt(20000);
93                 short randShort = randShrt.shortValue();
94         
95                 Object newObj = null;
96                 if (type.contains("java.lang.String")) {
97                         newObj = "example-" + property + "-val-" + randInt + suffix;
98                 } else if ( type.toLowerCase().equals("long") ||type.contains("java.lang.Long")) {
99                         newObj = randLong;
100                 } else if(type.toLowerCase().equals("boolean") || type.contains("java.lang.Boolean")){
101                         newObj = Boolean.TRUE;
102                 } else if ( type.toLowerCase().equals("int") || type.contains("java.lang.Integer")){
103                         newObj = randInt;
104                 }  else if ( type.toLowerCase().equals("short") || type.contains("java.lang.Short")){
105                         newObj = randShort;
106                 }
107                 
108                 return newObj;
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         @Override
115         public void processPrimitive(String propName, Introspector obj) {
116                 String  propType = obj.getType(propName);
117
118                 Object val = this.getValue(propName, propType, "");
119                 obj.setValue(propName, val);            
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         public void processPrimitiveList(String propName, Introspector obj) {
127                 int listSize = 2;
128                 String propType = "";
129                 List<Object> list = new ArrayList<>();
130                 for (int i = 0; i < listSize; i++) {
131                         propType = obj.getGenericType(propName);
132                         Object val = this.getValue(propName, propType, "-" + (i + 1));
133                         list.add(val);
134                 }
135                 obj.setValue(propName, list);           
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         @Override
142         public void processComplexObj(Introspector obj) {
143
144         }
145
146         /**
147          * {@inheritDoc}
148          */
149         @Override
150         public void modifyComplexList(List<Introspector> list, List<Object> listReference, Introspector parent, Introspector child) {
151                 // TODO Auto-generated method stub
152                 
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         @Override
159         public boolean createComplexObjIfNull() {
160                 return true;
161         }
162
163         /**
164          * {@inheritDoc}
165          */
166         @Override
167         public int createComplexListSize(Introspector parent, Introspector child) {
168                 return 1;
169         }
170 }