Skip to content

Commit

Permalink
Merge pull request #26 from virtualansoftware/excel-support
Browse files Browse the repository at this point in the history
Excel support
  • Loading branch information
elans3 authored Mar 15, 2021
2 parents 0b22a96 + 060a169 commit a79f8e1
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 65 deletions.
4 changes: 2 additions & 2 deletions modules/idaithalam/conf/cucumblan.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ service.api=https://localhost:8800
virtualan.data.load=idaithalam_post_v4.json
virtualan.data.heading=idaithalam_post_v4.json
virtualan.data.type=POSTMAN
service.api.okta_token=https://localhost:8080
service.api.okta=OKTA
service.api.okta_token.resource=https://localhost:8080
#service.api.okta - not needed
2 changes: 1 addition & 1 deletion modules/idaithalam/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<name>idaithalam-platform-testing</name>
<version>1.1.4-SNAPSHOT</version>
<properties>
<cucumblan.version>1.1.2-SNAPSHOT</cucumblan.version>
<cucumblan.version>1.1.2</cucumblan.version>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

public class FeatureFileMapping {
public FeatureFileMapping(String feature,
List<Item> items, String okta) {
List<Item> items) {
this.feature = feature;
this.items = items;
this.okta = okta;
}

/**
Expand All @@ -19,9 +18,5 @@ public FeatureFileMapping(String feature,
* The Items.
*/
List<Item> items;
/**
* The Okta.
*/
String okta;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@
import io.virtualan.idaithalam.core.UnableToProcessException;
import io.virtualan.idaithalam.core.contract.validator.FeatureFileGenerator;
import io.virtualan.idaithalam.core.domain.Item;
import java.util.UUID;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.json.support.Status;
import net.masterthought.cucumber.presentation.PresentationMode;

import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;
import org.apache.poi.ss.formula.functions.T;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.json.support.Status;
import net.masterthought.cucumber.presentation.PresentationMode;


/**
Expand Down Expand Up @@ -246,7 +248,7 @@ private static void generateFeatureFile(String path) throws IOException, UnableT
Mustache mustache = mf.compile("virtualan-contract.mustache");
FileOutputStream outputStream = new FileOutputStream(path+"/feature/virtualan-contract."+i+".feature");
Writer writer = new OutputStreamWriter(outputStream);
mustache.execute(writer, new FeatureFileMapping(getTitle(featureTitle, i, feature), items.get(i),okta)).flush();
mustache.execute(writer, new FeatureFileMapping(getTitle(featureTitle, i, feature), items.get(i))).flush();
writer.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.virtualan.idaithalam.core.contract.validator;

import io.cucumber.java.sl.In;
import io.virtualan.idaithalam.config.IdaithalamConfiguration;
import java.io.BufferedReader;
import java.io.File;
Expand All @@ -18,6 +19,7 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
Expand Down Expand Up @@ -95,48 +97,57 @@ public static void createCollection(String basePath, List<String> generatedTestC
InputStream stream = getInputStream(basePath, excelFilePath);
try {
if (stream != null) {
Workbook workbook = new XSSFWorkbook(stream);
Sheet firstSheet = workbook.getSheetAt(0);
Map<Integer, String> headerMap = new HashMap<>();
Iterator<Row> iterator = firstSheet.iterator();
int rowCount = 0;
Map<String, String> excludeResponseMap = new HashMap<>();
Map<String, String> cucumblanEnv = new HashMap<>();
Map<String, String> cucumblanMap = getCucumblan();
JSONArray virtualanArray = new JSONArray();
while (iterator.hasNext()) {
int count = 0;
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
Map<String, String> dataMap = new HashMap<>();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (rowCount == 0) {
headerMap.put(count++, cell.getStringCellValue());
} else {
String key = headerMap.get(cell.getColumnIndex());
if ("HttpStatusCode".equalsIgnoreCase(key)) {
dataMap.put(key, String.valueOf((int) cell.getNumericCellValue()));
Workbook workbook = new XSSFWorkbook(stream);
for (int sheet = 0; sheet < workbook.getNumberOfSheets(); sheet++) {
Sheet firstSheet = workbook.getSheetAt(sheet);
JSONArray virtualanArray = new JSONArray();
Map<Integer, String> headerMap = new HashMap<>();
int rowCount = 0;
for (Iterator<Row> iterator = firstSheet.iterator(); iterator.hasNext(); ) {
int count = 0;
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
Map<String, String> dataMap = new HashMap<>();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (rowCount == 0) {
headerMap.put(count++, cell.getStringCellValue());
} else {
dataMap.put(key, cell.getStringCellValue());
String key = headerMap.get(cell.getColumnIndex());
if ("HttpStatusCode".equalsIgnoreCase(key)) {
dataMap.put(key, String.valueOf((int) cell.getNumericCellValue()));
} else {
dataMap.put(key, cell.getStringCellValue());
}
}
}
if (rowCount > 0 && (generatedTestCaseList == null || generatedTestCaseList.isEmpty()
|| generatedTestCaseList
.contains(dataMap.get("TestCaseName")))) {
JSONObject object = buildVirtualanCollection(basePath, generatedPath, rowCount,
cucumblanMap,
excludeResponseMap,
dataMap);
virtualanArray.put(object);
}
rowCount++;
}
if (rowCount > 0 && (generatedTestCaseList == null || generatedTestCaseList.isEmpty() || generatedTestCaseList
.contains(dataMap.get("TestCaseName")))) {
JSONObject object = buildVirtualanCollection(basePath, generatedPath, rowCount, cucumblanMap,
excludeResponseMap,
dataMap);
virtualanArray.put(object);
if (IdaithalamConfiguration.isWorkFlow()) {
createIdaithalamProcessingFile(generatedPath, rowCount, cucumblanMap, virtualanArray,
firstSheet.getSheetName() + "_WORKFLOW",
"WORKFLOW:" + firstSheet.getSheetName());
}
rowCount++;
}
if (IdaithalamConfiguration.isWorkFlow()) {
createIdaithalamProcessingFile(generatedPath, rowCount, cucumblanMap, virtualanArray,
"WORKFLOW",
"WORKFLOW");
log.info(virtualanArray.toString());
}
log.info(virtualanArray.toString());
createPrpos(generatedPath, cucumblanMap, "cucumblan.properties");
InputStream streamEnv = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("cucumblan-env.properties");
if (streamEnv != null) {
createPrpos(generatedPath, streamEnv, "cucumblan-env.properties");
}
if (!excludeResponseMap.isEmpty()) {
createPrpos(generatedPath, excludeResponseMap, "exclude-response.properties");
}
Expand All @@ -151,6 +162,7 @@ public static void createCollection(String basePath, List<String> generatedTestC
"Unable to create collection for the given excel file " + excelFilePath + " >>> " + e
.getMessage());
}

}

/**
Expand Down Expand Up @@ -186,6 +198,10 @@ public static InputStream getInputStream(String basePath, String fileNameWithSub
.getResourceAsStream(fileNameWithSubCategory);
}
}
if (stream == null) {
log.error(" File is missing(" + basePath + ") : " + fileNameWithSubCategory);
System.exit(-1);
}
return stream;
}

Expand Down Expand Up @@ -222,6 +238,10 @@ public static String getFileAsString(String basePath, String fileNameWithSubCate
.getResourceAsStream(fileNameWithSubCategory);
}
}
if (stream == null) {
log.error(" File is missing(" + basePath + ") : " + fileNameWithSubCategory);
System.exit(-1);
}
return convertStreamToString(stream);
}

Expand All @@ -247,6 +267,8 @@ private static JSONObject buildVirtualanCollection(String basePath, String gener
virtualanObj.put("scenario", dataMap.get("TestCaseNameDesc"));
createProcessingType(dataMap, paramsArray, "StoreResponseVariables", "STORAGE_PARAM");
createProcessingType(dataMap, paramsArray, "AddifyVariables", "ADDIFY_PARAM");
getValue("tags", dataMap, virtualanObj);
getValue("security", dataMap, virtualanObj);
if (dataMap.get("HTTPAction") != null) {
virtualanObj.put("method",
dataMap.get("HTTPAction").toUpperCase());
Expand All @@ -268,14 +290,21 @@ private static JSONObject buildVirtualanCollection(String basePath, String gener
if (!IdaithalamConfiguration.isWorkFlow()) {
JSONArray virtualanArray = new JSONArray();
virtualanArray.put(virtualanObj);
createIdaithalamProcessingFile(generatedPath, rowCount, cucumblanMap, virtualanArray, dataMap.get("TestCaseName"),
createIdaithalamProcessingFile(generatedPath, rowCount, cucumblanMap, virtualanArray,
dataMap.get("TestCaseName"),
virtualanObj.get("scenario") != null ? virtualanObj.get("scenario").toString()
: "Not defined");
log.info(virtualanArray.toString());
}
return virtualanObj;
}

private static void getValue(String key, Map<String, String> dataMap, JSONObject virtualanObj) {
if (dataMap.get(key) != null) {
virtualanObj.put(key, dataMap.get(key));
}
}

private static void builHttpStausCode(Map<String, String> dataMap, JSONObject virtualanObj) {
if (dataMap.get("HttpStatusCode") != null) {
virtualanObj.put("httpStatusCode", dataMap.get("HttpStatusCode"));
Expand Down Expand Up @@ -306,7 +335,7 @@ private static void createProcessingType(Map<String, String> dataMap,
JSONArray paramsArray, String requestProcessingType, String param) {
if (dataMap.get(requestProcessingType) != null) {
String[] processTypes = dataMap.get(requestProcessingType).split(";");
for(String keyValue : processTypes) {
for (String keyValue : processTypes) {
String[] processType = keyValue.split("=");
if (processType.length == 2) {
buildParam(processType[0], processType[1], paramsArray, param);
Expand All @@ -320,17 +349,17 @@ private static JSONObject createStoreProcessingType(Map<String, String> dataMap,
JSONObject virtualanObjParam = new JSONObject();
if (dataMap.get(requestProcessingType) != null) {
String[] processType = dataMap.get(requestProcessingType).split(";");
for( String store : processType) {
for (String store : processType) {
virtualanObjParam.put(store.split("=")[0], store.split("=")[1]);
}
return virtualanObjParam;
return virtualanObjParam;
}
return null;
}


private static void createIdaithalamProcessingFile(String generatedPath, int rowCount,
Map<String, String> cucumblanMap, JSONArray virtualanArray, String testcaseName,
Map<String, String> cucumblanMap, JSONArray virtualanArray, String testcaseName,
String scenario) {
String fileCreated = generateExcelJson(generatedPath, virtualanArray,
"Virtualan_" + testcaseName + "_" + rowCount);
Expand All @@ -352,6 +381,11 @@ private static void buildUrl(Map<String, String> cucumblanMap,
virtualanObj.put("url", aURL.getPath());
cucumblanMap.put("service.api." + resource,
aURL.getProtocol() + "://" + aURL.getAuthority());
String okta = virtualanObj.optString("security");
if (okta != null && !okta.isEmpty() && okta.split("=").length == 2) {
cucumblanMap.put("service.api.okta_token." + resource, okta.split("=")[1]);
virtualanObj.put("security", "okta");
}
createQueryParam(aURL.getQuery(), paramsArray);
virtualanObj.put("resource", resource);
if (dataMap.get("ExcludeField") != null) {
Expand All @@ -366,6 +400,22 @@ private static String getResource(String resource) {
return "default";
}

private static void createPrpos(String path, InputStream stream, String fileName) {
try {
Properties props = new Properties();
//Populating the properties file
props.load(stream);
//Instantiating the FileInputStream for output file
FileOutputStream outputStrem = new FileOutputStream(
path + File.separator + fileName);
//Storing the properties file
props.store(outputStrem, "This is a " + fileName + " properties file");
log.info(fileName + " Properties file created......");
} catch (IOException e) {
log.warn(" Unable to generate " + fileName + " properties " + e.getMessage());
}
}

private static void createPrpos(String path, Map<String, String> propsMap, String fileName) {
try {
Properties props = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.virtualan.idaithalam.core.domain.AvailableParam;
import io.virtualan.idaithalam.core.domain.Item;
import io.virtualan.mapson.Mapson;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -215,10 +214,20 @@ public static List<Item> createFeatureFile(JSONArray arr, String path) throws IO
private static Item getItem(JSONObject object, String path) throws IOException {
Item item = new Item();
extractedInput(object, item, path);
item.setTags(object.optString("tags"));
item.setHttpStatusCode(object.optString("httpStatusCode"));
item.setMethod(object.optString("method"));
item.setAction(object.optString("method").toLowerCase());
item.setResource(object.optString("resource"));
if(object.optString("security") != null && !object.optString("security").isEmpty()){
if("okta".equalsIgnoreCase(object.optString("security"))) {
item.setOkta(object.optString("security"));
}else if("basicAuth".equalsIgnoreCase(object.optString("security"))) {
item.setBasicAuth(object.optString("security"));
}else {
log.warn("Unknown security setup");
}
}
extractedScenario(object, item);
List<AvailableParam> availableParams = getAvailableParamList(object);
item.setAvailableParams(availableParams);
Expand Down
Loading

0 comments on commit a79f8e1

Please sign in to comment.