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