diff --git a/src/main/java/sourcecode/MockUp/MockUp.java b/src/main/java/sourcecode/MockUp/MockUp.java index 1793d31..e58d51e 100644 --- a/src/main/java/sourcecode/MockUp/MockUp.java +++ b/src/main/java/sourcecode/MockUp/MockUp.java @@ -5,6 +5,7 @@ import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.URLDecoder; import java.util.ArrayList; +import java.util.HashMap; import org.apache.commons.io.IOUtils; import org.apache.http.Consts; @@ -71,6 +72,26 @@ public class MockUp String method = t.getRequestMethod(); String url = t.getRequestURI().toString(); url = URLDecoder.decode(url, "UTF-8"); + String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8); + + + int limit = 0; + + if(!getKey("limit", url).equals("")) + { + try{ + limit = Integer.parseInt(getKey("limit", url)); + }catch(Exception e) + { + + } + } + + + String[] fields = new String[0]; + if(!getKey("fields", url).equals("")) + fields = getKey("fields", url).split(","); + // System.out.println(t.getRequestMethod()); // if(!method.equals(GET)) @@ -96,7 +117,7 @@ public class MockUp // System.out.println(t.getRequestURI()); - String keyData = findKey(method,url,IOUtils.toString(t.getRequestBody(),Consts.UTF_8)); + String keyData = findKey(method,url,bodyData); String collectionName = getSuffixTableName(url); JSONObject responsJSON = new JSONObject(); @@ -139,7 +160,7 @@ public class MockUp { - ArrayList responseList = getDBData(keyData,collectionName,method); + ArrayList responseList = getDBData(keyData,collectionName,method,bodyData); @@ -152,6 +173,7 @@ public class MockUp { if(method.equals(GET) && responseList.size() > 1) { + System.out.println("Multi Data => Merge"); ArrayList resultData = new ArrayList(); for(int i=0;i 0) && (value.get("resultData").toString().substring(0,1).equals("["))) -// { -//// Object test = gson.fromJson(value.get("resultData").toString(), Object.class); -//// System.out.println(test); -// -//// value.get("resultData").toString() -// JSONObject resultlist = new JSONObject("{'test' : "+value.get("resultData").toString()+"}"); -//// System.out.println(resultlist.get("test")); -// JSONArray JSONArrayData = value.getJSONArray("resultData"); -// System.out.println("aaaaaaaaaaaaaaaaaaaa"+JSONArrayData.get(0)); -//// for(int n=0;n Direct"); JSONObject rowJSON = new JSONObject(responseList.get(0)); if(rowJSON.has("value")) responsJSON = (JSONObject) rowJSON.get("value"); @@ -224,6 +225,13 @@ public class MockUp } + //limit + responsJSON = limitData(responsJSON,limit); + + //field + responsJSON = FieldsData(responsJSON,fields); + + response = responsJSON.toString(); @@ -260,7 +268,6 @@ public class MockUp } return value; } - private static void MongoConnector(String db) { @@ -297,22 +304,34 @@ public class MockUp } } - private static ArrayList getDBData(String keyData,String collectionName,String method) + private static ArrayList getDBData(String keyData,String collectionName,String method,String bodyData) { ArrayList returnData = new ArrayList(); - collectionName = method.toLowerCase()+"_"+collectionName; +// collectionName = method.toLowerCase()+"_"+collectionName; System.out.println("===> Find Normal"); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("key", keyData); - returnData = getDB(basicDBObject,collectionName); + returnData = getDB(basicDBObject,method.toLowerCase()+"_"+collectionName); if(returnData.size() == 0) { System.out.println("===> Go to Main flow"); BasicDBObject basicDBObject2 = new BasicDBObject(); basicDBObject2.put("key", ""); - returnData = getDB(basicDBObject2,collectionName); + returnData = getDB(basicDBObject2,method.toLowerCase()+"_"+collectionName); + +// if(method.equals(POST) && returnData.size()>0) +// { +// //post main flow +// JSONObject rowJSON = new JSONObject(returnData.get(0)); +// rowJSON.remove("_id"); +// JSONObject value = rowJSON.getJSONObject("value"); +// value.put("resultData", new JSONObject(bodyData)); +// +// Document doc = Document.parse(rowJSON.toString()); +// insertDB(doc,GET.toLowerCase()+"_"+collectionName); +// } } @@ -400,4 +419,180 @@ public class MockUp System.out.println("Update Mongo"); } + private static void insertDB(Document doc,String collectionName) + { +// System.out.println("Start Update Mongo"); + + MongoCollection collection = database.getCollection(collectionName); + collection.insertOne(doc); + + System.out.println("Insert Mongo"); + } + + private static JSONObject limitData(JSONObject responsJSON,int limit) + { + if(limit != 0) + { + int size = 0; + try{ + System.out.println("Limit : "+limit); + JSONArray JSONArrayData = responsJSON.getJSONArray("resultData"); + + ArrayList limitResultData = new ArrayList(); + size = JSONArrayData.length(); + for(int i=0;i 0 && responsJSON.has("resultData")) + { + String select = ""; + for(String row:fileds) + select += ","+row; + System.out.println("Select Fields => "+select.substring(1)); + + try{ + JSONArray jsonArray = responsJSON.getJSONArray("resultData"); + JSONArray newJsonArray = new JSONArray(); + for(int i=0;i 0) + { + JSONObject newResponse = new JSONObject(); + + for(String row:fileds) + { + if(json.has(row)) + newResponse.put(row, json.get(row)); + + } + returnData = newResponse; + }else + { + returnData = json; + } + + + return returnData; + } + + public static ArrayList getKeyUrl(String url) { + + ArrayList arrData = new ArrayList(); + + String[] arr = url.split("[/]"); + + for(int i = 0; i < arr.length; i++) { + + if(arr[i].contains("?")) + arr[i] = arr[i].substring(0, arr[i].indexOf("?")); + + if(!arr[i].equals("")) + arrData.add(arr[i]); + } + + return arrData; + + } + + public static HashMap> getKeyFilter(String url) { + + String key = "filter="; + String value = null; + HashMap> hashMap = new HashMap>(); + + value = url.substring(url.indexOf(key)+key.length()); + value = value.substring(0, value.indexOf(")&")+1); + String[] array = value.split("[()]"); + + for(int i = 0; i < array.length; i++) { + + String[] temp = array[i].split("="); + + if(temp.length > 1) { +// System.out.println(temp[0]+" "+temp[1]); + + if(hashMap.containsKey(temp[0])) { + hashMap.get(temp[0]).add(temp[1]); + } else { + ArrayList tempArr = new ArrayList<>(); + tempArr.add(temp[1]); + hashMap.put(temp[0], tempArr); + } + } + } + + return hashMap; + + } + + public static String getKey(String key, String url) { + + key = key + "="; + String value = ""; + + if(url.contains(key)) { + value = url.substring(url.indexOf(key)+key.length()); + if(value.indexOf("&") != -1) + value = value.substring(0, value.indexOf("&")); + } + + return value; + } + + } -- libgit2 0.21.2