Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 15 January 2022

Interview Preparation - Java - Experienced Professional

In this Blog, I am trying to collect useful blogs which helps for preparing Java Interview - For Experienced professional. 

0) Introduction to Algorithms and Data structure in Java

From 0 to 1: Data Structures & Algorithms in Java by Loony Corn

https://github.com/PacktPublishing/From-0-to-1-Data-Structures-Algorithms-in-Java

Popular Problem-Solving Approaches in Data Structures and Algorithms

https://medium.com/enjoy-algorithm/popular-problem-solving-approaches-in-data-structures-and-algorithms-6b4d30a0823d

1) For Java Interview preparation

https://medium.com/javarevisited/25-topics-and-resources-to-crack-java-developer-interviews-in-2021-8fbfe317513

https://blog.usejournal.com/from-hi-from-google-again-to-congratulations-on-your-offer-with-google-6f77e93be2bd

https://medium.com/nerd-for-tech/googles-interview-preparation-routine-be6647910a5b


2) For quick database concepts review

https://keerthiga14.medium.com/top-6-important-concepts-of-dbms-519faae8f065

3) For OAuth2 & Security

https://darutk.medium.com/the-simplest-guide-to-oauth-2-0-8c71bd9a15bb

https://darutk.medium.com/diagrams-and-movies-of-all-the-oauth-2-0-flows-194f3c3ade85

https://auth0.com/docs/authorization/which-oauth-2-0-flow-should-i-use

3) Rest API performance improvement

https://developers.google.com/blogger/docs/3.0/performance

4) Deploy Spring Boot microservice into Azure

https://docs.microsoft.com/en-us/learn/modules/azure-spring-cloud-workshop/

5) For Java Programming interview questions

https://www.java67.com/2018/06/data-structure-and-algorithm-interview-questions-programmers.html

6) Microservices using Spring Boot quick demo - ( daily code buffer you tube)

https://www.youtube.com/watch?v=BnknNTN8icw



Saturday, 13 June 2020

Java Best Practices to write readable and maintainable code

From my understanding and experience following are the few best practices to write readable and maintainable code

Variables:

Methods:

Exceptions:


As shown in the above diagram never ever catch Throwable

We can use Throwable in a catch clause, but we should never do it! If we use Throwable in a catch clause, it will not only catch all exceptions; it will also catch all errors. Errors are thrown by the JVM to indicate serious problems that are not intended to be handled by an application

Also we should avoid using directly Exception  in the catch block. Try to handle specific exception
Ex:

try{
      readFile();
    }
catch(FileNotFoundException) { }

In catch block,
1) Do not print stacktrace ,  instead use any logging framework
    Ex : catch(.... ex){
                               log.error(ex);
                               }
2) Throw the exception using custom exception wherever appropriate

Class

The class should meet following important principle
1. Follow SRP - Single Responsibility Principle
2. Program to an Interface
3. Maintain Strong Encapsulation within Class
4. Maintain high Cohesion
5. Always try to have minimum Coupling
6 . Use Dependency Injection
7.  Principle of Proximity i.e. well organized methods

SOLID Principle
Single Responsibility Principle
Open Closed Principle'
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion

Comments

- Use JavaDoc format if necessary
- Always remove commented out code before pushing to version control system i.e svn or git
- Use comments if necessary, it should not compensate for buggy code. Prefer giving comments only for public methods wherever necessary


Tests(Junit/Mockito)

- Always verify one thing per test
- 1 assertion per test (Except-few cases where it is not applicable)
- No if branching in tests

Test should have basic template like as shown below

 Arrange
   - Setup and initialize the objects and the environment
  Act
    - Exercise the functionality under test
 Assert
    - Verify the result

Test Pattern
AAA - Arrange-Act-Assert
BDD - Given - When - Then

Other Points
- Install Static Code Checker like SonarLint 




=====================Reference=================================
Google Java Style Guide
https://google.github.io/styleguide/javaguide.html

Mr. Andrejs Doronins Java Readability & Maintainability Course




Wednesday, 22 August 2018

Java Api Design/ Coding Standards Resources Or blog informations

Here I am trying to document/ create a repository of useful blogs about java coding standards or good API design with Java


1) What I learned from doing 1000 code reviews  by Steven Heidel

https://stevenheidel.medium.com/what-i-learned-from-doing-1000-code-reviews-fe28d4d11c71


2) API Design with Java 8 by Per-Åke Minborg

dzone article

Monday, 13 August 2018

Apache Maven Build Tool Learnings - Java

How to seperate unit and integration test?

- maven-surefire-plugin : designed to handle unit test(Junit)
- maven-failsafe-plugin : designed to handle integration tests


           Reference Blog for complete information
           How to separate integration test from unit test




Following is the process to run Unit and Integration Tests separately

1) Under properties section of the pom.xml file, define following properties
<skipTests>false</skipTests>
<skipITs>${skipTests}</skipITs>
<skipUTs>${skipTests}</skipUTs>

2) Under build section add maven-failsafe-plugin(for integration tests) and maven-surefire-plugin(for unit tests) as follows

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.13</version>
<configuration>

<skipTests>${skipTests}</skipTests>
<skipITs>${skipITs}</skipITs>
</configuration>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<trimStackTrace>false</trimStackTrace>
<skipTests>${skipUTs}</skipTests>
</configuration>
</plugin>

3) Below are the commands to skip/run the tests seperately

i) mvn install -DskipUTs : Skips Unit tests
ii)mvn install -DskipITs : Skips Integration tests
iii)mvn install -DskipTests : Skips both Unit and Integration Tests

4) We need to name the integration test as per maven-failsafe-plugin naming convensions for example, *IT.java




Following are the steps to skip tests by default but want the ability to re-enable tests from the command line in surefire plugin.

1) In the Properties section of the pom.xml need to add a property

<skipTests>true</skipTests>

2) Modify maven-surefire-plugin the plugin under build section as follows

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<configuration>

<trimStackTrace>false</trimStackTrace>

<includes>

<include>**/*Test.java</include>

</includes>

<excludes>

<exclude>**/*IntegrationTest.java</exclude>

<exclude>**/*IT.java</exclude>

</excludes>

<skipTests>${skipTests}</skipTests>

</configuration>

</plugin>

3) When we run , mvn clean install or mvn test, unit tests will not run by default

4) We can use "mvn install -DskipTests=false" to run the tests from the command line

Saturday, 11 August 2018

Read values from properties file in maven project- Java

If the file is placed under target/classes after compiling, then it is already in a directory that is part of the build path. The directory src/main/resources is the Maven default directory for such resources, and it is automatically placed to the build path by the Eclipse Maven plugin (M2E). So, there is no need to move your properties file.
The other topic is, how to retrieve such resources. Resources in the build path are automatically in the class path of the running Java program. Considering this, you should always load such resources with a class loader.

References : Stackoverflow


Example code:
String resourceName = "myconf.properties"; // could also be a constant
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
    props.load(resourceStream);
}
// use props here ...

Wednesday, 8 August 2018

Docker Containers v/s Virtual Machines

Docker

  - Application Delivery Technology
  -  Build an application with a Docker Image
  - Ship an application with Docker Hub
  -  Run an application with Docker Container
  - Avoid single point of failure





Docker Compose
   Defining and Running multi-container Applications.
   - Configuration defined in one or more files
   docker-compose.yml(default)
   docker-compose.override.yml(default)
   Multiple files specified using -f
   All paths relative to base configuration file

- Great for dev, staging, and CI

 Docker Swarm
  Native clustering for Docker
  Provides a unified interface to a pool of Docker hosts
  Fully integrated with Machine and Compose
  Serves the standard Docker API
  1.2 - Ready for Production
    - Reschedule containers when a node fails
    - Better node management


Differences between docker containers and virtual machines can be measured based on the operating system support, security, portability, and performance

Below diagram shows details information




References : https://www.docker.com/captains/arun-gupta



Monday, 11 June 2018

Java Helper class to invoke SOAP web service using proxy in Oracle ADF

Following is the code snippet can be used as a helper class to invoke SOAP web service via proxy in Oracle ADF



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import com.oracle.xmlns.apps.xxsrf.soaprovider.plsql.xxsrf_capex_api.XXSRF_CAPEX_API_PortType;
import com.oracle.xmlns.apps.xxsrf.soaprovider.plsql.xxsrf_capex_api.XXSRF_CAPEX_API_Service;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import oracle.adf.share.logging.ADFLogger;
import oracle.wsm.security.util.SecurityConstants;
import weblogic.wsee.jws.jaxws.owsm.SecurityPoliciesFeature;


public class WebserviceHelper {
  private static final ADFLogger LOGGER = ADFLogger.createADFLogger(WebserviceHelper.class);

  public WebserviceHelper() {
    super();
  }

  // Creating instance of Service.java class
  private static XXSRF_CAPEX_API_Service xXSRF_CAPEX_API_Service;
  private static InputStream input = null;
  private static Properties prop = new Properties();

  public XXSRF_CAPEX_API_PortType getWsPort() {
    String url = null;
    URL urlObj;
    try {
      input = new FileInputStream("/app/scripts/SRFConfig.properties");
      prop.load(input);
      url = prop.getProperty("CAPEX_APPROVAL_URL");
      LOGGER.info("WSDL URL " + url);
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      urlObj = new URL(url);
      QName qName =
          new QName("http://xmlns.oracle.com/apps/xxsrf/soaprovider/plsql/xxsrf_capex_api/",
              "XXSRF_CAPEX_API_Service");

      LOGGER.info("WSDL URL Object " + urlObj);
      LOGGER.info("qName " + qName);
      xXSRF_CAPEX_API_Service = new XXSRF_CAPEX_API_Service(urlObj, qName);
      SecurityPoliciesFeature securityFeatures =
          new SecurityPoliciesFeature(new String[] {"oracle/wss_username_token_client_policy"});
      XXSRF_CAPEX_API_PortType xXSRF_CAPEX_API_PortType =
          xXSRF_CAPEX_API_Service.getXXSRF_CAPEX_API_Port(securityFeatures);
      // Add your code to call the desired methods.
      BindingProvider bindingProvider = (BindingProvider) xXSRF_CAPEX_API_PortType;
      Map<String, Object> context = bindingProvider.getRequestContext();
      // context.put(WSBindingProvider.USERNAME_PROPERTY, "Test");
      // context.put(WSBindingProvider.PASSWORD_PROPERTY, "Test");
      context.put(SecurityConstants.ClientConstants.WSS_CSF_KEY, "user-key");
      // bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
      return xXSRF_CAPEX_API_PortType;
    } catch (MalformedURLException e) {
      LOGGER.info("Inside the catch block" + e);
      e.printStackTrace();
    }
    return null;
  }
}

Monday, 30 April 2018

Eclipse error “Could not find or load main class”

In my case issue occurred because of missing library. So below are the steps to make it work

1) Right click your project, Build Path --> Configure Build Path --> Java Build Path --> Libraries
2) Remove the missing libraries
3) Go to your main class and run it