Thursday, December 20, 2018

OC Principal of SOLID Design


The-openclosed-principle:
Open to Extension: Collections. Sort(<>, comparator)
                - It is just the comparator implementation, going to be changed.
Closed for Modification
                                - If behavior changes, we should not be changing the entities
                                                - implementation closed to modification
                                -Sort method is not going to change
One of the strategy ‘Program by Interface, not by Implementation’ helps to achieve this
Backed by strategy pattern (behavior)
Strategy Pattern:
Encapsulate the strategies and decide what to use depending on some specific conditions.
StrategyClassà implements Strategy interface
Context Class  <uses> strategy classes
Sample


//Define Strategy
public class ServiceInvoker {
    public void invokeSomething();
}

//Strategy1
NasdaqApiInvoker implements ServiceInvoker {
    public void invokeSomething(){
}
}
//Strategy2
NikkeiApiInvoker implements ServiceInvoker {
    public void invokeSomething(){
}
}

//Context class to inject strategy
SockPriceFetcher
ServiceInvoker serviceInvoker;
StockPriceFetcher (ServiceInvoker serviceInvoker ){
this.serviceInvoker= serviceInvoker;
}
fetchStockValue(){
 serviceInvoker. invokeSomething()
}
}

//Client
Class GetStockData{
StockPriceFetcher  stockPriceFetcher =  new StockPriceFetcher  (new NasdaqApiInvoker()) // StockPriceFetcher  is decoupled with specific strategy
sockPriceFetcher. fetchStockValue ()

StockPriceFetcher  stockPriceFetcher2= new StockPriceFetcher  (new NikkeiApiInvoker ()};
stockPriceFetcher2.. fetchStockValue ()

}

Sunday, September 30, 2018

Power of Mockito.

1. Spy
@Spy

ClassToBTested classToBTested= new ClassToBTested();

2. Call RealMethod
@Mock mockedClass
when(mockedClass).doSomething(<AnyArg>).thenCallRealMethod.

3. Argument capturing:
ArgumentCaptor< ArgumentClass1 > captor1= ArgumentCaptor.forClass(ArgumentClass1.class);

ArgumentCaptor< ArgumentClass2> captor2 =ArgumentCaptor.forClass(ArgumentClass2.class);
classToBTested.testSomeMethod(someArgument);

Mockito.verify(classToBTested).doSometing(captor1.capture(), captor2.capture()));
ArgumentClass1 argumentObj1 = captor1.getValue();
ArgumentClass2 argumentObj2 = captor2.getValue();

//Assert argumentObj1 data here

4. Avoid Spy Objects from invocation during mocking:
doReturn("xyz").when(mockedClass).invokingSomeOtherMethod().
when(mockedClass).invokingSomeOtherMethod().thenReturn("xyz");

5. Mocking new instance creation:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassToBeTested.class)
public class ClassToBeTested Test {

    private ClassToBeTested classToBeTested;

    @Before
    public void setUp() {
        classToBeTested = new ClassToBeTested();
    }

    @Test
    public void testSomeBehaviour() throws Exception {
        InternalClass internalClassObj = mock(InternalClass.class);//new InternalClass();
        PowerMockito.whenNew(InternalClass.class)
            .withAnyArguments().thenReturn(internalClassObj);

      classToBeTested.invokeBehaviourToBeTested();
 //verify the argument is internalClassObj

assert captor.getValue() equalsTo internalClassObj

doThrow(new Exception()).when(mockedObject).methodReturningVoid(...);

   

Sunday, July 22, 2018

Elastic Search Setup Concepts


-ES in NRT search framework.
- Only a small latency from a document is indexed until it is searchable.
- Latency is of 1 second.
- Cluster consists of nodes.
Shard is nothing but chunks whereas Replica is copy.

A mapping type is a way of separating the documents in an index into logical groups.
MySQL => Databases => Tables => Columns/Rows

Elasticsearch => Indices => Types => Documents with Properties

Aliases are created for index.
An Index can have multiple aliases.
Alias is used only for reads operation.
Write operation always point to index.
Read operation can hit and get result from replica as well.This way additional hardware to have more replicas will give better performance.
Updating shard is not easy. While we create a new index and reads will point to existing aliases pointing to new index.Write can’t do such. For a short interval 20. Writes will fail and hence the health check will fail.

Cat/_indices will give the size of indexes
Primary needs to be looked at. This will give sum of shards.

If we have hierarchy in the mapping parent.child.child in the query could be costly. In that case we can create a new node for  parent.child.child to have faster result.

Sunday, April 15, 2018

Service Discovery for Micro Services


Service Registeries Providers


  • Apache Zookeeper
  • Consul
  • Etcd


Service Discovery with Spring Boot 

Consul Provides support for
-Service discovery
-Health Checking
-K/V storage
-Multiple Data Centres

How to enable in Spring boot app

Add @EnableServiceDiscoveryClient to the service searchable


@RefreshScope
Allow properties automatically reloaded //can be used by both consumable and caller services

Add

Springboot  Bootstrap.yml for consul

application name:
name:consumable-service
Server:
port:


Now the consumable service can be invoked with /consumable-service and does not required host name and Port
Properties can be