2 * ============LICENSE_START=======================================================
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
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.openecomp.aai.introspection.generator;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Random;
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;
34 public class CreateExample implements Wanderer {
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;
44 * Instantiates a new creates the example.
46 * @param loader the loader
47 * @param objectName the object name
49 public CreateExample(Loader loader, String objectName) {
52 this.objectName = objectName;
53 this.blacklist = new ArrayList<>();
58 * Gets the example object.
60 * @return the example object
61 * @throws AAIException
63 public Introspector getExampleObject() throws AAIException {
64 result = loader.introspectorFromName(objectName);
65 blacklist = new ArrayList<>();
67 blacklist.add("relationship-list");
68 if (!result.isContainer()) {
69 blacklist.add("resource-version");
71 IntrospectorWalker walker = new IntrospectorWalker(this, PropertyPredicates.includeInExamples());
73 walker.preventCycles(true);
74 walker.setBlacklist(blacklist);
76 //this.getExampleObject(result);
84 * @param property the property
85 * @param type the type
86 * @param suffix the suffix
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();
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")) {
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")){
104 } else if ( type.toLowerCase().equals("short") || type.contains("java.lang.Short")){
115 public void processPrimitive(String propName, Introspector obj) {
116 String propType = obj.getType(propName);
118 Object val = this.getValue(propName, propType, "");
119 obj.setValue(propName, val);
126 public void processPrimitiveList(String propName, Introspector obj) {
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));
135 obj.setValue(propName, list);
142 public void processComplexObj(Introspector obj) {
150 public void modifyComplexList(List<Introspector> list, List<Object> listReference, Introspector parent, Introspector child) {
151 // TODO Auto-generated method stub
159 public boolean createComplexObjIfNull() {
167 public int createComplexListSize(Introspector parent, Introspector child) {