package com.xxx.mobile.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Map;
import org.apache.commons.httpclient.util.URIUtil;
import org.openqa.selenium.remote.JsonToBeanConverter;
import org.openqa.selenium.remote.Response;
import com.xxx.mobile.util.sshserver.ClusterSsh;
import com.xxx.tahoe.testcase.Constant;
import jsystem.framework.report.Reporter;
import jsystem.framework.system.SystemObjectImpl;
public class HandleJSON extends SystemObjectImpl{
private String serverName="";
private String sshKeyLoc="mailcontrol";
private ClusterSsh cssh;
private String accountNumber="86";
private String policyName;
private String policyDesc;
private String profile;
private String profileDesc;
private String profilUrl="https://SERVERNAME:8085/os/v-1/account-ACCOUNTNUMBER/namespace-mobile/object-mobilepolicies/section-POLICYNAME/section-PROFILE/export";
private String allPoliciesUrl="https://SERVERNAME:8085/os/v-1/account-ACCOUNTNUMBER/namespace-mobile/object-mobilepolicies/sections";
private String commandHead="curl -H Accept:application/json --user'cn=mailcontrol (admin),account=1,dc=blackspider,dc=com:7Nz+BXcOXW3ZTXgZptPmRg'-k ";
private String queryKeyAppendix=" |grep data | sed 's/\\\\n//g' |sed's/\"//g'|sed 's/,//g'|sed -n -r -e 's/^\\s+data\\s:\\s(.*)/\\1/p'|base64-d 2>/dev/null | sed 's/,/\\n/g'| sed 's/\"//g' |grep KEY | sed -n -r-e 's/.*KEY:(.*)/\\1/p'";
private String queryIconAppendix=" |grep data | sed 's/\\\\n//g' |sed's/\"//g'|sed 's/,//g'|sed -n -r -e 's/^\\s+data\\s:\\s(.*)/\\1/p'|base64-d 2>/dev/null | sed 's/,/\\n/g'| sed 's/\"//g' |grep KEY | sed -n -r-e 's/.*KEY:(.{60}).*/\\1/p'";
private String queryPoliciesAppendix=" 2>/dev/null | grep -e'\"KEY\"'|sed 's/\"//g'| sed -n -r -e's/^\\s+KEY\\s:\\s(.*)/\\1/p'|sed 's/,//g'|grep \"VALUE\"";
public HandleJSON(ClusterSsh cssh,String serverName){
this.cssh=cssh;
this.serverName=serverName;
}
public HandleJSON(ClusterSsh cssh, String serverName, String accountID, StringpolicyName, String profile){
this.cssh=cssh;
this.serverName=serverName;
this.accountNumber=accountID;
// this.policyName=Constant.randomPolicyName;
// this.profile=Constant.randomProfileName;
}
public void connectServer()throws Exception{
if (cssh.connectToHost(serverName)) {
report.report("Connected to " + serverName);
} else {
report.report("Failed to connect to " + serverName, Reporter.FAIL);
}
cssh.becomeRoot();
}
public void connectServer(String serverName)throws Exception{
if (cssh.connectToHost(serverName)) {
report.report("Connected to " + serverName);
} else {
report.report("Failed to connect to " + serverName, Reporter.FAIL);
}
cssh.becomeRoot();
}
public void disConnectServer()throws Exception{
cssh.stopBeingRoot();
cssh.disconnectFromHost();
}
/**
* Get payload item value according to key.
* Not include profile name and description
* @param key
* @param type
* true - the key value is a boolean type
* false- the key value is not a boolean type
* @return
* if there is not value, return an empty string.
* @throws Exception
*/
public String queryValue(String key, boolean type) throws Exception{
// String commandLine=this.composeQuery(key, type);
// report.report("Command line is: "+commandLine);
// String result=cssh.curl(commandLine);
// report.report("Result from JSON is: "+result);
String result=this.queryValue(key);
return result;
}
/**
* For All type. Get payload item value according to key.
* @param key
* @return
* @throws Exception
*/
public String queryValue(String key)throws Exception{
String commandLine=this.composeQuery(key);
report.report("Command line is: "+commandLine);
String result=cssh.curl(commandLine);
result=this.manageString(result);
report.report("Result from JSON is: "+result);
return result;
}
public String queryIconValue(String key)throws Exception{
String commandLine=this.composeIconQuery(key);
report.report("Command line is: "+commandLine);
String result=cssh.curl(commandLine);
result=this.manageString(result);
report.report("Result from JSON is: "+result);
return result;
}
/**
* Check if the exact value exists in payload.
* Good for check a key with several values.
* E.g. type
* IosDeviceProfile
* RestrictionPayload
* WebClipPayload
* @param key
* @param value
* @return
* true - if it exists
* false - if not exists
* @throws Exception
*/
public boolean isValueExist(String key, String value) throws Exception{
String commandLine=this.composeQuery(key)+ " | grep \""+value+"\"";
report.report("Command line is: "+commandLine);
String result=cssh.curl(commandLine);
result=this.manageString(result);
report.report("Result from JSON is: "+result);
if(result.trim().equals(value)){
return true;
}else{
return false;
}
}
/**
* This is query all exists policy name from Object store.
* Good for checking the default policy left after delete all policies.
* @return
* @throws Exception
*/
public String queryAllPoliciesName() throws Exception{
String commandLine=this.composeQueryAllPolicies();
report.report("Command line is: "+commandLine);
String result=cssh.curl(commandLine);
report.report("Result from JSON is: "+result);
return result;
}
/**
* Check if policy name exist in Object Store
* @return
* Policy name string in server
* @throws Exception
*/
public boolean isPolicyNameExist() throws Exception{
String commandLine=this.composeQueryPolicy("display_name",this.policyName);
report.report("Command line is: "+commandLine);
String result=cssh.curl(commandLine);
report.report("Result from JSON is: "+result);
if(result.trim().equals(this.policyName)){
return true;
}else{
return false;
}
}
/**
* Check if policy description exist in Object Store
* @return
* Policydescription String in server
* @throws Exception
*/
public boolean isPolicyDescExist() throws Exception{
String commandLine=this.composeQueryPolicy("description",this.policyDesc);
report.report("Command line is: "+commandLine);
String result=cssh.curl(commandLine);
report.report("Result from JSON is: "+result);
if(result.trim().equals(this.policyDesc)){
return true;
}else{
return false;
}
}
/**
* Check profile name is correct in Object store.
* @return
* profile name string in JSON object
* @throws Exception
*/
public boolean isProfileNameCorrect() throws Exception{
String commandLine=this.composeQueryProfile("display_name");
report.report("Command line is: "+commandLine);
String result=cssh.curl(commandLine);
report.report("Result from JSON is: "+result);
if(result.trim().equals(this.profile)){
return true;
}else{
return false;
}
}
/**
* Get profile description from object store
* @retrun
* profile description string in JSON object
* @throws Exception
*/
public boolean isProfileDescCorrect() throws Exception{
String commandLine=this.composeQueryProfile("description");
report.report("Command line is: "+commandLine);
String result=cssh.curl(commandLine);
report.report("Result from JSON is: "+result);
if(result.trim().equals(this.profileDesc)){
return true;
}else{
return false;
}
}
private String composeQueryProfile(String key) throws Exception{
String queryProfile=this.composeCurl()+" | sed -n -r -e'/^\\s+\""+key+"\"/s/.*:\\s\"(.*)\"[,]{0,1}.*/\\1/p'";
return queryProfile;
}
private String composeQueryAllPolicies()throws Exception{
String policy=this.allPoliciesUrl.replaceAll("SERVERNAME:8085",this.serverName+":8085");
policy=policy.replaceAll("ACCOUNTNUMBER", this.accountNumber);
String queryString=this.commandHead+policy+" 2>/dev/null | sed -n -r -e'/^\\s+\"name\"/s/.*:\\s\"(.*)\"[,]{0,1}.*/\\1/p'";
report.report(queryString);
return queryString;
}
private String composeQueryPolicy(String key,String value)throws Exception{
String policy=this.allPoliciesUrl.replaceAll("SERVERNAME:8085",this.serverName+":8085");
policy=policy.replaceAll("ACCOUNTNUMBER", this.accountNumber);
String appendix=this.queryPoliciesAppendix.replaceAll("KEY",key);
appendix=appendix.replaceAll("VALUE", value);
String queryString=this.commandHead+policy+appendix;
return queryString;
}
/**
* Compose query string and execute(Obsoleted)
* @param key
* @param type
* - true it's a boolean or number type key or null in Json
* - false it's a string type key in Json
* @return
* @throws Exception
*/
private String composeQuery(String key, boolean type)throws Exception{
String temp="";
if (type){
temp=this.composeCurl()+" | sed -n -r -e'/^\\s+\"data\"/s/.*"+key+"\\\\\":(true|false|null|[-]{0,1}[0-9]+)[,]{0,1}.*/\\1/p'";
}else{
temp=this.composeCurl()+" | sed -n -r -e'/^\\s+\"data\"/s/.*"+key+"\\\\\": \\\\\"([0-9a-zA-Z\\.]+)\\\\\"[,]{0,1}.*/\\1/p'";
}
report.report(temp);
return temp;
}
/**
* This is for query all type key-value in payload data
* @param key
* @return
* @throws Exception
*/
private String composeQuery(String key)throws Exception{
String appendix=this.queryKeyAppendix.replaceAll("KEY", key);
String temp=this.composeCurl()+appendix;
return temp;
}
private String composeIconQuery(String key) throws Exception{
String appendix=this.queryIconAppendix.replaceAll("KEY", key);
String temp=this.composeCurl()+appendix;
return temp;
}
private String composeCurl()throws Exception{
String tempURL=this.handleURL();
String composedCurl=this.commandHead+tempURL+" 2>/dev/null";
return composedCurl;
}
private String handleURL() throws Exception{
String urlASCII=this.profilUrl.replaceAll("SERVERNAME:8085",this.serverName+":8085");
urlASCII=urlASCII.replaceAll("ACCOUNTNUMBER", this.accountNumber);
urlASCII=urlASCII.replaceAll("POLICYNAME", Constant.randomPolicyName);
urlASCII=urlASCII.replaceAll("PROFILE", Constant.randomProfileName);
String result=URIUtil.encodePath(urlASCII,"ISO-8859-1");
return result;
}
public String manageString(String a) {
String temp=a;
int length=temp.length();
while(temp.endsWith("}") || temp.endsWith("]")){
temp=temp.substring(0, length-1);
length=temp.length();
}
return temp;
}
public String getServerName() {
return serverName;
}
public void setServerName(String serverName) {
this.serverName = serverName;
}
public String getSshKeyLoc() {
return sshKeyLoc;
}
public void setSshKeyLoc(String sshKeyLoc) {
this.sshKeyLoc = sshKeyLoc;
}
public String getPolicyName() {
return policyName;
}
public void setPolicyName(String policyName) {
this.policyName = policyName;
}
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
public String getPolicyDesc() {
return policyDesc;
}
public void setPolicyDesc(String policyDesc) {
this.policyDesc = policyDesc;
}
public String getProfileDesc() {
return profileDesc;
}
public void setProfileDesc(String profileDesc) {
this.profileDesc = profileDesc;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
}