Sample Spring Test Client for Https

 

Sample Test Client for https:

 

@Test
	public void testAuth() throws Exception {

		TestRestTemplate restTemplate = new TestRestTemplate();
		restTemplate.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory(
			HttpClients.custom().setSSLSocketFactory(socketFactory()).build()));

		ResponseEntity httpsEntity = restTemplate.getForEntity("https://<your id>:<https port>/greeting",
				String.class);

		assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
	    assertThat(httpsEntity.getBody()).containsIgnoringCase("nahid");

	}

 

 

Sample Socket Factory example

 

	private SSLConnectionSocketFactory socketFactory() throws Exception {
		char[] password = "changeit".toCharArray();		
		KeyStore truststore = KeyStore.getInstance("JKS");
		truststore.load(new ClassPathResource("truststore.jks").getInputStream(), password);
		SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadKeyMaterial(truststore, password);
		builder.loadTrustMaterial(truststore, new TrustSelfSignedStrategy());
		return new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());
	}

 

Spring MVC : Date problem fix

Add this following code to resolve the date problem in your controller


@InitBinder
     private void dateBinder(WebDataBinder binder) {
          // The date format to parse or output your dates
          SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
          // Create a new CustomDateEditor
          CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
          // Register it as custom editor for the Date type
          binder.registerCustomEditor(Date.class, editor);
     }

RSA encryption example

This an example of rsa encryption (Size is 2048)  using openssl tool. Output will be in base64 format, so that it can be shared easily.

Step 1: Generate the private key file

$ openssl genrsa -out private.pem 2048

Step 2: Extract the public key file

$ openssl rsa -in private.pem -out public.pem -outform PEM –pubout

Step 3 : Save your secret key in secret.txt

 Step 4: Encrypt the secret file

$ openssl rsautl -encrypt -inkey public.pem -pubin -in secret.txt –out secret.ssl

Step 5: Convert to base 64

$ openssl enc –base64 –in secret.ssl –out secret.ssl.enc

AWS Api Gateway: Java Client Example

This is a sample of Java Client for AWS Api Gateway.



package com.test.client;
import com.amazonaws.opensdk.config.ConnectionConfiguration;
import com.amazonaws.opensdk.config.ProxyConfiguration;
import com.amazonaws.opensdk.config.TimeoutConfiguration;
import com.test.SampleSDK;
import com.test.model.SampleServiceRequest;
import com.test.model.SampleServiceResult;
public class TestClient {
       
       SampleSDK sdk;
       
       
       public TestClient() {
              initSdk();
              }
       
       
       private void initSdk() {
              
              ProxyConfiguration proxy = new ProxyConfiguration(); //optional
              proxy.setProxyHost("x.x.x.x"); //optional
              proxy.setProxyPort(portno);         //optional
              
              sdk = SampleSDK.builder()
              .connectionConfiguration(
              new ConnectionConfiguration()
              .maxConnections(100)
              .connectionMaxIdleMillis(1000))
              .timeoutConfiguration(
              new TimeoutConfiguration()
              .httpRequestTimeout(3000)
              .totalExecutionTimeout(10000)
              .socketTimeout(2000))
              .proxyConfiguration(proxy)
              .build();
              }
       public static void main(String[] args) {
                     
              TestClient testClient = new TestClient();
              
               SampleServiceResult result =testClient.getResult();
              
              
               System.out.print("result is:"+result.getSampleResponse().getGreetings());
              
       }
       
       public  SampleServiceResult getResult()
       {
              
              SampleServiceRequest rq = new SampleServiceRequest();   
                          
              SampleServiceResult result = sdk.getSampleJavaLambda(rq);
              
              return  result;
              
       }     
       
              
}

 

SoapUI: JDBC Step example

Follow these steps to prepare your first jdbc step in SoapUI

1) Copy & paste the oracle driver jar file in SoapUi’s lib directory
2) Create a jdbc step

example configuration of the newly created jdbc step as follows :

a) Driver: oracle.jdbc.driver.OracleDriver
b) Connection String: jdbc.oracle.thin:hr/hr@127.0.0.1:1521:XE

Write your query string “select * from departments”

Add assertion with “contains”

Run your step and observe!

SoapUI: Using Variables

You can use variables to your request so that you don’t have to change your request body every time.

Your project > Custom properties

Add your project variables

You can add variables by these steps:

1) keep your cursor on your request location (where you want to put your project variables)

2) Right click , use “Get Data..” , choose your variables from project section.

SoapUI: Groovy Script for accessing Mock Service Request

If you want to access your mock service request pragmatically via Groovy Script step and perform assertion, then you can following this sample code.

def mockService =testRunner.testCase.testSuite.project.getMockServiceByName("SampleMockService")

def mockOperation=mockService.getMockOperationByName("SampleRequest");

def requestContent=mockOperation.getLastMockResult().getMockRequest().getRequestContent();

log.info "result was: " +requestContent;

def found=null

if(requestContent.contains( "Your Key Word" ))
{
log.info("content found!")

found="found"

}

File file = new File("C:/temp/ExampleResult.txt")
file.write requestContent
println file.text

assert found=="found"