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