category:primary is:unread
Sunday, January 15, 2017
Filter unread emails from Primary tab of Gmail
In Gmail, use following search filter to Filter unread emails from Primary tab
category:primary is:unread
category:primary is:unread
Posted by Vishnu Agrawal at 3:50 PM 0 comments
Sunday, August 9, 2015
Java: Compare two JSON strings
Following is the code to parse two json strings and compare them
import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class TestJson3{ private static String srcJsonStr = "SRC JSON String here"; private static String destJsonStr = "Dest JSON String here"; public static void main(String[] args){ try{ JSONObject srcJson = new JSONObject(srcJsonStr); JSONObject destJson = new JSONObject(destJsonStr); compareJson(srcJson, destJson); } catch(Exception e){ e.printStackTrace(); } } public static void compareJson(JSONObject srcJson, JSONObject destJson){ StringBuilder diffLog = new StringBuilder(); try{ MapsrcJsonMap = new LinkedHashMap (); Map destJsonMap = new LinkedHashMap (); srcJsonMap = parseJson("", srcJson, srcJsonMap); destJsonMap = parseJson("", destJson, destJsonMap); //union Set union = new HashSet (srcJsonMap.keySet()); union.addAll(destJsonMap.keySet()); //h1-h2 Set diff1 = new HashSet (srcJsonMap.keySet()); diff1.removeAll(destJsonMap.keySet()); //h1-h2 Set diff2 = new HashSet (destJsonMap.keySet()); diff2.removeAll(srcJsonMap.keySet()); //intersection Set intersect = new HashSet (srcJsonMap.keySet()); intersect.retainAll(destJsonMap.keySet()); if(intersect.size() > 0){ for(String key: intersect){ Object srcData = srcJsonMap.get(key); Object destData = destJsonMap.get(key); if( ! srcData.toString().equals(destData.toString())){ diffLog.append("KEY :: " + key + "\n"); diffLog.append("SRC DATA :: " + srcData + "\n"); diffLog.append("DEST DATA :: " + destData + "\n"); } } } if(diff1.size() > 0){ diffLog.append("\n"); diffLog.append("###########################"); diffLog.append("Only in SRC JSON"); diffLog.append("###########################"); diffLog.append("\n"); for(String key: diff1){ diffLog.append("KEY :: " + key + "\n"); diffLog.append("DATA :: " + srcJsonMap.get(key) + "\n"); diffLog.append("\n"); } } if(diff2.size() > 0){ diffLog.append("\n"); diffLog.append("###########################"); diffLog.append("Only in DEST JSON"); diffLog.append("###########################"); diffLog.append("\n"); for(String key: diff2){ diffLog.append("KEY :: " + key + "\n"); diffLog.append("DATA :: " + destJsonMap.get(key) + "\n"); diffLog.append("\n"); } } } catch(Exception e){ e.printStackTrace(); } System.out.println(diffLog.toString()); } public static Map parseJson(String key, JSONObject jsonObject, Map map){ try{ Iterator iterator = jsonObject.keys(); while(iterator.hasNext()){ String obj = iterator.next().toString(); String keyVal = (key.equals("")) ? obj : key + "." + obj; if(jsonObject.get(obj) instanceof JSONArray){ JSONArray jsonArr = (JSONArray) jsonObject.get(obj); for(int k=0; k < jsonArr.length; k++){ if(jsonArr.get(k) instanceof JSONObject){ parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k), map); } else{ map.put(keyVal, jsonArr.get(k)); } } } else{ if(jsonObject.get(obj) instanceof JSONObject){ parseJson(keyVal, (JSONObject) jsonObject.get(obj), map); } else{ map.put(keyVal, jsonObject.get(obj)); } } } } catch(Exception e){ e.printStackTrace(); } return map; } }
Posted by Vishnu Agrawal at 3:07 PM 0 comments
Java: Store Json String into a Java Map
Following is the code to parse a json string and store the structure in Map
import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class TestJson2{ private static String jsonStr = "JSON String here"; public static void main(String[] args){ try{ JSONObject obj = new JSONObject(jsonStr); parseJson(obj); } catch(Exception e){ e.printStackTrace(); } } public static void parseJson(JSONObject jsonObject){ Mapmap = new LinkedHashMap (); try{ parseJson("", jsonObject, map); for(String key: map.keySet()){ System.out.println(key + "::" + map.get(key)); } } catch(Exception e){ e.printStackTrace(); } } public static Map parseJson(String key, JSONObject jsonObject, Map map){ try{ Iterator iterator = jsonObject.keys(); while(iterator.hasNext()){ String obj = iterator.next().toString(); String keyVal = (key.equals("")) ? obj : key + "." + obj; if(jsonObject.get(obj) instanceof JSONArray){ JSONArray jsonArr = (JSONArray) jsonObject.get(obj); for(int k=0; k < jsonArr.length; k++){ if(jsonArr.get(k) instanceof JSONObject){ parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k), map); } else{ map.put(keyVal, jsonArr.get(k)); } } } else{ if(jsonObject.get(obj) instanceof JSONObject){ parseJson(keyVal, (JSONObject) jsonObject.get(obj), map); } else{ map.put(keyVal, jsonObject.get(obj)); } } } } catch(Exception e){ e.printStackTrace(); } return map; } }
Posted by Vishnu Agrawal at 2:45 PM 0 comments
Java: parse a JSON string
Following is the code to parse a json string
import java.util.Iterator; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class TestJson1{ private static String jsonStr = ""; public static void main(String[] args){ try{ JSONObject obj = new JSONObject(jsonStr); parseJson("", obj); } catch(Exception e){ e.printStackTrace(); } } public static void parseJson(String key, JSONObject jsonObject){ try{ Iterator iterator = jsonObject.keys(); while(iterator.hasNext()){ String obj = iterator.next().toString(); String keyVal = (key.equals("")) ? obj : key + "." + obj; if(jsonObject.get(obj) instanceof JSONArray){ JSONArray jsonArr = (JSONArray) jsonObject.get(obj); for(int k=0; k < jsonArr.length; k++){ if(jsonArr.get(k) instanceof JSONObject){ parseJson(keyVal + "[" + k + "]", (JSONObject) jsonArr.get(k)); } else{ System.out.println(keyVal + " :: " + jsonArr.get(k)); } } } else{ if(jsonObject.get(obj) instanceof JSONObject){ parseJson(keyVal, (JSONObject) jsonObject.get(obj)); } else{ System.out.println(keyVal + " :: " + jsonObject.get(obj)); } } } } catch(Exception e){ e.printStackTrace(); } } }
Posted by Vishnu Agrawal at 2:24 PM 0 comments
Tuesday, September 17, 2013
Wednesday, November 28, 2012
Shell script: format numbers
Shell script to format date in yyyy.mm.dd format
#!/bin/bash year=2012 for (( c=1; c<=12; c++ )) do month=$(printf "%02d" $c) for (( d=1; d<=15; d++ )) do day=$(printf "%02d" $d) echo $year.$month.$day done done
Posted by Vishnu Agrawal at 10:57 PM 0 comments
Labels: shell scripting
Java : Send Email
Following is the code to send email using java. Download mail.jar and activation.jar to run the program.
import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class EmailProcessor { private String mailHost; private String from; private String to; private String subject; private String text; public static void main(String[] args) { EmailProcessor emp = new EmailProcessor("mailhost", "from@test.com", "to1@test.com,to2@test.com", "Subject", "Email Text"); emp.send(); } public EmailProcessor(String mailhost, String from, String to, String subject, String text){ this.from = from; this.to = to; this.subject = subject; this.text = text; this.mailHost = mailhost; } public void send(){ Properties props = new Properties(); props.put("mail.smtp.host", mailHost); Session mailSession = Session.getDefaultInstance(props); Message simpleMessage = new MimeMessage(mailSession); InternetAddress fromAddress = null; InternetAddress[] toAddress = null; try { fromAddress = new InternetAddress(from); String[] toEmails = to.split(","); toAddress = new InternetAddress[toEmails.length]; for (int i = 0; i < toEmails.length; i++) { toAddress[i] = new InternetAddress(toEmails[i].trim()); } } catch (AddressException e) { Utility.logError(e.getMessage()); e.printStackTrace(); } try { simpleMessage.setFrom(fromAddress); simpleMessage.setRecipients(RecipientType.TO, toAddress); simpleMessage.setSubject(subject); simpleMessage.setText(text); Transport.send(simpleMessage); } catch (MessagingException e) { Utility.logError(e.getMessage()); e.printStackTrace(); } } }
Posted by Vishnu Agrawal at 10:47 PM 0 comments
Labels: java
Friday, June 8, 2012
Selenium: Testing auto complete fields
Selenium's "type" command does not trigger auto complete feature to give suggestion. We can use "typeKeys" method to simulate auto complete. Here is the sample code for this:
Here we are first entering value in the text field, then using selenium's 'typeKeys' method to press 'space' character and then 'backspace' (\b) character.
public static void inputTextInAutocompleteField(String testField, String testValue) { try { selenium.type(testField, testValue); selenium.typeKeys(testField," \b") ; } catch (Exception e) { e.printStackTrace(); } }
Posted by Vishnu Agrawal at 6:29 PM 0 comments
Labels: selenium
Selenium: How to accept SSL certificates
When you run your selenium tests on any HTTPS url, things go wrong. We get certificate exception and test doesn't proceed. In firefox, the workaround for this problem is to create your own Firefox profile, with the specific certificate added on it manually and then start Selenium server with that firefox profile, but for Internet Explorer there is no such workaround.
However, we can resolve this issue by installing CyberVillains Certificate on Windows. We need to follow below steps to work Selenium with SSL.
1. Start selenium server with "-trustAllSSLCertificates" command
2. Use "*firefoxproxy" instead of "firefox" OR "*iexploreproxy" instead of "iexplore"
3. Install CyberVillains Certificate (selenium bundles a special certification authority (CA) certificate that is used to trust all the other SSL certificates.)
Installing CyberVillains Certificate
---------------------------------------
1. Extract selenium jar into a folder
2. Goto sslSupport diretory
3. Double click on cybervillainsCA.cer
However, we can resolve this issue by installing CyberVillains Certificate on Windows. We need to follow below steps to work Selenium with SSL.
1. Start selenium server with "-trustAllSSLCertificates" command
2. Use "*firefoxproxy" instead of "firefox" OR "*iexploreproxy" instead of "iexplore"
3. Install CyberVillains Certificate (selenium bundles a special certification authority (CA) certificate that is used to trust all the other SSL certificates.)
Installing CyberVillains Certificate
---------------------------------------
1. Extract selenium jar into a folder
2. Goto sslSupport diretory
3. Double click on cybervillainsCA.cer
Posted by Vishnu Agrawal at 5:12 PM 0 comments
Labels: selenium
Tuesday, June 5, 2012
Bash: read csv file
Here is the script to read csv file in shell script.
#!/bin/bash FILENAME=test.csv OLDSEP=$IFS IFS=, [ ! -f $FILENAME ] && { echo "File not found : $FILENAME"; exit 99; } while read field1 field2 field3 field4 do echo "$field1 : $field2 : $field3 : $field4" done < $FILENAME IFS=$OLDSEP
Posted by Vishnu Agrawal at 1:02 PM 3 comments
Labels: linux, shell scripting, solaris
Subscribe to:
Posts (Atom)