2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.introspection.generator;
23 import org.onap.aai.exceptions.AAIException;
24 import org.onap.aai.introspection.*;
26 import java.security.SecureRandom;
27 import java.util.ArrayList;
28 import java.util.List;
30 public class CreateExample implements Wanderer {
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;
40 * Instantiates a new creates the example.
42 * @param loader the loader
43 * @param objectName the object name
45 public CreateExample(Loader loader, String objectName) {
48 this.objectName = objectName;
49 this.blacklist = new ArrayList<>();
54 * Gets the example object.
56 * @return the example object
57 * @throws AAIException
59 public Introspector getExampleObject() throws AAIException {
60 result = loader.introspectorFromName(objectName);
61 blacklist = new ArrayList<>();
63 blacklist.add("relationship-list");
64 if (!result.isContainer()) {
65 blacklist.add("resource-version");
67 IntrospectorWalker walker = new IntrospectorWalker(this, PropertyPredicates.includeInExamples());
69 walker.preventCycles(true);
70 walker.setBlacklist(blacklist);
72 // this.getExampleObject(result);
80 * @param property the property
81 * @param type the type
82 * @param suffix the suffix
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();
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")) {
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")) {
100 } else if (type.toLowerCase().equals("short") || type.contains("java.lang.Short")) {
111 public void processPrimitive(String propName, Introspector obj) {
112 String propType = obj.getType(propName);
114 Object val = this.getValue(propName, propType, "");
115 obj.setValue(propName, val);
122 public void processPrimitiveList(String propName, Introspector obj) {
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));
131 obj.setValue(propName, list);
138 public void processComplexObj(Introspector obj) {
146 public void modifyComplexList(List<Introspector> list, List<Object> listReference, Introspector parent,
147 Introspector child) {
148 // TODO Auto-generated method stub
156 public boolean createComplexObjIfNull() {
164 public int createComplexListSize(Introspector parent, Introspector child) {