Monday, April 28, 2025

GitHub: Squash all commits in PR

 

CommandMeaning
git fetch originGet the latest origin/master.
git reset --soft origin/masterMove your branch to match origin/master, but keep your work staged.
git commit -m "Final PR commit"Make a single commit containing all the changes.
git push --forceForce push the new clean history to your PR branch.

Tuesday, June 21, 2022

Android Test Automation with Espresso


What is Espresso?

  • It automatically synchronises your test actions with user interface of your application.
  • Framework also, ensure that your activity is started before the test runs. It also, lets the test wait until all observed background activities have finished 
  • The framework prevents direct access to activities and views of the application because holding on to these objects and operating on them off the UI thread is a major source of test flakiness. Thus, you will not see methods like getView() and getCurrentActivity() in the Espresso API. 
  • You can still safely operate on views by implementing your own subclasses of ViewAction and ViewAssertion.



UI test for single apps: 
  • verify that target app behaves as expected when user performs a specific action or enters specific input in activities 
  • it allows you to check that target app returns the correct UI output in response to user interactions in the app activities
  • Espresso allows you to simulate user actions and test complex app user interactions 

UI test for multiple apps:
  • this type of test provide the correct behaviour of interaction b/w different user apps or between user apps and system apps.
  • eg. you want to test that your camera app share images correctly with 3rd party social media application or with default android photo application 
  • UI Automator allows you to create such scenarios

API components

To Automate, we need 3 basic things i.e. find something, do something and check something

The main components of Espresso include the following:

  • Espresso – Entry point to interactions with views (via onView() and onData()). Also exposes APIs that are not necessarily tied to any view, such as pressBack().
  • ViewMatchers – 
    • Allows to find view in current view hierarchy (Find Something).
    • A collection of objects that implement the Matcher<? super View> interface. You can pass one or more of these to the onView() method to locate a view within the current view hierarchy.
  • ViewActions – 
    • allows to perform actions on the views (Do Something).
    • A collection of ViewAction objects that can be passed to the ViewInteraction.perform()method, such as click().
  • ViewAssertions – 
    • allows to alert state of a view (Check Something)
    • A collection of ViewAssertion objects that can be passed theViewInteraction.check()method. Most of the time, you will use the matches assertion, which uses a View matcher to assert the state of the currently selected view.


Espresso Test Recorder

Pros of Espresso Test Recorder
  • Allows to create effective UI based test cases with user interactions
  • we can capture assertions and interactions without accessing app's structure directly which increases execution speed and optimises test cases.
  • It saves lot of time to search for locators and then writing test cases.
  • It supports multiple assertions making more reliable test cases.
Cons of Espresso Test Recorder
  • Currently does not Support Recording of Web Views interactions
  • Once we complete Recording once for next time Recording it launches the app there is no API to control such behaviour.
  • Cann't record Assertions for Toast Messages.


Pros

  • Supported for all android versions
  • simpler and quicker to setup
  • supports Jacoco to measure code coverage
  • extensive testing is possible as it covers many UI actions and gestures (toast messages, camera activity, etc.)

Cons

  • The test cases are written inside android application project code and hence building project to run test case takes lot of time.




Gradle is used to give instructions for building your project based on instructions that you give in build.gradle file.




Chapter 1. Introduction to Espresso




 










What is AndroidX?



















Chapter 2. Setting up Espresso













 


Chapter 3. Writing the First Test




https://developer.android.com/training/testing/espresso/cheat-sheet





Friday, June 3, 2022

RESTful Web Service with SpringBoot



Spring Data JPA: to connect our REST API to a database. hence we can use Hibernate/JPA for this.


REST is a style of software architecture for distributed hypermedia systems

=========================================================================

# RESTful Web Services

Social media Application

User make many posts. hence, the relationship b/w user and posts is one to many.
  • Retrieve all Users -      GET /users
  • Create a User -             POST /users
  • Retrieve one User -      GET /users/{id}
  • Delete a User -             DELETE /users/{id}

  • Retrieve all posts for a User -        GET /users/{id}/posts
  • Create a posts for a User -             POST /users/{id}/posts
  • Retrieve details of a post -             GET /users/{id}/posts/{post_id}




  • We have used annotation for RestController
  • Then mapped GET request to the URI to the above method. 
  • Now, whatever my method will return, is displayed on the browser.

  • Also, we can use GetMapping, instead of RequestMapping.
========================================================================

Step 1. Enhancing the Hello World Service to return a Bean




This error means "No convertor found for return value of type".

So, If we don't create a GETTER, automatic conversion will not work.

This happened because there is not getters in HelloWorldBean class.

We have added GETTER also, to remove the above Exception.
So, variable, "message" is coming back as JSON string


=======================================================================


Now we will understand what's happening in the background?
Who is handling the request?
How HelloWorldBean content getting translated to JSON?


Step 2. RQuick Review of Spring Boot Auto Configuration and Dispatcher Servlet - What's happening in the background?

What is dispatcher servlet?
who is configuring dispatcher servlet?
What does dispatcher servlet do?
How does the HelloWorldBrean object get converted to JSON?
Who is configuring the error mapping?


============================
CONDITIONS EVALUATION REPORT
============================
This has lot of details of AutoConfiguration reports.


DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
      - found 'session' scope (OnWebApplicationCondition)

Above says, "DispatcherServletAutoConfiguration matched, because it found a class called DispatcherServlet "


  • We added a starter on SpringBoot Starter Web
  • SpringBoot Starter Web has dependency on spring web MVC framework
  • Hence, we got DispatcherServlet class into our classpath.
  • So, SpringBoot Autoconfiguration says, "I found DispatcherServlet on the classpath"
  • Hence, now we can configure the DispatcherServletAutoConfiguration

ErrorMvcAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
      - found 'session' scope (OnWebApplicationCondition)

It says "javax.servlet.Servlet classes are found on classpath, So we would need to configure error page"

ErrorMvcAutoConfiguration#basicErrorController matched:
      - @ConditionalOnMissingBean (types: org.springframework.boot.web.servlet.error.ErrorController; SearchStrategy: current) did not find any beans (OnBeanCondition)

   ErrorMvcAutoConfiguration#errorAttributes matched:
      - @ConditionalOnMissingBean (types: org.springframework.boot.web.servlet.error.ErrorAttributes; SearchStrategy: current) did not find any beans (OnBeanCondition)

   ErrorMvcAutoConfiguration.DefaultErrorViewResolverConfiguration#conventionErrorViewResolver matched:
      - @ConditionalOnBean (types: org.springframework.web.servlet.DispatcherServlet; SearchStrategy: all) found bean 'dispatcherServlet'; @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver; SearchStrategy: all) did not find any beans (OnBeanCondition)

So, its configuring the basicErrorController, errorAttributes and conventionErrorViewResolver.

The error page that we saw earlier was White label error page. 
WhitelabelErrorViewConfiguration has generated the error page.


So, all these configurations are getting fired because of something called SpringBoot AutoConfiguration. 

SpringBoot looks at all the classes, all the jars which are available on the classpath, and based on whatever is in the classpath, it tries to auto-configure different things like DispatcherServlet.


HttpMessageConvertersAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.http.converter.HttpMessageConverter' (OnClassCondition)
      - NoneNestedConditions 0 matched 1 did not; NestedCondition on HttpMessageConvertersAutoConfiguration.NotReactiveWebApplicationCondition.ReactiveWebApplication did not find reactive web application classes (HttpMessageConvertersAutoConfiguration.NotReactiveWebApplicationCondition)

Other things that are autoconfigured are HTTP Message Converters. So, these HTTP message converters are responsible for converting to JSON.
What is happening is called Jackson to object mapper.

JacksonAutoConfiguration.Jackson2ObjectMapperBuilderCustomizerConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.http.converter.json.Jackson2ObjectMapperBuilder' (OnClassCondition)

Jackson to object mapper does the conversion from JSON to beans and beans to JSON.

Who is configuring Dispatcher Servlet?
Ans. SpringBoot AutoConfiguration

How does the HelloWorldBrean object get converted to JSON?
Ans. Because of SpringBoot AutoConfiguration as Message Converters and Jackson beans are getting initialized. 

Who is configuring the error mapping?
Ans. SpringBoot AutoConfiguration, creates a default error page for us. 


==========================================================================

Mapping servlets: dispatcherServlet urls=[/], webServlet urls=[/h2-console/*]

[/] this is the root of the web applications


Dispatcher servlet do 2 things:
  1. convert to JSON
  2. sends back the response

  • When you type the URL /hello-world-bean, the request goes to DIspatcherServlet 
    • DispatcherServlet will be the one which is handling the request first. 
    • This follows the pattern called FRONT CONTROLLER.
    • DispatcherServlet is the front controller for spring MVC Framework.
    • So request goes to DispatcherServlet
    • DispatcherServlet knows all the different mapping present in the application
    • It knows /hello-world2 and /hello-world-bean GET method is mapped to what specific method
  • Then dispatcher servlet finds the HelloWorld Controller, 
  • It calls specific method helloWorldBean(), gets the object, invokes the conversion on it,
    • So, once, DispatcherServlet gets the request, it determines that what is the right controller to execute the request, and will execute the right method of the controller
    • Once object is returned back, then dispatcherServlet will look how do I send response back.
    • FOr this we have @RestController annotation. part of it has @ResponseBody
    • What would happen when we put ResponseBody on a controller?
    • the response from it would be mapped by message convertor to some other format.
    • Here, we use Jackson as message convertor
  • Converts it into JSON and then response back.
    • So, DispatcherServlet will say do the JSON conversion using jackson

Saturday, May 28, 2022

What is a WEB SERVICE?



            Interoperable: can be used by any application built in any technology.


=================================================================

How does data exchange between applications take place?



How can we make the web services platform independent?

Ans. Request and Response should be in proper formats, to support web service on different platforms
  • XML
  • JSON


How does Application A knows the format of Request and Response?


=================================================================

Web Service - Key Terminologies

Request: i/p to web service

Response: o/p from web service

Message Exchange Format: format of request and response

Service Provider or Server: which hosts the web service. 

eg. Web Service is a service provider

Service Consumer or Client: who consumes the web service

eg. Application A, JavaApplication, DotNetApplication, PHPApplication is a service consumer. 



Contract b/w service provider and service consumer.


Transport: how service is called.
  • One, weather service is exposed on the internet i.e. HTTP
    • HTTP is over the web like we use the URL in browser similarly Application_A will call web service.
  • Two, weather service is exposed over the transport which is queue i.e. MQ. 
    • The service requester would place a message in the queue. 
    • The service provider would be listening on the queue. 
    • As soon as there's request on the queue it would take the request, do the processing of it, create the response and put it back in the queue. 
    • The service requester would get the response from the queue.
    • The transport will be MQ, here it is WebSphere MQ


 =================================================================

Types of Web Services

  1. SOAP
  2. REST
SOAP and REST are not really comparable. 

REST defines an architectural approach.
SOAP poses restrictions on the format of XML which is exchanged between your service provider and service consumer.

Introduction of SOAP Web Services

  • SOAP defines a specific way of building web services.
  • Here we use XML as a request exchange format. 
  • It defines the specific XML request and response structure. 
SOAP
  • Format
    • XML Request and Response
  • Transport
    • SOAP over MQ
    • SOAP over HTTP
  • Service Defination
    • WSDL

  • WSDL defines the end point i.e. where your service is exposed.
  • It defines all the operations exposed
    • get all codes details
    • add a new course
    • delete course
  • Also, specifies the request and response structure.


SOAP is all about adhering to services XML structure. Adhering to envelop header and body.
 Once we have your request and response in that structure, then we can say that we are developing SOAP web services. 


=================================================================

Introduction to RESTful Web Service

when we enter the URL in the browser, a request is sent to the website server.
The website server responds back with a response. 

What is the format of request and response? 
These requests and responses are in a format defined by HTTP protocol.
In addition to request header and request body, HTTP also defines HTTP Methods and HTTL Status Codes.

Roy Fielding (who invented HTTP) said, why don't we use HTTP to develop web services.  
Hence, concept of RESTful webservices came.


RESTful web services try to define services using different concepts that are already present in HTTP.
Most important abstraction in REST is something called resource.
A resource is ant thing that you want to expose to outside world through your application.

eg. in TODO Management Service 
  • users are resource who are using it, Let Ram is a resource. 
  • specific TODO is a resource
  • LIST of TODOs is also a resource.

So, we define a URI for these resources.

  • A resource has an URI (Uniform Resource Identifier)
    • /user/ram/todos/1
    • /user/ram/todos
    • /user/ram
  • Resources ca have different representations
    • XML
    • HTML
    • JSON

The most important thing is the fact that you can define your resource and perform actions on these resources using whatever facilities are provided by HTTP.

 EXAMPLE
  • Create a User: POST /users
  • Delete a User: DELETE /users/1
  • Get all User: GET /users
  • Get one Users: GET /users/1

REST

  • Data Exchange Format
    • No restriction. JSON is popular
  • Transport
    • Only HTTP
  • Service Defination
    • No Standard. WADL/Swagger


Nginx Setup

 

1. yum install yum-utils 

2. yum-config-manager --add  https://openresty.org/package/centos/openresty.repo 

3. yum install openresty /usr/local/openresty/nginx 

4. cd  /usr/local/openresty/nginx/conf 

5. Add the proxy settings as per needs 

5. Edit nginx.conf to update the ip of the server in "proxy redirect" sections .For example, current server ip is 18.32.76.34.

6. Remove nginx.conf.default  

7. cd /usr/local/openresty/nginx/sbin  

8. nginx -p /usr/local/openresty/nginx/ -c conf/nginx.conf 

9. pkill -9 docker  



#user  nobody;

worker_processes  8;

worker_rlimit_nofile 100000;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

pid    /var/run/nginx.pid;

 

 

events {

    worker_connections 100000;

    use epoll;

    multi_accept on;

 

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

     ################# Gzip Settings ################

    gzip on;

    gzip_comp_level 4;

    gzip_min_length 1024;

    gzip_proxied any;

    gzip_static on;

    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js application/soap+xml;

    gzip_disable "MSIE [1-6]\.";

map $http_x_forwarded_proto $http_scheme

{ ~https https; default http; }

 

 

 

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                  '$status $body_bytes_sent '

                  'X-Auth-Token: "$sent_http_x_auth_token"';

 

 

 

 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    tcp_nopush     on;

    tcp_nodelay        on;

    keepalive_timeout  30;

    keepalive_requests 10000;

    reset_timedout_connection on;

    client_body_timeout 30;

    send_timeout 300;

    #gzip  on;

 

    server {

        listen       80;

        server_name  localhost;

        access_log  /usr/local/openresty/nginx/logs/frontend.log ;

        error_log   /usr/local/openresty/nginx/logs/error.log;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

 

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

         error_page   500 502 503 504  /50x.html;

        underscores_in_headers on;

 

        location = /50x.html {

            root   html;

 

        }

 

############################Redirection Logic For Comp1######

 

location /comp1/ {

 # proxy_redirect  http://12.89.29.40/comp1/api/v2.0/search/;

 #   proxy_read_timeout 60s;

      real_ip_header      X-Forwarded-For;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_set_header X-Forwarded-Proto $http_scheme;

           proxy_set_header  X-Real-IP $remote_addr;

                add_header  Access-Control-Allow-Origin *;

 proxy_pass      http://12.89.29.40:9023$request_uri;

 #                      ################# Gzip Settings ################

           gzip  on;

           gzip_comp_level 4;

           gzip_min_length 10240;

           gzip_proxied expired no-cache no-store private auth;

           gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js application/soap+xml;

           gzip_disable "MSIE [1-6]\.";

 

             }  

   

}


Friday, March 25, 2022

Docker Setup


1. Repository SetUp -  

Pre requisite - Login as sudo 

1. yum install -y yum-utils device-mapper-persistent-data lvm2 

2. yum-config-manager  --add-repo https://download.docker.com/linux/centos/docker-ce.repo 

 

 2. Installation of Docker Engine -  

     1. yum install docker-ce docker-ce-cli containerd.io 

      Installation of specific version of docker-  

     2. yum install docker-ce-18.03.0.ce  

 

    Updation of docker.service -  

    1. vi /usr/lib/systemd/system/docker.service 

 

    Update following properties in docker.service -  

    2. ExecStart=/usr/bin/dockerd --insecure-registry=myregistry.com -H tcp://0.0.0.0:2000 –H unix:///var/run/docker.sock 

    3. ExecReload=/bin/kill -s HUP $MAINPID 

 

    Flush Changes-  

    1.systemctl daemon-reload 

 

    Start Docker- 

    1. systemctl start docker 

Popular Posts

Most Featured Post

GitHub: Squash all commits in PR

  Command Meaning git fetch origin Get the latest origin/master . git reset --soft origin/master Move your branch to match origin/master , b...