lundi 5 août 2019

SpringBoot test failing when I add SSL keystone/certification

I am trying to add SSL authentication to my spring boot server.

I have added the following to my application.properties

server.port=9119
logging.config=classpath:logback-spring.xml
logging.file=messages
logging.file.max-size=50MB
logging.level.com.nulogix=DEBUG
server.ssl.key-alias=selfsigned_nulogix_001
server.ssl.key-store-password=Nul0gix
server.ssl.key-store=/Users/asluborski/Documents/mock.jks
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
billing.engine.address=127.0.0.1
billing.engine.port=9119
billing.engine.api.version=0.97
version=0.97
billing.engine.core.name=Patient_Responsibility

I generated a keystore/certificate using the following command while in my Documents directory on OSX

keytool -genkey -alias selfsigned_nulogix_001 -keyalg RSA -keysize 2048 -validity 700 -keypass Nul0gix -storepass Nul0gix -keystore mock.jks

This generated the keystore and I see the image in my directory file.

Now when I run my tests in terminal using

mvn test -Dbilling.engine.address=127.0.0.1 -Dbilling.engine.port=9119 -Dserver.ssl.key-alias=selfsigned_nulogix_001 -Dserver.ssl.key-store=/Users/asluborski/Documents/mock.jks -Dserver.ssl.key-password=Nul0gix

It gives me an http response error

[ERROR] Errors: 
[ERROR]   AnalyzeEndPointTest.testSampleCms:127 » HttpResponse 
[ERROR]   AnalyzeEndPointTest.testSampleNormal:98 » HttpResponse 
[ERROR]   HttpClientTest.test_bad:54 » HttpResponse 
[ERROR]   HttpClientTest.test_good:76 » HttpResponse 

When I just include

mvn test -Dbilling.engine.address=127.0.0.1 -Dbilling.engine.port=9119

It passes all the tests

Why is this? Do I have to store the keystore in my class path? I would want to avoid that way...

My server is able to start when I run it on Spring Boot as a java app.

Here are my test classes but I do not believe my code has any reason with the problem.

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

import com.google.gson.Gson;
import com.nulogix.billing.mockserver.MockServerApp;

public class HttpClientTest {

    public static final String request_bad  = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|";
    public static final String request_good = "ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0";


    static ConfigurableApplicationContext context;

    @BeforeClass
    static public void  setup(){
        SpringApplication springApplication = new SpringApplicationBuilder()           
                .sources(MockServerApp.class)
                .build();
        context = springApplication.run();
    }

    @AfterClass
    static public void tearDown(){
        SpringApplication.exit(context);
        }

    @Test
    public void test_bad() throws ClientProtocolException, IOException {
        // missing parameter
        String result = Request.Post("http://127.0.0.1:9119")
                .connectTimeout(2000)
                .socketTimeout(2000)
                .bodyString(request_bad, ContentType.TEXT_PLAIN)
                .execute().returnContent().asString();

        Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);

        // ensure the key exists
        assertEquals(resultJsonObj.containsKey("status"), true);
        assertEquals(resultJsonObj.containsKey("errorMessage"), true);

        // validate values
        Boolean status = (Boolean) resultJsonObj.get("status");
        assertEquals(status, false);
        String errorMessage = (String) resultJsonObj.get("errorMessage");
        assertEquals(errorMessage.contains("Payload has incorrect amount of parts"), true);
    }


    @Test
    public void test_good() throws ClientProtocolException, IOException {
        String result = Request.Post("http://127.0.0.1:9119")
                .connectTimeout(2000)
                .socketTimeout(2000)
                .bodyString(request_good, ContentType.TEXT_PLAIN)
                .execute().returnContent().asString();

        Map<?, ?> resultJsonObj = new Gson().fromJson(result, Map.class);

        // ensure the key exists
        assertEquals(resultJsonObj.containsKey("status"), true);
        assertEquals(resultJsonObj.containsKey("errorMessage"), false);
        assertEquals(resultJsonObj.containsKey("HasCopay"), true);
        assertEquals(resultJsonObj.containsKey("CopayAmount"), true);
        assertEquals(resultJsonObj.containsKey("HasCoinsurance"), true);
        assertEquals(resultJsonObj.containsKey("CoinsuranceAmount"), true);
        assertEquals(resultJsonObj.containsKey("version"), true);

        // validate values
        Boolean status = (Boolean) resultJsonObj.get("status");
        assertEquals(status, true);
        String version = (String) resultJsonObj.get("version");
        assertEquals(version, "0.97");
    }

}

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.UnknownHostException;
import javax.xml.bind.JAXBException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import com.nulogix.billing.configuration.EndPointTestConfiguration;
import com.nulogix.billing.mockserver.MockServerApp;
import com.nulogix.billing.model.Constants;
import com.nulogix.billing.service.Analyze;
import com.nulogix.billing.service.AnalyzeResponse;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = EndPointTestConfiguration.class)

public class AnalyzeEndPointTest {

    public static final String PATTERN_BEGIN = "<AnalyzeResponsePayload";
    public static final String PATTERN_FINI = "</AnalyzeResponsePayload>";
    public static final String COINSURANCE_BEGIN = "<coInsurancePercentage>";
    public static final String COINSURANCE_END = "</coInsurancePercentage>";
    public static final String COINSURANCE_AMOUNT_B = "<coInsuranceAmount>";
    public static final String COINSURANCE_AMOUNT_E = "</coInsuranceAmount>";
    public static final String COPAY_AMOUNT_B = "<copayAmount>";
    public static final String COPAY_AMOUNT_E = "</copayAmount>";





    @Autowired
    private AnalyzeEndPoint analyzeEndpoint;

    // This is for demo mode, no interaction with billing server
    @Autowired
    private String studyDetailDemo;

    // There are for live integration test
    @Autowired
    private String studyDetailSampleNormal;

    @Autowired
    private String studyDetailSampleCms;



     static ConfigurableApplicationContext context;

    @BeforeClass
    static public void  setup(){
        SpringApplication springApplication = new SpringApplicationBuilder()           
                .sources(MockServerApp.class)
                .build();
        context = springApplication.run();
    }

    @AfterClass
    static public void tearDown(){
        SpringApplication.exit(context);
        }

    @Test
    public void testDemo() throws UnknownHostException, JAXBException, IOException {
        Analyze request = new Analyze();
        request.setStudyContainer(studyDetailDemo);
        AnalyzeResponse analyzeResponse = analyzeEndpoint.doAnalyze(request);
        String result = analyzeResponse.getAnalyzeReturn();
        int idx1 = result.indexOf(PATTERN_BEGIN);
        int idx2 = result.indexOf(PATTERN_FINI);
        assertNotNull(result);
        assertTrue("", idx1 >=0);
        assertTrue("", idx2 >=0);
    }
    @Test

    public void testSampleNormal() throws UnknownHostException, JAXBException, IOException {
        Analyze request = new Analyze();
        request.setStudyContainer(studyDetailSampleNormal);
        AnalyzeResponse analyzeResponse = analyzeEndpoint.doAnalyze(request);
        String result = analyzeResponse.getAnalyzeReturn();
        int idx = result.indexOf(PATTERN_BEGIN);
        result = result.substring(idx);
        result = result.replaceAll(Constants.CDATA_TAIL, "");
        int idx1 = result.indexOf(COINSURANCE_AMOUNT_B) + COINSURANCE_AMOUNT_B.length();
        int idx2 = result.indexOf(COINSURANCE_AMOUNT_E);
        int idx3 = result.indexOf(COPAY_AMOUNT_B) + COPAY_AMOUNT_B.length();
        int idx4 = result.indexOf(COPAY_AMOUNT_E);
        int idx5 = result.indexOf(COINSURANCE_BEGIN) + COINSURANCE_BEGIN.length();
        int idx6 = result.indexOf(COINSURANCE_END);
        String coInsurancePercent = result.substring(idx5,idx6);
        Double coInsurancePercentDouble = Double.valueOf(coInsurancePercent);
        String coPayAmount = result.substring(idx3,idx4);
        Double coPayAmountDouble = Double.valueOf(coPayAmount);
        String coInsuranceAmount = result.substring(idx1,idx2);
        Double coInsuranceAmountDouble = Double.valueOf(coInsuranceAmount);
        assertNotNull(result);
        assertTrue("", coInsuranceAmountDouble == 0.0);
        assertTrue("", coPayAmountDouble == 0.11);
        assertTrue("",coInsurancePercentDouble == 0.0);

        // TODO: check response code
    }

    @Test
    public void testSampleCms() throws UnknownHostException, JAXBException, IOException {
        Analyze request = new Analyze();
        request.setStudyContainer(studyDetailSampleCms);
        AnalyzeResponse analyzeResponse = analyzeEndpoint.doAnalyze(request);
        String analyzeReturnXml = analyzeResponse.getAnalyzeReturn();
        int idx = analyzeReturnXml.indexOf(PATTERN_BEGIN);
        analyzeReturnXml = analyzeReturnXml.substring(idx);
        analyzeReturnXml = analyzeReturnXml.replaceAll(Constants.CDATA_TAIL, "");
        int idx1 = analyzeReturnXml.indexOf(COINSURANCE_BEGIN) + COINSURANCE_BEGIN.length();
        int idx2 = analyzeReturnXml.indexOf(COINSURANCE_END);
        String coInsurance = analyzeReturnXml.substring(idx1, idx2);
        Double coInsuranceDouble = Double.valueOf(coInsurance);
        int idx3 = analyzeReturnXml.indexOf(COINSURANCE_AMOUNT_B) + COINSURANCE_AMOUNT_B.length();
        int idx4 = analyzeReturnXml.indexOf(COINSURANCE_AMOUNT_E);
        String coInsuranceAmount = analyzeReturnXml.substring(idx3,idx4);
        Double coInsuranceamountDouble = Double.valueOf(coInsuranceAmount);
        int idx5 = analyzeReturnXml.indexOf(COPAY_AMOUNT_B) + COPAY_AMOUNT_B.length();
        int idx6 = analyzeReturnXml.indexOf(COPAY_AMOUNT_E);
        String coPayAmount = analyzeReturnXml.substring(idx5,idx6);
        Double coPayAmountDouble = Double.valueOf(coPayAmount);
        assertTrue("", coInsuranceDouble == 19.2);
        assertTrue("",coInsuranceamountDouble == 65.9);
        assertTrue("",coPayAmountDouble == 0);

        // TODO: check response code and make sure it is 0.
    }

}

Also, when I try to use my curl command for post it says it fails to connect to localhost: connection refused

Aucun commentaire:

Enregistrer un commentaire