aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/testsuitegeneration/TestSuiteWriter.java
blob: 9b35aa059aee67a7e534ff6f57496ec7c80f893b (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*-
 * ============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.testsuitegeneration;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ws.rs.core.Response.Status;
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.rest.ueb.NotificationEvent;
import org.openecomp.aai.rest.ueb.UEBNotification;
import org.openecomp.aai.serialization.db.EdgeRule;
import org.openecomp.aai.serialization.db.MultiplicityRule;
import org.openecomp.aai.testing.InjectResourceVersion;
import org.openecomp.aai.testing.LocateEndPoints;
import org.openecomp.aai.testing.UpdateObject;
import org.openecomp.aai.testsuitegeneration.TestSuite.TestSuiteBuilder;
import org.springframework.web.util.UriUtils;

import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;

import edu.emory.mathcs.backport.java.util.Arrays;

public class TestSuiteWriter extends AbstractWriter {

	protected final String END_POINT_SUITE_CONTENT = "end_point_suite_content_txt.ftl";
	protected final String CHILD_TEST_CONTENT = "child_test_content_txt.ftl";
	
	protected final String[] TOP_ENTITY_EXCEPTIONS = new String[] { "tenant", "volume-group", "dvs-switch", "image", "flavor", "oam-network", "availability-zone"};
	protected final Set<String> EXCEPTION_SET = new HashSet<String>(Arrays.asList(TOP_ENTITY_EXCEPTIONS));
	
	/**
	 * Instantiates a new test suite writer.
	 *
	 * @param file the file
	 * @param builder the builder
	 */
	public TestSuiteWriter(File file, TestSuiteBuilder builder) {
		
		// Just call the super constructor
		super(file, builder);
		
	}

	/**
	 * Creates a suite for the parent object creation/teardown.
	 *
	 * @param path the path
	 * @param parentObject the parent object
	 * @param lep the lep
	 * @param genericPath the generic path
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws IllegalAccessException the illegal access exception
	 * @throws IllegalArgumentException the illegal argument exception
	 * @throws InvocationTargetException the invocation target exception
	 * @throws NoSuchMethodException the no such method exception
	 * @throws SecurityException the security exception
	 */
	protected String createParentSuite(String path, Introspector parentObject, LocateEndPoints lep, String genericPath) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		if(genericPath == null) genericPath = path;
		
		// Extract the object name to create the suite path
		String name = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, parentObject.getDbName());
		String suitePath = outputPath + "/" + name + "Suite";
		
		// Create the suite directory
		File suiteDir = new File(suitePath);
		suiteDir.mkdir();
		
		// Randomly generate field values to update the parent object
		InjectResourceVersion inject = new InjectResourceVersion();
		Introspector injectedObject = inject.addResourceVersionDeep(parentObject);
		Introspector updatedParentObject = (new UpdateObject()).randomlyUpdateFieldValues(injectedObject);

		String populatedPath = (String)lep.populatePath(parentObject, path).get(0);
		//check the child map
		String lowerHyphenName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, name);
		Pattern p = Pattern.compile("^[\\w\\-]+?,,");
		Matcher m = null;
		boolean hasChildren = this.hasChildren(parentObject);

		// Create the variable mapping for the suite
		Map<String, Object> suiteMap = new HashMap<>();
		
		if (hasChildren && DeleteScope.get(lowerHyphenName).contains("THIS_NODE_ONLY")) {

			try {
				tearDownPath = generateRecursiveCleanUp(populatedPath, suitePath, updatedParentObject, lep, "", "${topLevelFeatureURL}");
				//tearDownPathChildren = generateRecursiveCleanUp(path, additionalPath, parentObject, lep, "OfParent");

			} catch (IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException
					| SecurityException e) {
				e.printStackTrace();
			} 
			parentCleanUpPage = this.tearDownPath;

		} else {
			parentCleanUpPage = ".AAI.Tests.GenericTests.SimpleParentCleanUp";
		}
		String encodedPath = this.encodeUsernamePlaceholders(populatedPath);
		encodedPath = encodedPath.replaceFirst("/aai/v\\d+/", "");
		UEBNotification notification = new UEBNotification(loader);
		Introspector uebObj = null;
		Introspector uebUpdatedObj = null;
		String uebLink = "";
		try {
			notification.createNotificationEvent("test-generation", Status.CREATED, new URI(encodedPath), parentObject, new HashMap<String, Introspector>());
			notification.createNotificationEvent("test-generation", Status.OK, new URI(encodedPath), updatedParentObject, new HashMap<String, Introspector>());
			List<NotificationEvent> events = notification.getEvents();
			uebObj = events.get(0).getObj();
			uebUpdatedObj = events.get(1).getObj();
			AddNamedPropWildcard addWildCard = new AddNamedPropWildcard(loader);
			addWildCard.process(uebObj);
			addWildCard.process(uebUpdatedObj);
			uebLink = "-!${address}!-"+new URI((String)events.get(0).getEventHeader().getValue("entity-link")).getPath();
			uebLink = this.decodeUserNamePlaceholders(uebLink);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AAIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String entity = this.getHyphenatedClassName(name);
		String topEntity = this.computeTopEntity(entity);
		suiteMap.put("topLevelBody", replaceUsernamePlaceholders(parentObject.marshal(true)));
		suiteMap.put("topLevelUpdatedBody", replaceUsernamePlaceholders(updatedParentObject.marshal(true)));
		suiteMap.put("topUebBody", replaceUsernamePlaceholders(uebObj.marshal(true)));
		suiteMap.put("topUebUpdatedBody", replaceUsernamePlaceholders(uebUpdatedObj.marshal(true)));
		suiteMap.put("topUebLink", replaceUsernamePlaceholders(uebLink));
		suiteMap.put("topLevelFeatureURL", replaceUsernamePlaceholders(populatedPath));
		suiteMap.put("type", "suite");
		suiteMap.put("entity", entity);
		suiteMap.put("topEntity", topEntity);
		suiteMap.put("topLevelCleanUpPage", parentCleanUpPage);
		
		Map<String, String> symLinks = new HashMap<>();
		symLinks.put("SuiteSetUp", ".AAI.Tests.GenericTests.SuiteSetUpTemplate");
		symLinks.put("SuiteTearDown", ".AAI.Tests.GenericTests.SuiteTearDownTemplate");
		suiteMap.put("symLinks", symLinks);
		// Process the freemarker files
		processDataModel(END_POINT_SUITE_CONTENT, "content.txt", suiteDir, suiteMap);
		processDataModel(PROPERTIES_XML, "properties.xml", suiteDir, suiteMap);
		
		// Create a placeholder page for the parent test
		String parentTest = suitePath + "/" + name + "Test";
		suiteDir = new File(parentTest);
		suiteDir.mkdir();
		suiteMap = new HashMap<>();
		suiteMap.put("type", "test");
		processDataModel("empty_context_txt.ftl", "content.txt", suiteDir, suiteMap);
		processDataModel(PROPERTIES_XML, "properties.xml", suiteDir, suiteMap);
		
		return suitePath;
	}
	
	/**
	 * Format test name.
	 *
	 * @param genericPath the generic path
	 * @return the string
	 */
	protected String formatTestName(String genericPath) {
		String dropSuiteName = genericPath.substring(genericPath.indexOf("}")+1);
		String removeVariables = dropSuiteName.replaceAll("\\/\\{.*?\\}", "");
		String[] split = removeVariables.split("/"); 
		List<String> result = new ArrayList<>();
		for (int i = 0; i < split.length; i++) {
			
			if (!this.isPlural(split[i])) {
				result.add(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, split[i]));
			}
		}
		String addHyphens = Joiner.on("-").join(result).replaceFirst("-", "");
		
		return addHyphens;
	}

	/**
	 * Adds the test case.
	 *
	 * @param suitePath the suite path
	 * @param path the path
	 * @param obj the obj
	 * @param genericPath the generic path
	 * @param lep the lep
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void addTestCase(String suitePath, String path, Introspector obj, String genericPath, LocateEndPoints lep) throws IOException {
		if (genericPath == null){
			genericPath = suitePath;
		}
		supplimentalMap.put("tearDownPath", this.tearDownPathChildren);

		this.addTestCase(suitePath, path, obj, supplimentalMap, CHILD_TEST_CONTENT, genericPath, lep);
	}
	
	/**
	 * Adds the test case.
	 *
	 * @param parentPath the parent path
	 * @param path the path
	 * @param obj the obj
	 * @param supplimentalMap the supplimental map
	 * @param templateName the template name
	 * @param genericPath the generic path
	 * @param lep the lep
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	protected void addTestCase(String parentPath, String path, Introspector obj, Map<String, Object> supplimentalMap,
			String templateName, String genericPath, LocateEndPoints lep) throws IOException {
		
		if (genericPath == null){
			genericPath = parentPath;
		}
		
		InjectResourceVersion inject = new InjectResourceVersion();
		Introspector injectedResource = inject.addResourceVersionDeep(obj);
		Introspector updatedObj = (new UpdateObject()).randomlyUpdateFieldValues(injectedResource);

		String name =  CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, obj.getDbName());
		String entityName = this.getHyphenatedClassName(name);
		String nameWithDepth = this.formatTestName(genericPath);

		if (!nameWithDepth.equals("")) {
			name = nameWithDepth;
		}
		String testPath = parentPath + "/" + name + "Test";

		File dir = new File(testPath);

		dir.mkdir();
		
		Map<String, Object> testMap = new HashMap<>();

		boolean hasChildren = this.hasChildren(obj);
		
		if (hasChildren && DeleteScope.get(obj.getDbName()).contains("THIS_NODE_ONLY")) {

			try {
				tearDownPath = generateRecursiveCleanUp(path, parentPath, updatedObj, lep, "", "${featureURL}");
				//tearDownPathChildren = generateRecursiveCleanUp(path, additionalPath, parentObject, lep, "OfParent");

			} catch (IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException
					| SecurityException e) {
				e.printStackTrace();
			} 
			this.deletePage = this.tearDownPath;		
		} else {
			this.deletePage = ".AAI.Tests.GenericTests.DeleteTop";
		}
		String testPagePath = ".AAI.Tests.GenericTests.GenericTest";
		try {
			Introspector parent = null;
			if (!genericPath.equals(obj.getFullGenericURI())) {
				String parentObjectPath = genericPath.replace(obj.getFullGenericURI(), "");
				parent = lep.createChildForPath(parentObjectPath, 1, 1);
				EdgeRule rule = org.openecomp.aai.serialization.db.EdgeRules.getInstance().getEdgeRule(parent.getDbName(), obj.getDbName());
				
				if (rule.getMultiplicityRule().equals(MultiplicityRule.ONE2ONE)) {
					testPagePath = ".AAI.Tests.GenericTests.PlaceholderTest";
				}
			}
		} catch (IllegalArgumentException | AAIException | IllegalAccessException | InvocationTargetException | InstantiationException | JSONException | JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String featureUrl = replaceUsernamePlaceholders(path);
		String encodedPath = this.encodeUsernamePlaceholders(path);
		encodedPath = encodedPath.replaceFirst("/aai/v\\d+/", "");

		UEBNotification notification = new UEBNotification(loader);
		Introspector uebObj = null;
		Introspector uebUpdatedObj = null;
		String uebLink = "";
		try {
			notification.createNotificationEvent("test-generation", Status.CREATED, new URI(encodedPath), obj, new HashMap<String, Introspector>());
			notification.createNotificationEvent("test-generation", Status.OK, new URI(encodedPath), updatedObj, new HashMap<String, Introspector>());
			List<NotificationEvent> events = notification.getEvents();
			uebObj = events.get(0).getObj();
			uebUpdatedObj = events.get(1).getObj();
			AddNamedPropWildcard addWildCard = new AddNamedPropWildcard(loader);
			addWildCard.process(uebObj);
			addWildCard.process(uebUpdatedObj);
			uebLink = "-!${address}!-"+new URI((String)events.get(0).getEventHeader().getValue("entity-link")).getPath();
			uebLink = this.decodeUserNamePlaceholders(uebLink);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AAIException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		
		
		testMap.put("body", replaceUsernamePlaceholders(obj.marshal(true)));
		testMap.put("updatedBody", replaceUsernamePlaceholders(updatedObj.marshal(true)));
		testMap.put("uebBody", replaceUsernamePlaceholders(uebObj.marshal(true)));
		testMap.put("uebUpdatedBody", replaceUsernamePlaceholders(uebUpdatedObj.marshal(true)));
		testMap.put("uebLink", replaceUsernamePlaceholders(uebLink));
		testMap.put("featureURL", featureUrl);
		testMap.put("type", "test");
		testMap.put("testPath", testPagePath);
		testMap.put("deletePage", this.deletePage);
		testMap.put("putPage", ".AAI.Tests.GenericTests.StandardPut");
		testMap.put("entity", entityName);
		testMap.putAll(supplimentalMap);

		this.processDataModel(templateName, "content.txt", dir, testMap);
		this.processDataModel(PROPERTIES_XML, "properties.xml", dir, testMap);
	}
	
	/**
	 * Compute top entity.
	 *
	 * @param entity the entity
	 * @return the string
	 */
	private String computeTopEntity(String entity) {
		String result = entity;
		if (!this.apiVersion.equals("v7")) {
			if (EXCEPTION_SET.contains(entity)) {
				result = "cloud-region";
			}
		}
		
		return result;
	}
}