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