aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/testsuitegeneration/AbstractWriter.java
blob: c440c1458bd08fb99b3e7e6d2b8f1c77638a438b (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*-
 * ============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.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openecomp.aai.introspection.Introspector;
import org.openecomp.aai.introspection.IntrospectorFactory;
import org.openecomp.aai.introspection.Loader;
import org.openecomp.aai.introspection.LoaderFactory;
import org.openecomp.aai.introspection.ModelType;
import org.openecomp.aai.introspection.Version;
import org.openecomp.aai.logging.LogLineBuilder;
import org.openecomp.aai.rest.CustomJacksonJaxBJsonProvider;
import org.openecomp.aai.testing.LocateEndPoints;
import org.openecomp.aai.testsuitegeneration.TestSuite.TestSuiteBuilder;
import org.springframework.web.util.UriUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.CaseFormat;
import com.google.common.collect.Multimap;

import freemarker.template.Configuration;
import freemarker.template.Template;

public abstract class AbstractWriter {

	protected final String PROPERTIES_XML = "properties_xml.ftl";
	
	protected final String TEMPLATE_PATH = "src/main/java/org/openecomp/aai/testsuitegeneration/";
	
	public static final String USERNAME_PLACEHOLDER = "#!#username.placeholder#!#";
	
	public final Integer START_PORT = 42671;
	protected Map<String, Object> supplimentalMap = new HashMap<>();
	protected Configuration configuration = new Configuration();
	protected String outputPath;
	protected Version apiVersion;
	protected Version currentApiVersion;
	protected String tearDownPath;
	protected String tearDownPathChildren;
	protected String deletePage;
	protected String parentCleanUpPage;
	protected String classPackagePath;
	protected String cleanUpPage;
	protected Multimap<String, String> DeleteScope;
	protected Multimap<String, String> EdgeRules;
	protected final LogLineBuilder llBuilder = new LogLineBuilder();
	protected final Loader loader;

	
	/**
	 * Instantiates a new abstract writer.
	 *
	 * @param file the file
	 * @param builder the builder
	 */
	// Abstract constructor
	public AbstractWriter(File file, TestSuiteBuilder builder) {
		this.outputPath = file.getAbsolutePath();
		this.apiVersion = builder.getApiVersion();
		this.currentApiVersion = builder.getCurrentApiVersion();
		this.DeleteScope = builder.getDeleteScope();
		this.EdgeRules = builder.getEdgeMap();
		this.classPackagePath = builder.getPojoPackage();
		this.deletePage = "";
		this.tearDownPath = "";
		this.parentCleanUpPage = "";
		this.loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, apiVersion, llBuilder);
		createVersionSuite();
	}
	
	
	/**
	 * Creates a suite for the writer's specified API version.
	 */
	private void createVersionSuite() {
		Map<String, Object> suiteMap = new HashMap<>();
		
		// Set the PUT expected result
		String createStatus = "200";
		Pattern p = Pattern.compile("v(\\d+)");
		Matcher m = p.matcher(apiVersion.toString());
		Integer versionNumber = 0;
		if (m.find()) {
			versionNumber = Integer.parseInt(m.group(1));
		}
		
		createStatus = "201";
		// Add the placeholder values to the map
		suiteMap.put("version", apiVersion);
		suiteMap.put("currentVersion", currentApiVersion);
		suiteMap.put("createStatus", createStatus);
		suiteMap.put("type", "suite");
		suiteMap.put("slimPort", START_PORT + (versionNumber * 20));
		
		// Create the directory
		File dir = new File(this.outputPath);
		dir.mkdir();
		
		// Process the freemarker templates
		this.processDataModel("version_suite_content_txt.ftl", "content.txt", dir, suiteMap);
		this.processDataModel("properties_xml.ftl", "properties.xml", dir, suiteMap);
	}
		
	
	/**
	 * Generates the necessary pages to recursively clean up children objects.
	 *
	 * @param path the path
	 * @param fitnessePath the fitnesse path
	 * @param topLevel the top level
	 * @param lep the lep
	 * @param additionalName the additional name
	 * @param urlVariableName the url variable name
	 * @return the string
	 * @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 generateRecursiveCleanUp(String path, String fitnessePath, Introspector topLevel, LocateEndPoints lep, String additionalName, String urlVariableName) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		String childPath = "";
		String fitnessePageName =  CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, topLevel.getDbName());

		String folderName = fitnessePageName + "AdditionalTests";
		String additionalPath = fitnessePath + "/" + folderName;
		File dir = new File(additionalPath);
		dir.mkdir();
		Map<String, Object> additionalMap = new HashMap<>();
		additionalMap.put("type", "static");
		additionalMap.put("skip", "true");
		processDataModel("static_page_content_txt.ftl", "content.txt", dir, additionalMap);
		processDataModel("properties_xml.ftl", "properties.xml", dir, additionalMap);
		List<String> pages = findCascadeDeleteChildren(topLevel, additionalPath, path, lep, additionalName);
		
		Map<String, Object> map = new HashMap<>();		
		
		String testPageName = "ChildrenTearDown" + additionalName;
		String tearDownPath = additionalPath + "/" + testPageName;
		dir = new File(tearDownPath);
		dir.mkdir();
		
		map.put("type", "test");
		map.put("pages", pages);
		map.put("parentCleanUpPage", ".AAI.Tests.GenericTests.FauxRecursiveDelete");
		map.put("errorCheckPage", ".AAI.Tests.GenericTests.DeleteErrorCheck");
		String decodedUrl = urlVariableName;
		try {
			decodedUrl = UriUtils.decode(decodedUrl, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		map.put("urlVariableName", this.replaceUsernamePlaceholders(decodedUrl));
		this.processDataModel("recursive_teardown_content_txt.ftl", "content.txt", dir, map);
		this.processDataModel("properties_xml.ftl", "properties.xml", dir, map);
		
		
		
		return folderName + "." + testPageName;
	}
	
	
	/**
	 * Searches for children to be deleted via cascade delete.
	 *
	 * @param obj the obj
	 * @param fitNessePath the fit nesse path
	 * @param restPath the rest path
	 * @param lep the lep
	 * @param additionalName the additional name
	 * @return the list
	 */
	protected List<String> findCascadeDeleteChildren(Introspector obj, String fitNessePath, String restPath, LocateEndPoints lep, String additionalName) {
		List<String> list = new ArrayList<>();
		try {
			findHelper(obj, list, fitNessePath, restPath, lep, additionalName);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	/**
	 * Helper to the find method.
	 *
	 * @param obj the obj
	 * @param list the list
	 * @param fitNessePath the fit nesse path
	 * @param restPath the rest path
	 * @param lep the lep
	 * @param additionalName the additional name
	 * @throws IllegalAccessException the illegal access exception
	 * @throws IllegalArgumentException the illegal argument exception
	 * @throws InvocationTargetException the invocation target exception
	 * @throws InstantiationException the instantiation exception
	 * @throws UnsupportedEncodingException the unsupported encoding exception
	 */
	protected void findHelper(Introspector obj, List<String> list, String fitNessePath, String restPath, LocateEndPoints lep, String additionalName) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, UnsupportedEncodingException {
		
		if (obj == null) {
			return;
		}
		List<String> properties = obj.getProperties();
		properties.remove("relationship-list");
		for (String prop : properties) {
			
			if (obj.isComplexType(prop)) {
				Introspector child = IntrospectorFactory.newInstance(obj.getModelType(), obj.getValue(prop), llBuilder);
				
				findHelper(IntrospectorFactory.newInstance(obj.getModelType(), obj.getValue(prop), llBuilder), list, fitNessePath, restPath, lep, additionalName);
			} else if (obj.isListType(prop)) {
				if (DeleteScope.get(
						CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, this.trimClassName(obj.getGenericType(prop))))
						.contains("CASCADE_TO_CHILDREN")) {
					List<?> children = (List<?>)obj.getValue(prop);
					Introspector temp = null;
					List<String> urls = new ArrayList<>();
					for (Object child : children) {
						temp = IntrospectorFactory.newInstance(obj.getModelType(), child, llBuilder);
						String decodedUrl = restPath + temp.getURI();
						try {
							decodedUrl = UriUtils.decode(decodedUrl, "UTF-8");
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
						urls.add(replaceUsernamePlaceholders(decodedUrl));
					}
					
					Map<String, Object> map = new HashMap<>();

					map.put("deleteChildren", ".AAI.Tests.GenericTests.CleanUp");
					map.put("urls", urls);
					map.put("type", "test");
					String childrenTestPage = "RemoveChildren" + additionalName;
					String removeChildrenPath = fitNessePath + "/" + childrenTestPage;
					File dir = new File(removeChildrenPath);
					dir.mkdir();
					this.processDataModel("remove_children_content_txt.ftl", "content.txt", dir, map);
					this.processDataModel("properties_xml.ftl", "properties.xml", dir, map);
					list.add(childrenTestPage);
				} else {
					//still wrong 
					Introspector temp = obj.newIntrospectorInstanceOfNestedProperty(prop);
					if (this.hasChildren(temp) && DeleteScope.get(
						CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, this.trimClassName(obj.getGenericType(prop))))
						.contains("THIS_NODE_ONLY")) {
						List<?> children = (List<?>)obj.getValue(prop);
						String result = "";
						for (int i = 0; i < children.size(); i++) {
							Introspector child = IntrospectorFactory.newInstance(obj.getModelType(), children.get(i), llBuilder);
						
							try {
								result = generateRecursiveCleanUp(restPath + child.getURI(), fitNessePath, child, lep, additionalName + new Integer(i).toString(), "!-" + restPath + child.getURI() + "-!");
							} catch (NoSuchMethodException | SecurityException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							list.add(result);

						}

					}
				}
				
			}
		}
		
	}
	
	/**
	 * Checks for children.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	protected boolean hasChildren(Introspector obj) {
		
		for (String prop : obj.getProperties()) {
			if (!prop.equals("relationship-list")) {

				if (obj.isComplexType(prop) || (obj.isListType(prop) && obj.isComplexGenericType(prop))) {
					return true;
				}
			}
		}
		
		return false;
	}
	
	
	/**
	 * Processes the template file through freemarker.
	 *
	 * @param templateFilename The filename of the template to process.
	 * @param outputFilename THe filename of the output file to generate.
	 * @param outputDirectory The directory of the output file.
	 * @param variableMap The variable map where key is the placeholder variable name.
	 */
	protected void processDataModel(String templateFilename, String outputFilename, File outputDirectory, Map<String, Object> variableMap) {

		try {
			Template template = configuration.getTemplate(TEMPLATE_PATH + templateFilename);
			Writer file = new FileWriter(new File(outputDirectory.getPath() + "/" + outputFilename));
			template.process(variableMap, file);
			file.flush();
		} catch(Exception e) {
			System.out.println("Failed to process file {" + outputFilename + "}");
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Marshals an object into a JSON string.
	 *
	 * @param obj The object to marshal
	 * @return A string representation of the object in JSON format.
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	protected String jsonMarshal(Object obj) throws IOException {
		CustomJacksonJaxBJsonProvider provider = new CustomJacksonJaxBJsonProvider();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		provider.writeTo(obj, obj.getClass(), null, null, null, null, os);
		String output = new String(os.toByteArray(), "UTF-8");
		ObjectMapper mapper = provider.getMapper();
		Object json = mapper.readValue(output, obj.getClass());
		output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
		return output;
	}
	
	
	/**
	 * Retrieves the root to FitNesse from the full path.
	 *
	 * @param fullPath The full output path.
	 * @return The root to FitNesseRoot
	 */
	protected String getFitNesseRoot(String fullPath) {
		String result = fullPath;
		Pattern p = Pattern.compile("Root(?:/|\\\\)(.*)");
		Matcher m = p.matcher(fullPath);
		if(m.find()) {
			result = m.group(1);
		}
		return result;
	}
	
	
	/**
	 * Converts an UpperCamel class name to a lower-hyphen class name.
	 * @param className The class name in UpperCamel format.
	 * @return The class name is lower-hyphen format.
	 */
	protected String getHyphenatedClassName(String className) {
		String output = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className);
		
		// Special case for cvlan-tag
		if(output.equals("cvlan-tag-entry")) {
			output = "cvlan-tag";
		}
		
		return output;
	}
	
	/**
	 * Checks if is plural.
	 *
	 * @param word the word
	 * @return true, if is plural
	 */
	protected boolean isPlural(String word) {
		return word.matches(".*?(es|(?<!s)s)$");
	}
	
	/**
	 * Trim class name.
	 *
	 * @param name the name
	 * @return the string
	 */
	protected String trimClassName(String name) {
		
		return name.substring(name.lastIndexOf('.') + 1, name.length());
	}
	
	/**
	 * Gets the key from path.
	 *
	 * @param path the path
	 * @return the key from path
	 */
	protected String getKeyFromPath(String path) {
		Pattern p = Pattern.compile("(\\{(.+?)\\}\\/?)+$");
		Matcher m = p.matcher(path);
		String key = "";
		if (m.matches()) {
			key = m.group();
		}
		return key;
	}
	protected String encodeUsernamePlaceholders(String body) {
		String encode = "";
		try {
			encode = UriUtils.encodePath(USERNAME_PLACEHOLDER, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return body.replace(USERNAME_PLACEHOLDER, encode);
	}
	protected String decodeUserNamePlaceholders(String body) {
		String decode = "";
		try {
			decode = UriUtils.decode(body, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return decode;
	}
	/**
	 * Replace username placeholders.
	 *
	 * @param body the body
	 * @return the string
	 */
	protected String replaceUsernamePlaceholders(String body) {
		return body.replace(USERNAME_PLACEHOLDER, "--!${username}!-");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}