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