aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/testing/UpdateObject.java
blob: 9d8500c8bde0262f5330bfcdc3ad3408cbcaf163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.aai.testing;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.xml.bind.JAXBException;

import org.json.JSONException;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.introspection.Introspector;
import org.openecomp.aai.introspection.IntrospectorWalker;
import org.openecomp.aai.logging.LogLineBuilder;
import org.openecomp.aai.serialization.db.EdgeRule;
import org.openecomp.aai.serialization.db.EdgeRules;
import org.openecomp.aai.serialization.db.MultiplicityRule;

public class UpdateObject extends TestDataGenerator {
	
	private PopulateObject pop = new PopulateObject(); 
	
	protected List<String> blacklist = new ArrayList<>();
	protected final LogLineBuilder llBuilder = new LogLineBuilder();
	
	/**
	 * Instantiates a new update object.
	 */
	public UpdateObject() {
		blacklist.add("any");
		blacklist.add("relationship-list");
		blacklist.add("resource-version");
	}
	
	/**
	 * Randomly update field values.
	 *
	 * @param obj the obj
	 * @return the introspector
	 */
	public Introspector randomlyUpdateFieldValues(Introspector obj){
		Introspector clone = null;

		clone = (Introspector)obj.clone();

		IntrospectorWalker walker = new IntrospectorWalker(this, llBuilder);
		walker.setBlacklist(blacklist);
		walker.walk(clone);
		
		return clone;

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void processPrimitive(String propName, Introspector obj) {
		List<String> keys = obj.getKeys();
		if (!keys.contains(propName)) {
			Random random = new Random();
			int num = random.nextInt(100);
			if (num <= 33) {
				Object val = this.createNewRandomObj(obj.getType(propName));
				obj.setValue(propName, val);
			}
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void processPrimitiveList(String propName, Introspector obj) {
		Random random = new Random();
		int num = random.nextInt(100);
		if (num <= 33) {
			List<Object> list = (List<Object>) obj.getValue(propName);
			int size = list.size();
			Random rand = new Random();
			int newSize = 0;
			if (size == 0) {
				newSize = rand.nextInt(max);
			} else {
				newSize = rand.nextInt(size);
			}
			list.clear();
			for (int i = 0; i < newSize; i++) {
				list.add(this.createNewRandomObj(obj.getGenericType(propName)));
			}
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void processComplexObj(Introspector obj) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void modifyComplexList(List<Object> list, Introspector parent, Introspector child) {
		
		EdgeRule rule;
		int numberOfObjects = max;

		try {
			rule = EdgeRules.getInstance().getEdgeRule(parent.getDbName(), child.getDbName());
			if (rule.getMultiplicityRule().equals(MultiplicityRule.ONE2ONE)) {
				numberOfObjects = 1;
			}
		} catch (AAIException e) {
			System.out.println(e.getErrorObject().getDetails());
		}
		
		int editRemoveOrAdd;
		List<Integer> randNumList = new ArrayList<Integer>();
		Random rand = new Random();
		int numOfItemstoUpdate = 0;
		
		if (numberOfObjects == 1) {
			numOfItemstoUpdate = rand.nextInt(numberOfObjects);
		} else {
			numOfItemstoUpdate = rand.nextInt(numberOfObjects-1)+1;
		}
		for (int i = 0; i < numOfItemstoUpdate; i++){
			randNumList.add(i);
		}
		Collections.shuffle(randNumList);
		for (int i = 0; i < numOfItemstoUpdate; i++) {
			editRemoveOrAdd = rand.nextInt(numberOfObjects);
			int index = randNumList.get(i);
			Introspector newObject = null;
			try {
				newObject = pop.populateRecursively((Introspector)child.clone(), min, max, false);
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
					| InstantiationException | JSONException | IOException | JAXBException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (list.size() < numberOfObjects){
				if (editRemoveOrAdd == 0 && index < list.size()){
					if (newObject != null) {
						list.set(index, newObject.getUnderlyingObject());
					}
				}else if (editRemoveOrAdd == 1 && index < list.size()){
					if (list.size() != 1) {
						list.remove(index);
					}
				}else if (editRemoveOrAdd == 2 && list.size() + 1 <= numberOfObjects){
					if (newObject != null) {
						list.add(index, newObject.getUnderlyingObject());
					}
				}
			}
			
		}
		
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean createComplexObjIfNull() {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public int createComplexListSize(Introspector parent, Introspector child) {
		return 0;
	}

}