Wednesday, 16 December 2015

Design Pattern

1. builder pattern
  • instead of using many constructors, it use builder to handle multiple parameters
public class Pizza {
    private int size; 
    private boolean cheese; 
    private boolean pepperoni; 
    private boolean bacon; 

    public static class Builder { 
        private final int size; 
        private boolean cheese = false; 
        private boolean pepperoni = false; 
        private boolean bacon = false; 

        public Builder(int size) { this.size = size; } 
        public Builder cheese(boolean value) { cheese = value; return this; } 
        public Builder pepperoni(boolean value) { pepperoni = value; return this; } 
        public Builder bacon(boolean value) { bacon = value; return this; } 

        public Pizza build() { return new Pizza(this);}  //build pizza from builder
    }

    private Pizza(Builder builder) { 
        size = builder.size; 
        cheese = builder.cheese; 
        pepperoni = builder.pepperoni; 
        bacon = builder.bacon; 
    } 
}

Pizza pizza = new Pizza.Builder(12).cheese(true).pepperoni(true).bacon(true).build();




2. adapter pattern
  • translate one interface to another
public interface Duck { 
    public void quack();
    public void fly();
}

public interface Turkey { 
    public void gobble();
    public void fly();
}

public class TurkeyAdapter implements Duck {
    Turkey turkey;

    public TurkeyAdapter(Turkey turkey) {
        this.turkey = turkey;
    }

    public void quack() {
        turkey.gobble();
    }

    public void fly() {
        turkey.fly();
    }
}

public static void main(String[] args) {
    WildTurkey turkey = new WildTurkey();
    Duck turkeyAdapter = new TurkeyAdapter(turkey);

    turkeyAdapter.quack();
    turkeyAdapter.fly();
}


3. bridge pattern
  • decouple abstraction from its implementation
  • prefer composition over inheritance
  • very similar to strategy pattern
/** abstraction */ 
abstract class Shape { 
    protected DrawingAPI drawingAPI; 
    protected Shape(DrawingAPI drawingAPI){ this.drawingAPI = drawingAPI; } 
}

class CircleShape extends Shape {  
    public CircleShape(DrawingAPI drawingAPI) { super(drawingAPI); } 
}

/** implementor */
interface DrawingAPI { public void drawCircle(double x, double y, double radius); }

class DrawingAPI1 implements DrawingAPI { 
    public void drawCircle(double x, double y, double radius) {} 

class DrawingAPI2 implements DrawingAPI { 
    public void drawCircle(double x, double y, double radius) {} 

public static void main(String[] args) {  
    Shape s1 = new CircleShape(1, 2, 3, new DrawingAPI1()); 
    Shape s2 = new CircleShape(5, 7, 9, new DrawingAPI2()); 
}


4. command pattern
  • 'command' object include 'receiver' and an 'execute' method on 'receiver'
  • 'client' create 'command' object and store them in 'invoker' (a list or queue)
public interface Command { void execute(); }  //command interface

public class Light {   //receiver
    public void on() { }
    public void off() { }

public class onCommand implements Command {   //on command
    private Light light; 
    public onCommand (Light light) { this.light = light; } 
    @Override public void execute() { light.on(); } 
}

public class Switch {      //invoker
    private List<Command> l = new ArrayList<Command>();
    public void storeAndExecute(Command cmd) { 
        this.l.add(cmd); 
        cmd.execute(); }  // optional
}

public class offCommand implements Command {   //off command
    private Light light;
    public offCommand(Light light) { this.light = light; }
    @Override public void execute() { light.off(); }

public class TestSwitch { 
    public static void main(String[] args){ 
        Light lamp = new Light(); 
        Command on = new onCommand(lamp); 
        Command off = new offCommand(lamp); 

        Switch mySwitch = new Switch(); 
        switch(args[0]) { 
            case "ON": 
                mySwitch.storeAndExecute(on); 
                break; 
            case "OFF": 
                mySwitch.storeAndExecute(off); 
                break; 
            default: break; 
        } 
    } 
}


5. mvc pattern
  • controller 'observe' and interact with view
  • controller 'observe' and interact with model
  • for security reason, view not necessarily 'observe' and interact with model (e.g. secure database)
6. singleton pattern
  • using enum


reference:
1. when would you use the builder pattern?
2. bridge pattern
3. java singleton using enum

Wednesday, 9 December 2015

git

1. vocabulary
  • working directory/staging area/repository
  • origin: when you clone another repository, git automatically creates a remote named "origin" and points to it
  • master: the main branch

2. git config
  • /etc/gitconfig, ~/.gitconfig, .git/config, one override another
  • git config --list
  • .gitignore specify files not committed to repo, for eclipse project, can be target/, .classpath, .project, .settings/

3. common git command
  • git init
  • git clone
  • git config 
  • git checkout -b new_branch             //create new branch and checkout 
  • git checkout -track -b origin/daves_branch           //create local branch to track remote
  • git commit -m 'initial commit'
  • git add --all
  • git commit --amend                          //combine commit and change comment
  • git reset HEAD
  • git reset --soft HEAD^
  • git reset --soft HEAD~1
  • git reset -- hard HEAD^ 
  • git clean -f -d                                    //remove permanently untracked directory and file
  • git reset                                             //unstage all 
  • git revert <commit>                         //undo a commit in the history
  • git rebase -i HEAD~2                      //combine 2 commits into 1
  • git checkout <feature_branch>
  • git merge <master_branch>            //3-way merge, create new commit C
          A---B feature
         /
   D---E---F---G master
 
          A---B---C feature
         /       /
    D---E---F---G  master 
 
          A---B feature          //fast forward
         /
   D---E- master 
 
    D---E---A---B  feature(master) 
 

  • git checkout <feature_branch>       // rebase move the STARTING POINT of  'feature' on
  • git rebase <master_branch>           // master, destroy A, B, C and create new commits A' B' C'          
          A---B---C feature
         /
   D---E---F---G master
 
                  A'--B'--C' feature
                 /
   D---E---F---G master
  • git remote add <name> <url> 
  • git remote -v
  • git fetch <remote> [branch]
  • git branch -r                                     //display remote branch
  • git branch
  • git pull = git fetch + git merge
  • git pull --rebase (by default it will perform merge, can force it to rebase) 
  • git push origin master
  • git push origin master -f           //overwrite remote branch
  • git log
  • git status
  • git diff
  • git rm
  • git mv
  • git reset --hard origin/master    //clean everything in repo

4. merge vs rebase
  • merging is always safe
  • rebasing is not (Golden Rule of Rebasing - never rebase public branches like 'master' to 'feature)

5. centralized work flow
  • single central repositary
  • work on local master branch
  • git clone
  • git pull --rebase origin master
  • git rebase -- continue
  • git push origin master

6. feature branch flow
  • work on local feature branch
  • git checkout -b feature_branch master
  • git push -u origin feature_branch
  • submit pull request in github or bitbucket (code review) 
  • git checkout master
  • git pull
  • git pull origin feature_branch
  • git push

7. gitflow workflow
  • master(release) branch and dev branch

8. stash(bitbucket) and github


9. local vs remote branch
  • always: work on local branch, rebase on remote branch, push to remote branch
  • local branch can track remote branch
  • after clone, local 'master' will track 'origin/master'
  • 'git fetch, git checkout remote-branch-name' will create local branch that track remote branch 'original/remote-branch-name', just like 'master'
  • or use 'git fetch, git checkout -b remote-branch-name origin/remote-branch-name'

10. clone with/without account
  • git clone git://github.com/SomeUser/SomeRepo.git    //without account
  • git clone git@github.com:UserName/OtherRepo.git

11. submodule
  • .gitmodules                                //define structure
  • git submodule init
  • git submodule update

12. add reviewer to github

Thursday, 3 December 2015

appium

1. use xcode to launch ios simulator
  • 'xcode' -> 'open developer tool' -> 'simulator', will run default simulator
  • now menu show simulator, 'file' to select other simulator if available

2. install ios app to simulator
  • find unsigned app in release, etc. iOS-Simulator-MyRNDemoApp.1.3.0-162.zip @https://github.com/saucelabs/my-demo-app-rn/releases
  • launch ios simulator
  • drag app to simulator,
  • or use cmd: xcrun simctl install BB6F3DD7-2BB6-428F-9673-562A23BE7480 ~/desktop/test.app

3. find device name, udid, platform version, bundle id for iso app
  • xcrun simctl list      //list available simulator with device name & udid, 'booted' means running
  • can find apple native app bundle id @https://support.apple.com/en-gb/guide/deployment/depece748c41/web
  • for custom app, find bundle id in its project, or ask developer

4. use appium inspector to inspect ios app
  • pre-requisite: run device/simulator, run appium server (cmd/ui)
  • specify 'desired capabilities'
  • can use 'bundleId' or 'app' in 'desired capability' to specify the app
  • when using 'bundleId', can find apple native app bundle id @https://support.apple.com/en-gb/guide/deployment/depece748c41/web
  • when using 'app', set 'appium:noReset' so that it will not reinstall app from filepath everytime
  • sample capability
{
  "appium:platformVersion": "13.3",
  "appium:deviceName": "iPhone 11 Pro Max",
  "appium:automationName": "XCUITest",
  "appium:bundleId": "com.apple.Preferences",
  "appium:platformName": "iOS"
}

5. use android studio to launch android simulator
  • 'more actions' -> 'sdk manager' to install sdk
  • 'more actions' -> 'virtual device manager' -> to create device
  • run device

6. install android app to android simulator
  • find .apk file in release
  • launch android simulator
  • drag apk to simulator

7. use appium inspector to inspect android app
  • similar to inspecting ios app
  • set 'ANDROID_HOME' in appium server configuration
  • sample capability
{
  "appium:appPackage": "com.saucelabs.mydemoapp.rn",
  "appium:deviceName": "emulator-5554",
  "appium:automationName": "UiAutomator2",
  "appium:appActivity": ".MainActivity",
  "appium:platformName": "android"
}

8. locator strategy
  • accessibilityId: content-desc (android), accessibility-id (ios)
  • className: class (android and ios)
  • id: id (android), name (ios)
  • name: name (android and ios)
  • xpath

5. get apk from phone
  • 'adb shell pm list packages' - list all packages
  • 'adb shell pm path com.example.someapp' - get full path in phone
  • 'adb pull /data/app/com.example.someapp-2.apk'

6. get AndroidManifest.xml
  •  apktool.bat d -f myapp.apk

7. cross platform - android/ios, mobile web/app
  • XCUITest - ios testing fw by apple for iOS 9.3 and above
  • UIAutomation - ios testing fw by apple for iOS 9.3 and lower
  • Espresso - android testing fw by google
  • UiAutomator/UiAutomator2 - android testing fw by google
  • appium use webdriver api, but it can have any of the above driver underneath, just like selenium, also use webdriver api, but can have chrome, firefox, safari driver

Tuesday, 24 November 2015

Agile and Scrum

1. scrum roles
  • product owner
  • development team: cross functional
  • scrum master

2. scrum events
  • sprint planning
  • daily scrum
  • sprint review and retrospective

3. scrum artifact
  • product backlog
  • sprint backlog
  • burn down chart

4. agile principles
  • customer satisfaction
  • changing requirements
  • deliver working software in sprint
  • face-to-face communication

5. test pyramid

reference
1. test pyramid

Thursday, 19 November 2015

core java

1. static initialize block

class person {
    private static final date boom_start;

    static {
        Calendar gmtcal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        gmtcal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
        boom_start = gmtcal.getTime();
    }
}
  • the static initializer for a class gets run when the class is first accessed, either to create an instance, or to access a static method or field.

2. object instance initialize sequence
  • super()
  • static initilizer
  • instance field initialzer
  • constructor

3. wildcard
 
List<? extends Number>
matches a list of type Number or any of its subclasses
<? super A>
matches a list of type A or any of its superclasses


4. java collection interface
  • set -  no duplicated element, at most one null
  • list
  • queue - usually FIFO, exception priority queue
  • deque - 'deck', double ended queue
      map
  • treemap - sort in natural order
  • linkedhashmap - sort in insertion order

5. java collection implementation (sorted vs unsorted)
  • set - hashset, treeset, linkedhashset
  • list - arraylist, linkedlist
  • map - hashmap, treemap, linkedhashmap
  • queue - linkedlist, priorityqueue
  • deque - linkedlist, arraydeque

6. java thread-safe collection (java.util.concurrent)
  • collection in java.util is not safe, except hashtable and vector
  • always safe to use concurrent collection than 'synchronized wrapper'
  • java.util.concurrent example - concurrenthashmap


7. java hash map
  • record are stored in bucket
  • hash function calculate hash code based on record
  • hash code is indexing to the bucket
  • if an object is used as "key" in hash map, you need to implement hashcode() and equals() for the object
  • hashcode() is used to calculate store location, equals() is used when there is a collision

8. stringbuffer vs stringbuilder
  • stringbuffer is synchronized, stringbuilder is not

9. vector vs arraylist
  • vector is synchronized, arraylist is not

10.  hashtable vs hashmap
  • hashtable is synchronized, hashmap is not

11. primitive vs object
  • primitives: byte, short, int, long, float, double, boolean, char
  • object: the rest

12. equals() vs ==
  • equals() is comparison of value
  • == is comparison of reference
  • if no parent class override equals(), then finally it goes to 'Object' equals(), which is ==
  • if you override equals(), you need to override hashcode(),  so that if two objects equal(), they have the same hashcode(). it is required by java
  • reversal of the above is not necessarily true
  • in eclipse, Source --> Generate equals() and hashCode()

13. generics
  • user 'type' as parameters when defining class, interface, methods 
  • do you need to add to the List? Do you care what is in the list? 
      yes yes - use List<Foo>
      yes no - use List<? super Foo>
      no yes - use <? extends Foo> --- most common
      no no - use <?>

14. how to overide equals() for assertEquals(obj1, obj2)

@Override
    public boolean equals(Object other){
        boolean equal = false;
        if (other instanceof Money){
            Money otherMoney = (Money)other;
            equal = (this.dollars() == otherMoney.dollars()
                       && this.cents() == otherMoney.cents());
        }
        return equal;
    }


15. comparable vs comparator
      public interface Comparable<T> {
          public int compareTo(T o);
      }
      public interface Comparator<T> {
          int compare(T o1, T o2);
      }
  • comparable is used to define natural order of object, and is implemented by the class itself
  • comparator is used to define external ordering of object, and is implemented by an external comparator class
  • multiple comparator class are implemented when multiple ways are needed to compare two instance

16. jdbc related

      transaction
  • a set of one or more SQL statements
  • transaction ends with either a commit or a rollback
      stored procedures
  • a group of SQL statements that can be called by name

17. final, finally, finalize
      private final String name = "foo";
      public final String toString() {  return "NULL"; }

      try {
        //do stuff
      } catch (SomeException se) {
        //handle se
      } finally {
        lock.unlock(); //always executed, even if Exception or Error or se
      }

      protected void finalize()
       //is called when an object is garbage collected
       //rarely need to override
      { //free resources (e.g. unallocate memory) super.finalize(); }


18. java IO
  • byte based is called 'stream' (InputStream, OutputStream)
  • character based is called 'reader/writer' ()
  • stringreader turns a string into a reader, so that it can be passed to component that accept reader

19. xml
  • xml schema describes the structure of an xml document

20. jaxb
  • marshalling: convert java object to xml file
  • annotate object with JAXB annotation, later use jaxbMarshaller.marshal() or jaxbMarshaller.unmarshal() to do the object/xml conversion

21. system.getenv() vs. system.getproperty()
  • system.getenv() is for operating system environment variables
  • system.getproperty() is for jvm arguments which are passed as '-DpropName=value' to java application launcher

22. handle exceptions
  • checked exception
  • run-time exception

23. concurrency and coordination
  • 'synchronized' keyword lock on current object
  • 'wait()', 'notify()', 'notifyall()' must be called in 'synchronized' context 
  • 'wait()' suspend current thread until 'notify(), notifyall()'
  • 'notify(), notifyall()' wake up other thread(s)
  • 'join' wait for other thread to die

24. thread
  • implement runnable interface vs extend thread class

25. prefer interface to abstract class
  • interface is more flexible, avoid class hierarchy constraint (abstract class has to use inheritance)
  • interface is a blueprint that need full implementation, abstract class need partial design
  • interface can have 'default' method that has implementation

26. object serialization
  • what gets serialized is the "value" of the object, or the contents
  • methods are not serialized

27. java 8 stream
  • any type that implement 'Iterable' interface can be converted to stream using .stream()
  • intermediate operation returns a new modified stream
  • we can only use one terminal operation per stream
  • execute a terminal operation makes stream inaccessible
  • intermediate operations are lazy
  • in both reduce and collect, there're 3 params, identity (initial value), accumulator (how elements are added up), combiner (how result of multiple streams are added)
List<Customer> customersWithMoreThan100Points = customers         //filter objects
  .stream()
  .filter(c -> c.getPoints() > 100)
  .collect(Collectors.toList());

List<StaffPublic> result = staff.stream().map(temp -> {                       //convert objects
    StaffPublic obj = new StaffPublic();
    obj.setName(temp.getName());
    obj.setAge(temp.getAge());
    if ("mkyong".equals(temp.getName())) {
        obj.setExtra("this field is for mkyong only!");
    }
    return obj;
}).collect(Collectors.toList());


References:
1. top 25 java interview questions
2. java == vs equals() confusion
3. java collection class
4. java generics 
5. understanding collections and thread safety in java
6. java 8 map, filter and collect

Monday, 16 November 2015

Javascript, JQuery and Ajax

1. ajax
  • update part of the web page without loading the whole page
  • can use jquery or (xmlhttp + javascript) to implement, jquery is simpler

2. dom
  • document object model, the object model of html document
  • root is 'document' object, i.e. $ document

3. jquery
  • a javascript library
  • can host it locally or include it from a cdn
  • in the format of $(selector).action() or $(selector).event(handler())
      $("p").hide();
      
      $(document).ready(function(){               
          $("button").click(function(){
              $("p").hide();
          });
      });


4. javascript
      <script src="myScript.js"></script>

      //this is a comment
      function myFunction(p1, p2) {
   
      return p1 * p2;
     


5. angularjs
  • mvc pattern (ng-model, view, ng-controller)
  • model = data, view = view, controller = control interaction between model and view
  • $scope: application context

6. debug javascript (f12 open developer tool)
  • inspect -> elements -> event listeners -> click -> button -> handler -> right click 'show function definition'
  • or console -> type 'window' -> select the function listed by name -> show function definition
  • or source -> event listener breakpoints -> mouse -> click
  • or source -> find .js -> set breakpoint -> reload page (f5) or re-trigger .js from ui
  • or ctrl + shift + f, in search tab, select regular expression option, then search function name
  • f8 (continue), f10(step), f11(step into), shift+f11(step out), right click(continue to here)
  • source->call stack->restart frame when stopped at a breakpoint
  • press esc in another tab open the 'console' at the bottom
  • ctrl+shift+f to search all source code
  • use 'window.location.reload()' to clean up console variables

7. edit and debug javascript
  • add a break-point at an earlier point in the script
  • reload page
  • edit your changes into the code
  • ctrl + s (save changes)
  • right click, continue to here

8. inspect cookie and internal storage
  • inspect->application

reference
1. debug in chrome
2. inspect and debug javascript
3. restart frame
4. find javascript function definition in chrome 
5. how does the “this” keyword work?

Sunday, 8 November 2015

restful web service

1. what is restful service?
  • server provide resource for client to access and modify
  • resource is represented in text, xml, json (most popular)
  • http protocol is used
  • http get(read),  put(create), delete(delete), post(update/create), options(supported operations)
  • stateless, cachable
  • description languages: wadl, wsdl

2. rest testing with soapui
  • create project
  • add wadl or wsdl api, each api will display a sample request
  • to test a sample request, add it to 'test case' (test suite)
  • add assertions to test case result
  • run test case/suite and generate report
  • property transfer: transfer property from previous test step to next test step

3. rest vs soap
  • rest is architecture, soap is protocol
  • soap is older, heavier, stateful, built-in security, transactional, reliable, no caching, xml payload
  • use soap ui to test soap

4. url encoding
  • url can only be sent over the internet using the ascii character-set
  • when url contain non ascii text, it is converted with a "%" followed by two hexadecimal digits
  • url cannot contain spaces, space is converted with a plus (+) sign or with %20

5. postman
  • install "interceptor extension" to send http request with restricted header and cookie captured from browser
  • interceptor use postman as browser proxy, and can capture header and cookie from chrome to be used by postman later, therefore avoid CORS policies issue)
  • if login using chrome with 'interceptor' turned on, postman will not need authentication later
  • use 'environment' to cater for local, ci, dev, sit...
  • use array in postman (see reference)
  • postman native app handles cookie directly without interceptor, so remember to turn on interceptor in browser when needed
  • ssl certification verification can be turned off when certificate is involved, it can be done in postman settings, or when running newman in command line '--insecure'

  • extract token/value from response/header
  • var body =  JSON.parse(responseBody);
    postman.setEnvironmentVariable("access_token_from_auth0", body.access_token);

6. https
  • purpose: 1) verify that server is authentic 2) encrypt conversation
  • client obtain certificate from server that proves server identify
  • server can obtain certificate from client, but it RARELY happens
  • so it's a one-way certification, that's why restassured ''relaxedHTTPSValidation()" works
  • port 443
  • ssl certificate that contains public key is sent from server to client browser
  • client use public key to encrypt
  • server use private key to decrypt
  • connection is secure

7. http status
  • 200
  • 400 - bad request
  • 401 - unauthenticated
  • 403 - unauthorized
  • 500 - internal server error

8. http parameter
  • http 'GET' parameter is sent as query in url, e.g. http://example.com/page?parameter=value&also=another
  • http 'POST' parameter is sent in body as 'application/x-www-form-urlencoded' () or 'multipart/form-data'
  • if you have binary (non-alphanumeric) data (or a significantly sized payload) to transmit, use 'multipart/form-data'. otherwise, use 'application/x-www-form-urlencoded'

9. http header
  • name/value pairs, e.g. user-agent, accept-encoding, cookie
  • 'Content-Type':multipart/formdata means a list of document broken into pieces, each with different mime type

10. session (server side)
  • server create temporary file (differ in session id) to store session variables and value
  • session time out after say 30 min

11. cookie (browser side)
  • cookie contain server session id
  • both session and cookie are based on connection, therefore if connecting from two browsers, will have two pairs of cookie/session
  • client sends cookie to server, and server use session to "remember" the state of the application for that specific client and generate appropriate response
  • session cookie and persistent cookie are different, session cookie is deleted when browser close or leave site, persistent cookie is deleted when expired
  • cookie is domain specific, domain is set to the host name of the page that set the cookie

12. cookie vs jwt
  • cookie is stateful, session is created on server side, session id is sent to client as a cookie, session can expire
  • token is stateless, server does not have session data, server embed user data in token and send to client
  • cookie is sent automatically by browser with every request to server
  • token is not sent automatically by browser, client application must explicitly attach it to header
  • token is self contained, contain both validity (signature) and user information (header and payload)
  • header and payload are only base64-encoded, signature is created by signing header and payload with a private key
  • token can be decoded at jwt.io. signature validity can be performed.

13. sso (single sign on)
  • access more than one application in a single user session without having to re-authenticate

14. serialize/deserialize json/xml java
  • jackson 'ObjectMapper.writeValueAsString()'
  • jackson ''ObjectMapper.readValue(jsonInString, User.class)

15. soap
  • envelope, header, body, fault element 

16. ssl/tls certificate
  • certificate usually is stored on server side, only occasionally needed on client side when client also need to be authenticated
  • client and server will do 'ssl hand shake', where client (and if server) check validity of certificate, and negotiate encryption details

17. mock
  • mocky
  • wiremock


references:
1. restful tutorial
2. learn rest, a tutorial.
3. getting started with rest testing
4. functional testing
5. transferring property values
6. url encoding
7. can a json start with [?
8. jaxb hello world example
9. best way to compare 2 xml documents in java
10. compare two json objects in java
11. cucumber-jvm bdd for restful api
12. what is a good approach to verify xml response from restful service in java?
13. jaxb and marshal/unmarshal schema validation
14. sample restful test site
15. form-data or x-www-form-urlencoded?
16. how are parameters sent in an http post request?
17. how session works?
18. session (wiki)
19. cookie vs token
20. environment and array in postman
21. rest vs soap testing
22. rest vs soap
23. understand jwt
24. soap vs rest by smartbear
25. how to convert java object to from json jackson
26. xml soap
27. http put vs post
28. how https works
29. cookie in mobile, do the exist?
30. user session management and sso
31. sending cookies with postman
32. anatomy of jwt
33. extract data from postman and chaining requests
34. how do you make money using postman?
35. how do fix newman error self signed certificate?
36. newman

Thursday, 17 September 2015

sql

1. select
      select <column_names>
      from <<table_name>>
      where <condition>
      [and|or] <condition>
      group by <column_name>
      order by <column_name> [asc, desc]


2. select distinct
      select distinct <column_name>
      from <table_name>


3. where
      where <column_name> in ('value1', 'value2', ...)
      where <column_name> between 'value1' and'value2'
      where <column_name> like {pattern}


4. sum()    
      select sum(<column_name>)       //return a value
      from <table_name>
      where <condition>;

      select <column_name1>,<column_name2>, sum(aggregate_expression)
      from <table_name>
      where <condition>
      group by <column_name1>, <column_name2>;
  • If extra column in involved in select, then a group by is needed

5. group by
      select <column_name1>, <column_name2>, func(column_name3)
      from <table_name>
      group by <column_name1>, <column_name2>
  • group by is used when aggregate function is used in the select statement
  • group by really means "for each unique" or "for each unique combination of ..."
      select service, sum(fraud_vol), sum(fraud_val) from

      (select biz_svc_cd as service, volume as fraud_vol, value as fraud_val
      from x2p_fraud_daily
      where fraud_tran_date >= to_date('01/02/2016', 'dd/mm/yyyy')
      and fraud_tran_date <= to_date('29/02/2016', 'dd/mm/yyyy')
      and instr_tp = 'pmnt')

      group by service
      order by service asc


6. having
      select <column_name1>, sum(<column_name2>)
      from <table_name>
      group by <column_name1>
      having sum(column_name2) > 1500
  • having is used when aggregate function is used in the select statement

7. update
      update <table_name>
      set <column_name1>=value1, <column_name2>=value2,...
      where <column_name3>=value3;


8. delete
      delete from <table_name>
      where <column_name1>=value1;


9. insert into
      insert into <table_name>
      values (value1,value2,value3,...);

      insert into <table_name> (column_name1,column_name2,column_name3,...)
      values (value1,value2,value3,...);


10. count
      select count(*)
      from <table_name>;

      select count(distinct <column_name>)
      from <table_name>;

      select count(<column_name>)
      from <table_name>
      where <column_name1>=value1;


11. join
  • join is based on foreign key concept. 
  • based on a column match between table a and b, it combines columns of a and b on a condition
  • inner join only return rows that meet the condition
  • left join return all rows from table a
  • right join return all rows from table b
  • full join return all rows from table a and b
  • self join is to find relationship between rows in the same table (self join create cartesian product of table with itself)
      select yr, mon, fraud_reason, sum(val) from
           (select extract(year from fraud_tran_date) as yr, extract(month from fraud_tran_date) as mon, value as val, description as fraud_reason
           from x2p_fraud_daily
           left join lo_x2p_fraud_rsn
           on x2p_fraud_daily.fraud_rsn_id=lo_x2p_fraud_rsn.id
           where fraud_tran_date>= to_date('01/02/2016', 'dd/mm/yyyy')
           and fraud_tran_date<= to_date('29/02/2016', 'dd/mm/yyyy'))
      group by yr, mon, fraud_reason
      order by yr desc, mon asc, fraud_reason;


12. common command

      create table new_table as select * from old_table;
      insert into table_1 select * from table_2;
      select extract(year from fraud_tran_date) as yr, extract(month from fraud_tran_date) as mon
      where fraud_tran_date >= to_date('01/02/2016', 'dd/mm/yyyy')
      and sender in ('ctbaau2sxxx', 'ctbaau2sbca', 'ctbaau2sxb1', 'ctbaauc1bca', 'ctbaauc1boc')

13. bulk delete with 50000 in one batch to avoid performance issue

      begin
      loop
      delete /*+ parallel (table1, 4) */  from table1
           where event_date < to_date('[date]','mm/dd/yyyy') and rownum < 50000;
           exit when sql%rowcount < 49999;
           commit;
      end loop;
      commit;
      end;


14. select into             //backup a table by creating a new one

      select *
      into backup_table
      from table;


15. merge into

      merge into x2p_fraud_entry using
          (select
                  x2p_fraud_entry_staging.id,
                  x2p_fraud_entry_staging.version,
                  x2p_fraud_entry_staging.transaction_key,
            from x2p_fraud_entry_staging
            left outer join x2p_settlement_transaction
                on x2p_settlement_transaction.tx_id_key = x2p_fraud_entry_staging.transaction_key
          ) txsrc
          on (x2p_fraud_entry.id = txsrc.id)
          when matched then update set
                --x2p_fraud_entry.id = txsrc.id, -- no need to update the id
                x2p_fraud_entry.version = txsrc.version,
          when not matched then insert
          (
           id,
           version,
          )
          values
          (
           txsrc.id,
           txsrc.version,
          );


16. coalesce
  • returns the first non-NULL expression among its arguments
  • so if expression 1 is null value, expressions 2 can become its default value
      coalesce("expression 1", "expressions 2", ...)


17. to_timestamp
  • convert char string to timestamp
  • select to_timestamp('10-Sep-02 14:10:10.123000', 'DD-Mon-RR HH24:MI:SS.FF')
       from dual;
18. use with .. as to calculate percentage

      with mytable as
          (select .... from ...)
      select column_1/(select sum(column_1) from mytable)
      from mytable

19. indexing
  • content -> address

20. join query result with table

      select c1, c2 from t1
      left join (select c3, c4 from t2) t3
      on t1.c5 = t3.c6

21. query lookup with multiple column
      select
        d1.val as column1,
        d2.val as column2
      from table1 t
      join data d1 on ( d1.dataId = t.col1 )
      join data d2 on ( d2.dataId = t.col2 )


22. comment
      --


23. count(*) vs count(p.product_id)
  • count(*) counts the number of rows
  • count(p.product_id) counts the number of non-null product_id values

reference:
1. zentut sql tutorial

Cover Letter, Resume, Interview

1. Interview Question
  • Tell me about yourself.
  • What are you immediate objectives? What are your future aspirations?
  • What are your strengths and weaknesses?
  • What interests you in the position and/or our company?
  • What do you know about our company?
  • Can you give me an example of a time when you demonstrated the ability to be a team player?
  • Can you tell me an example of a specific time/event in your career when you displayed initiative? (Other areas may include time management, leadership, teamwork, problem solving, conflict resolution, staff motivation and the ability to work independently)

2. Ask the Interviewer
  • How has this vacancy come about?  
  • What will be the next step in the selection process?

Wednesday, 16 September 2015

cucumber bdd


1.  feature files
feature: <brief title>
    as a <role>
    i want a <feature>
    so that i can get <expected benefit>

    # additional background information
    <optional description>

    # acceptance criteria
    scenario: <brief title>
    <optional description>
        given <describe prerequisites>
        when <describe interaction or state change>
        then <describe intended results with "should">


2. scenario
  • each scenario consists a list of steps
  • steps start with keyword 'given', 'and', 'when', 'then', 'but'

3. step definition (generation)
  • each 'given' 'when' 'and' 'then' step can be auto generated but need to be implemented
  • cucumber search feature file and step definitions in classpath when run
  • also mark /src dir as source root so that step function can be found by ide (see link)
  • get rid of the 'throw new pendingException()' at the end of auto generated step function, otherwise junit/testNG will not run
  • how runner find where step definition is: glue = {"com/restassured/example"}
com.nab.ewcs.ubankac.se.FeatureRunner -t @mytest -g classpath:com/nab/ewcs/ubankac/se classpath:features -f html:cucumber-reports/cucumber-pretty --glue /Users/P781889/repo/tracker-SE-test/src/test/java/com/nab/ewcs/ubankac/se/ /Users/P781889/repo/tracker-SE-test/src/test/resources/features

4. background

feature: multiple site support
    as a multiple site owner
    i want to host blogs for different people
    in order to make gigantic piles of money

background:
    given a global administrator named "Greg"
    and a blog named "Greg's anti-tax rants"

scenario: Dr. Bill posts to his own blog
    given I am logged in as Dr. Bill
    when I try to post to "Expensive Therapy"
    then I should see "Your article was published."
  • background is run before each scenarios but after any Before Hooks

5. scenario outline - a list of scenarios

scenario outline: feeding a suckler cow
    given the cow weighs <weight> kg
    when we calculate the feeding requirements
    then the energy should be <energy> MJ
    and the protein should be <protein> kg

    examples:
    | weight | energy | protein |
    | 450      | 26500  | 215      |
    | 500      | 29500  | 245      |


6. 'before' and 'after' hooks

public class StepDefinitions {
     @before("@hook1")
     public void beforeScenario() {
     }

     @after("@hook1")
     public void afterScenario() {
     }
}
    • @before will be run before 1st step of each scenario
    • @before and @after can be used in same manner as in testng to setup and teardown
    • @before hook is useful to setup env while @after hook is useful to clean up env after each run

      7. capture groups

      given User enters Number 123 and String "abc"

      @given("^User enters Number (\\d+) and String \"([^\"]*)\"$") .      //match something in double quote
      public void user_enters_UserName_and_Password(int arg1, String arg2) throws Throwable {
          // write code here that turns the phrase above into concrete actions
          throw new PendingException();
       }

      given a blog post named "Random" with Markdown body  //doc string
       """
      some title, eh?
      ===============
      here is the first paragraph of my blog post.
      lorem ipsum dolor sit amet, consectetur adipiscing elit.
      """

      @given("^a blog post named \"([^\"]*)\" with markdown body$")
      public void a_blog_post_named_with_Markdown_body(String arg1) throws{ }

      given the following animals:   // data table
      | cow   |
      | horse |
      | sheep |

      @given("the following animals:")
      public void the_following_animals(List<String> animals) { }

      given the following users exist:   // data table
      | name  | email | twitter |
      | Aslak | aslak@cucumber.io | @aslak_hellesoy |
      | Julien | julien@cucumber.io | @jbpros |
      | Matt | matt@cucumber.io | @mattwynne |

      @given("^the following users exist:$")
      public void the_following_users_exist(DataTable arg1){ }
      • captured groups are captured 'arguments'
      • capture groups are strings (even when they match digits like \d+). for statically typed languages, cucumber will automatically transform those strings into the appropriate type
      
      
      8. regular expression

      ^I'm logged in$     //begin to end
      .                   //any char
      *                   //preceding token 0 or more times
      +                   //preceding token 1 or more times 
      d                   //any digit
      ?                   //previous token optional
      [^abc]              //anything but abc
      \d                  //match a digital char, same as [0-9]



      9. junit test runner
          @RunWith(Cucumber.class)
          @CucumberOptions(
              features = "Feature"
              ,glue={"stepDefinition"}
              )
          public class CucumberTest {
          }
      •  be careful when using maven, 'cucumber' lib version need to be compatible
          <dependency>
              <groupId>info.cukes</groupId>
              <artifactId>cucumber-java</artifactId>
              <version>1.2.4</version>
          </dependency>
      
          <dependency>
              <groupId>info.cukes</groupId>
              <artifactId>cucumber-junit</artifactId>
              <version>1.2.4</version>
          </dependency>
      
      
      
      10. testNG tester runner
          @CucumberOptions(
          features = "Feature"
          ,glue={"stepDefinition"}
          )
          public class NewTest extends AbstractTestNGCucumberTests{
          }
          <dependency>
              <groupId>info.cukes</groupId>
              <artifactId>cucumber-java</artifactId>
              <version>1.2.4</version>
          </dependency>
          <dependency>
              <groupId>info.cukes</groupId>
              <artifactId>cucumber-testng</artifactId>
              <version>1.2.4</version>
              <scope>test</scope>
          </dependency>  


      11. do not mix Junit and TestNG runner in same Maven project
      • junit runner will need 'cucumber-junit' dependency
      • testNG runner will need 'cucumber-testng' dependency
      • if both dependencies are included in same Maven project, one or both runner will not run properly

      12. writing feature file
      • wording should not be implementation specific
      • ask yourself, will this wording need to change if the implementation does?
      • user parameter to reduce duplicate step functions 
         When I am on "A" website
         Then I check page title is "title_A" 
         When I am on "B" website 
         Then I check page title is "title_B"

      13. @CucumberOptions(tags)
         @CucumberOptions(tags={"@one","@two"}
         Has @one AND @two tag
         @Cucumber.Options(tags = {"@one, @two"}) 
         Has @one OR @two tag 
      
      
      
      

      16. cucumber-jvm convension (works for maven)

      Example
      └───src
          ├───main
             └───java
          └───test
              ├───java
                 └───com
                     └───bensnape
                         └───example
                                 MyStepdefs.java
              
              └───resources
                  └───com
                      └───bensnape
                          └───example
                                  example.feature

      17. cucumber best practice
      • write "declarative" feature in domain specific language
      • insert a narrative
      • use background when necessary

      18. dependency injection in cucumber
      • a single INSTANCE of class is shared between many step definition class that use it
      • you will never need to create the INSTANCE using 'new'
            <dependency>
                  <groupId>info.cukes</groupId>
                  <artifactId>cucumber-picocontainer</artifactId>
                  <version>${cucumber.version}</version>
                  <scope>test</scope>
            </dependency>
            <dependency>
                  <groupId>org.picocontainer</groupId>
                  <artifactId>picocontainer</artifactId>
                  <version>${picocontainer.version}</version>
                  <scope>test</scope>
            </dependency>

            class AccountSteps {
                  KnowsTheDomain helper;  //defined in another class file, can be shared state among
                                                              //many step classes

                  public AccountSteps(KnowsTheDomain helper) {
                        this.helper = helper;
                  }
            }
      • picoContainer created a new instance of KnowsTheDomain for each scenario and injected that instance into every step definition class that needed it

      19. cucumber maven plugin

      20. cucumberjs datatable


      21. cucumberjs rerun failed test cases
      • on the first run add the parameters --format rerun:@rerun.txt
      • after a failed run, remove any arguments specifying the locations of feature files and add @rerun.txt
      ./node_modules/.bin/cucumber-js acceptance-tests/features/*.feature --tags @mytest -r acceptance-tests/steps --format json:acceptance-tests/output/cucumber.json --format rerun:@rerun.txt

      ./node_modules/.bin/cucumber-js @rerun.txt --tags @mytest -r acceptance-tests/steps --format json:acceptance-tests/output/cucumber.json --format rerun:@rerun.txt


      22. junit runner rerun failed test (see link)

      23. cucumberjs run in parallel
      • --parallel 4


      References:
      1. cucumber reference 
      2. toolsQA cucumber java test example
      3. cucumber with testNG 
      4. cucumber java implementation 
      5. cucumber selenium testNG maven example
      6. cucumber "background" keyword
      7. cucumber hooks
      8. cucumber hooks example
      9. cucumber tutorials
      10. cucumber step definition 
      11. regular expression syntax
      12. what does this regex mean?
      13. 15 expert tips for using cucumber 
      14. define step function's location for cucumber jvm 
      15. youtube: cucumber jvm setup 
      16. just enough regex for cucumber
      17. my stackflow question
      18. test your frontend code
      19. cucumber in java - 10 min tutorial
      20. cucumberjs datatable
      21. cucumberjs rerun failed tests
      22. how to rerun failed test cases in cucumber
      23. cucumberjs run feature file in parallel
      24. use maven cucumber reporting plugin
      25. intelliJ cannot find any declarations

      Monday, 14 September 2015

      selenium grid in the cloud

      1. what is selenium grid
      • when running test in local, use selenium webdriver to communicate with browser directly
      • when running test remotely (grid or cloud), use DesiredCapabilites + RemoteWebDriver
      • start selenium server as hub and node, single hub, multiple nodes, node register to hub
      • distribute test over different platform, different browsers in parallel
      • hub receive 'test' request with 'browser' and 'platform' information, and assign to node that has such capability

      2. install selenium grid
      • java -jar selenium-server-standalone-2.44.0.jar -role hub (install hub)
      • java -jar selenium-server-standalone-2.31.0.jar -role node -hub http://localhost:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15 -Dwebdriver.chrome.driver=lib\chromedriver.exe (install node)

      3. maxInstance vs maxSession
      • maxInstance (5 firefox and 5 ie in remote machine)
            -browser browserName=firefox,version=12,maxInstances=5,platform=LINUX
            -browser browserName=InternetExplorer,version=9.0,maxInstances=5,platform=LINUX
      • maxSession = 8 (any given time, total 8 sessions, 5ff3ie, 4ff4ie etc)

      4. change test script and xml to add 'RemoteWebDriver', 'DesiredCapabilities', 'parallel', 'thread-count' etc

      5. suggestions
      • on node machine, at least 1GB memory for each browser instance
      • driver instance quit after each test
      • use the same version of 'selenium-server' on hub and node

      6. advantage to run selenium testing on the cloud
      • save time, tests run in parallel
      • covers all platforms, all browsers and versions
      • no need to scale-up

      7. run selenium on Sauce Labs
      • install Sauce Labs Jenkins plugin
      • configure Sauce Labs username/access key
      • configure a Jenkins job

      8. bridged network, NAT, host-only network in virtual machine
      • NAT: vm and host system in within the same NAT
      • bridged: create another node in external physical network and vm receive its ip address
      • host-only: create a new LAN within host system, only accessible within host
      Virtualbox Networking Modes


      reference
      1. selenium grid showing WebDriverException error
      2. how to speed up test execution using selenium grid
      3. setting up grid2 and working with it
      4. selenium jenkins - how to do it yourself and the Sauce Labs advantage
      5. configuring the jenkins Sauce OnDemand plugin
      6. selenium grid 2 
      7. host-only, NAT and bridged network

      Thursday, 10 September 2015

      mobile/cross browser testing

      1. mobile testing
      1.  hardware related
      2.  core functions:  sms, call, bluetooth, wifi, mobile network, battery

      2. mobile application testing
      1. change of orientation (activity is destroyed) 
        is screen redrawn correctly?
        does application remember its state correctly?
        app should not lose what user entered in the UI
        app should not 'forget' its place in the current transaction
         
      2. change of configuration
        availability of keyboard or system language will also destroy the application
         
      3. switch your app with another app (navigate to home screen, then return to your app) will cause possible memory leak

      4. interrupt by call/sms/push notification/multi-task
      5. network status change (2g/3g/4g/wifi, offline, flight mode)
      6. install/uninstall
      7. screensize
      8. internationalization

        3. monkey test
        1. clean bootup, do not set lock screen for the phone
           
        2. adb devices
          adb shell
          pm | grep your.package.name
          adb shell monkey -p your.package.name -v 500 > monkey_report.txt
           
        4. test tricks
        • special char
        • come back and edit
        • back on browser or refresh page
        • mobile and multiple browsers

        5. set cookie in console
        • document.cookie = "key=value";

        6. mobile automation

        Thursday, 3 September 2015

        selenium testing best practice

        1. how to select test cases to automate?
        • how does business make money?
        • how do users use your application?
        • what browsers are user using?
        • what things have broken in the past?

        2. type of automation framework
        • module based
        • data driven
        • keyword driven (table lookup)
        • bdd (cucumber, jbehave)

        3. page object model
        • single place for the service offered by the page
        • clean separation between test code and page specific code

        4. page factory
        • a class provided by selenium webdriver to support page object model
        • allow testers use @FindBy annotation
        • use the initElements method to initialize all the web elements located by @FindBy

        5. avoid thread.sleep, prefer wait

            (new WebDriverWait(driver, 30)).until(new ExpectedCondition() {
                        public Boolean apply(WebDriver d) {
                            return d.getTitle().toLowerCase().startsWith("java developer")             

            }

        6. hrm modules
        • users
        • payroll
        • personal information
        • leave and timesheet
        • recruitment
        • performance
        • training

        7. ecommerce modules
        • customer registration and account management
        • product catalog
        • orders and payment
        • coupons
        • store locator
        • invoice and receipt

        8. do not use try/catch in bdd code
        • so that test will fail-fast (any run-time exception will terminate the step/step definition)

        9. visual testing using blue-harvest

        10. common issues
        • handle UnexpectedAlertOpenError in protractor
        • headless mode

        11. open new page
        • click something to open
        • findInteractableWebElement on the new page (to know that new page is open)


        reference
        1. figuring out what to test
        2. explicit vs implicit waits
        3. page object model
        4. selenium Wiki
        5. mixing Implicit Wait and Explicit Wait 
        6. serenity tips and tricks
        7. explicit and implicit timeout in serenity
        8. implicit vs explicit wait
        9. visual regression testing using blue-harvest
        10. handle UnexpectedAlertOpenError in Protractor
        11. headless mode