Monday, July 22, 2019

API - Hypermedia Specifications

To make Hypermedia implementation easier, there are linking specifications already in existence. Each specification is slightly different. Let's try to compare different -2 specifications.

Referenced from Undisturbed Rest


Strength
Weaknesses
Collection + JSON
         Strong choice for Collections
         Templated queries
         Early wide adoption
         Recognized as a standard
         JSON only
         Lack of identifier for documentation
         More complex/ difficult to implement
HAL
         Dynamic
         Nestable
         Easy to read/implement
         Multi-format
         URL templating
         Inclusion of documentation
         Wide adoption
         Strong community
         Recognized as a standard hypermedia spec
         RFC proposed
         JSON/XML format architecturally different
         CURIE’s are tightly coupled
JSON LD
         Strong format for data linking
         Can be used across multiple data formats
         Strong community
         Large working groups
         Recognized by W3C as standard
         JSON-Only
         More complex to integrate/interpret
         No identifier for documentation
CPHL
         Designed for cross-platform consistency; allows loosely coupled documentation
         Incorporates API Definitions
         Method and code on demand
         Allows for multiple formats while also providing a strict naming structure for common actions
         Poor adoption/not heavily tested
         Can become bloated
         Work in progress
         Listed as brainstorming documents
SIREN
         Provides a more verbose spec
         Query templating and form fields
         Incorporates actions
         Multi-format
         Poor adoption
         Lacks documentation
         Work in progress
JSON API
         Simple versatile format
         Easy to read/implement
         Flat link grouping
         URL templating
         Wide adoption
         Strong community
         Recognized as hypermedia standards
         JSON-Only
         Lack of Identifier for documentation
         Still, work in progress

Saturday, July 20, 2019

API Development Glossary


API - Application Programming Interface
CRUD - Create Update and Delete
CPHL - Cross Plateform Hypertext Language
CORS - Cross-origin Resource Sharing
DDD - Domain-Driven Design
ESB - Enterprise Service Bus
JSON - Javascript Object Notation
HTTP - Hypertext transfer protocol
HTML - Hypertext markup language
HATEOAS - Hypermedia as the Engine of Application of State
REST - Representational State transfer
RPC - Remote Procedure Call
RAML - RESTFul API Modeling Language
SOAP - Simple Object Access Protocol
SOA - Service Oriented Architecture
SDD - Spec Driven Design ( API development perspective)
SLA - Service Level Agreement
URL - Uniform Resource Location
URI - Unified Resource Identifier
WSDL - Webservice Definition Language
XML - Extensible Markup Language
YAML - Yet Another Markup Language






Tuesday, August 8, 2017

Unit Testing with Mockito

Mockito is a mocking framework, It lets you write tests with a clean & simple API. Following steps need to be performed to test your code using Mockito.

1) Include following dependency in your pom.xml

2) Annotate Test class with @RunWith(MockitoJUnitRunner.class)

3) Use annotation  @Mock or mock() for creating mocks.

4) Use annotation  @InjectMocks for  injecting mocks/spies fields annotated with @Spy or @Mock

5) Use annotation @Spy or spy() for partial mocking, real methods are invoked but still can be verified and stubbed

6) Use annotation @Before for method if something you want be applied for all your tests. For example if you want to mock dependency which is applicable for all test methods.

7) Use verify()  to check methods were called with given arguments
8) Finally @Test annotation for testing methods.

Here is the complete example of Test class using Mockito

a) Class which needs to be mocked and tested 



b) Test Class 


This example will give you fair Idea how to mock Class and Test it using mockito. In next blog, I shall be covering some of other aspects as well like mocking Future Interface or using Spy, Use of Before and Mocking Rest Controllers of Spring.














Sunday, June 25, 2017

Creating Thread using Extending Thread Class

Here is an example of creating thread using extending Thread class mechanism. Moreover, example also help you to understand, how to pass the values to thread and use different methods like Sleep, interrupt etc.


Creating Thread using Implementing Runnable Interface

Here is an example for creating thread and passing values to it.
There are two ways to create thread in java
1) Implementing Runnable Interface
2) Extending Thread Class
This example uses first option

Finding object in List ( Use of Contain)

Here is an example of searching object in List. Trying to list down all possible ways of finding object from List.
1) Searching String from list.
2) Searching Employee Object using contain method
2) Using Stream API (Java 8)





Wednesday, June 21, 2017

Immutable Class in java

Here is the example, how to create immutable Class in java

package com.nitesh;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
 * Make all fields final and private.
 * Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is 
 * to make the constructor private and construct instances in factory methods.
 * If the instance fields include references to mutable objects, don't allow those objects to be changed:
 * Don't provide methods that modify the mutable objects.
 * Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, 
 * and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.
 * @author Nitesh.Gupta
 *
 */
public final class MyImmuatable {
/**
* Final Integer field, Integer is anyways immutable
*/
private final Integer immutableField1;
/**
* Final String field
*/
private final String immutableField2;
/**
* Final Date object which is mutable and need to be taken care explicitly
*/
private final Date immutableField3;
/**
* Final List Object which is mutable 
*/
private final List immutableField4;
/**
* Constructor which is taking all inputs through parameters
* @param immutableField1
* @param immutableField2
* @param immutableField3
* @param immutableField4
*/
public  MyImmuatable (Integer immutableField1, String immutableField2, Date immutableField3,List immutableField4){
this.immutableField1 = immutableField1;
this.immutableField2 = immutableField2;
this.immutableField3 = new Date(immutableField3.getTime());
this.immutableField4 = Collections.unmodifiableList(immutableField4);
}

public Integer getImmutableField1() {
return immutableField1;
}

public String getImmutableField2() {
return immutableField2;
}

public Date getImmutableField3() {
return immutableField3;
}

public List getImmutableField4() {
return immutableField4;
}

@Override
public String toString() {
return "MyImmuatable [immutableField1=" + immutableField1 + ", immutableField2=" + immutableField2
+ ", immutableField3=" + immutableField3 + ", immutableField4=" + immutableField4 + "]";
}


}