Sunday, April 17, 2022

Installing Maven in AWS EC2 instances

 Following are the set of commands need to be executed sequentially to install maven.

  • sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
  • sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo
  • sudo yum install -y apache-maven
  • mvn –version

Thursday, April 7, 2022

 Issue:


Description:


Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.


Reason: Failed to determine suitable jdbc url



Action:


Consider the following:

If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


<Apr 6, 2022 6:15:45 PM EDT> <Error> <Deployer> <BEA-149265> <Failure occurred in the execution of deployment request with ID "35635055073500" for task "12". Error is: "weblogic.application.ModuleException: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine suitable jdbc url"

weblogic.application.ModuleException: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine suitable jdbc url

at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140)

at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)

at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:216)

at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:211)

at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42)

Truncated. see log file for complete stacktrace

Caused By: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine suitable jdbc url

at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineUrl(DataSourceProperties.java:283)

at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:176)

at com.alticeusa.adminconsole.admin.PrimaryHeimdallConfig.primaryDataSource(PrimaryHeimdallConfig.java:40)

at com.alticeusa.adminconsole.admin.PrimaryHeimdallConfig$$EnhancerBySpringCGLIB$$29fd0dc9.CGLIB$primaryDataSource$1(<generated>)

at com.alticeusa.adminconsole.admin.PrimaryHeimdallConfig$$EnhancerBySpringCGLIB$$29fd0dc9$$FastClassBySpringCGLIB$$b7c64311.invoke(<generated>)

Truncated. see log file for complete stacktrace

<Apr 6, 2022 6:15:45 PM EDT> <Error> <Deployer> <BEA-149202> <Encountered an exception while attempting to commit the 9 task for the application "admin-console-app".> 

<Apr 6, 2022 6:15:45 PM EDT> <Warning> <Deployer> <BEA-149004> <Failures were detected while initiating deploy task for application "admin-console-app".> 

<Apr 6, 2022 6:15:45 PM EDT> <Warning> <Deployer> <BEA-149078> <Stack trace for message 149004

weblogic.application.ModuleException: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine suitable jdbc url:org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.DataSourceBeanCreationException:Failed to determine suitable jdbc url

at org.springframework.boot.autoconfigure.jdbc.DataSo



Solution:

Tuesday, June 25, 2019

Running SOAP request in a Linux command line

curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:ws:shoppingCart" --data @shoppingcartreq.xml http://172.20.150.11:40100/Implws/services/ImplWSOrder

where

  1. "Content-Type: text/xml;charset=UTF-8" --> should be same if you are using a xml file
  2. "SOAPAction:ws:shoppingCart" --> need to specify the api that we are going to request
  3. shoppingcartreq.xml  -->  This should have the soap request - full soap request
  4. http://172.20.150.11:40100/Implws/services/ImplWSOrder --> URL of the webservice


another example: curl --header "Content-Type: text/xml;charset=UTF-8" --header "SOAPAction:ns2:searchProductOffering" --data @productreq.xml http://172.20.150.11:40100/Implws/services/GuidedSvc

Wednesday, April 4, 2018

Mockito and PowerMock Framework for JUNIT testing!

In this blog, i would like to share some of the important concepts in the Mockito/Powermock framework.

1. doReturn/when
2. eq
3. @InjectMocks
4 @Mock
5. @Runwith PowerMockRunner and Mockito
6. PowerMockito.doReturn(output).when(spyCbvApi,MemberMatcher.method(RestServices_CBV_APImpl.class,"getCVTDecision",GetCVTDecisionInput.class,boolean.class)).withArguments(any(GetCVTDecisionInput.class), Matchers.anyBoolean());
7. Naming convention for JUnit test cases.
8. whitebox
mocking static methods, variables
9. argumentcaptor
10. Mocking a static method:

https://stackoverflow.com/a/21116014/1739949
@RunWith(PowerMockRunner.class)
@PrepareForTest(DriverManager.class)
public class Mocker {

    @Test
    public void testName() throws Exception {

        //given
        PowerMockito.mockStatic(DriverManager.class);
        BDDMockito.given(DriverManager.getConnection(...)).willReturn(...);

        //when
        sut.execute();

        //then
        PowerMockito.verifyStatic();
        DriverManager.getConnection(...);

    }

Tuesday, December 19, 2017

Mockito Error

If you get this error, "No instance field named " reporter" could be found in the class hierarchy of org.mockito.internal.MockitoCore." you may be missing the mockito in your POM.xml dependencies, check it.  It will look like below (mockito and powermock):

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>1.10.19</version>
</dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.7.0</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.mockito</groupId>
                    <artifactId>mockito-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.7.0</version>
            <scope>test</scope>
        </dependency>

Wednesday, March 15, 2017

Disable Certificate Validation in Java SSL Connections

By design when we open an SSL connection in Java (e.g. through java.net.URL.openConnection(“https://….”)) the JSSE implementation of the SSL protocol performs few validations to ensure the requested host is not fake. This involves validation of the server’s X.509 certificate with the PKIX algorithm and checking the host name agains the certificate subject. If the SSL certificate is not validates as trusted or does not match the target host, an HTTPS and other SSL encrypted connection cannot be established and all attempts will result in SSLHandshakeException or IOException.

Example of HTTPS Connection in Java that will Fail Due to Certificate Validation Failure

Consider we are trying to download a resource from HTTPS server:
URL url = new URL("https://www.nakov.com:2083/");
URLConnection con = url.openConnection();
Reader reader = new InputStreamReader(con.getInputStream());
while (true) {
    int ch = reader.read();
    if (ch==-1) {
        break;
    }
    System.out.print((char)ch);
}

If the server uses self-signed X.509 certificate, we will get SSLHandshakeException the following exception during the SSL handshaking:

Exception in thread "main" <strong>java.io.IOException: HTTPS hostname wrong: should be <www.nakov.com></strong> at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing(Unknown Source) at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)

How to Turn Off Certificate Validation in Java HTTPS Connections


Avoiding these exceptions is possible by switching off the certificate validation and host verification for SSL for the current Java virtual machine. This can be done by replacing the default SSL trust manager and the default SSL hostname verifier:

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
 
public class Example {
    public static void main(String[] args) throws Exception {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
        };
 
        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 
        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
 
        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
 
        URL url = new URL("https://www.nakov.com:2083/");
        URLConnection con = url.openConnection();
        Reader reader = new InputStreamReader(con.getInputStream());
        while (true) {
            int ch = reader.read();
            if (ch==-1) {
                break;
            }
            System.out.print((char)ch);
        }
    }
}