Saturday, 28 October 2017

Some of the programming best practices

1. Unit Tests should not be dependent on ordering.
2. The database connection should be mocked or use an in-memory database such as H2 when using in unit test.
3. Do now use try/catches in Jnit and just throw Exception from the test. JUnit will handle this better.
4. Use Constant.equals(variable) rather than var.equals(const) to avoid NullPointerException.

Example of Using AWS S3 and CloudSearch with java







import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cloudsearchdomain.model.SearchRequest;
import com.amazonaws.services.cloudsearchdomain.model.SearchResult;
import com.amazonaws.services.s3.transfer.Download;
import com.amazonaws.services.s3.transfer.Transfer.TransferState;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferProgress;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomainClient;

/**
 * This class has functionality of accessing the Amazon web services CloudSearch and S3 and downloads the file
 */
public class AWSOps
{
String endPoint = getInstance().getProperty("ENDPOINT");
public String strZipLogFilePath=Constants.ZIPLOGFILEPATH;
String ID="";
private static final Logger log=LoggerFactory.getLogger(AWSOps.class);
/**
* This method has functionality of accessing the Amazon web services CloudSearch and S3 and downloads the metric log file
* @throws SQLException
* @param Ids
* @throws IOException
* @throws ParseException
*/
public void awsOperations(ArrayList<String> Ids) throws SQLException, IOException, ParseException
{
String ACCESS_KEY = getInstance().getProperty("AWS_ACCESS_KEY");
String SECRET_KEY = getInstance().getProperty("AWS_SECRET_KEY");
String strFileID = null;
AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
AmazonCloudSearchDomainClient domain = new AmazonCloudSearchDomainClient(credentials);
domain.setEndpoint(endPoint);
SearchRequest request=new SearchRequest();
SearchResult result=new SearchResult();

for(String ID:Ids)
{
if(!ID.contains("SomeText"))
{
request.setReturn(Constants.WHAT YOU WANT  TO Return);
request.setQuery(ID);
domain.search(request);
result=domain.search(request);
String strCloudSearchResult=result.toString();
if(strCloudSearchResult.contains("some text"))
{
String arrCloudSearchResult[]=strCloudSearchResult.split(":");
for(int cloudSearchResultCounter=0;cloudSearchResultCounter<arrCloudSearchResult.length;cloudSearchResultCounter++)
{
if(arrCloudSearchResult[cloudSearchResultCounter].length()>40)
{
strFileID=arrCloudSearchResult[cloudSearchResultCounter].substring(0, 41);
}
}
TransferManager transferManager=null;
try
{
File fileZipLog = new File(strZipLogFilePath);
if(!fileZipLog.exists())
{
fileZipLog.createNewFile();
}
else
{
fileZipLog.delete();
fileZipLog.createNewFile();
}
transferManager=new TransferManager(credentials);
Download download= transferManager.download("Some text", "Some path on S3"+strFileID.trim()+"Some file on S3", fileZipLog);
TransferProgress progress=download.getProgress();
Double percentage=progress.getPercentTransferred();
for(int i=0;i<=100&&percentage!=100;i++)
{
TransferState state=download.getState();
percentage=progress.getPercentTransferred();
if(((!download.isDone())&& i==100))
{
i=0;
}
if(download.isDone())
{
if ("COMPLETED".equals(state.name()))
{
transferManager.shutdownNow();
break;
}
}
Thread.sleep(1000);
}
}
catch (Exception e)
{
log.error("In AWSOps | awsOperations Method:"+e);
throw new RuntimeException(e);
}
break;
}
else
{
log.info("In AWSOps | awsOperations Method: (ZipLogFile is not available)");
continue;
}
}
}
}
}

Developing web application using Java and Python

import logging
import logging.config
import os
import json
import socket
from datetime import datetime
from redis import Redis
from rq import Queue
from flask import Flask, redirect, url_for, request, json, jsonify, render_template
from java_service_invoke import java_service_invoker
app = Flask(__name__)

logger = logging.getLogger(__name__)
# Create the Handler for logging data to a file
logger_handler = logging.FileHandler('info.log')
logger_handler.setLevel(logging.DEBUG)
with open("logging.json", 'r') as logging_configuration_file:
    config_dict = json.load(logging_configuration_file)

logging.config.dictConfig(config_dict)
# Create a Formatter for formatting the log messages
logger_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')

# Add the Formatter to the Handler
logger_handler.setFormatter(logger_formatter)

# Add the Handler to the Logger
logger.addHandler(logger_handler)
logger.info('Completed configuring logger()!')

q = Queue(connection=Redis())


@app.route('/create-request', methods = ['POST', 'GET'])
def create_test():
    logger.info("In createRequest")
    try:
        username=request.form['username']
        start_date = str(datetime.strptime(request.form['startdate'], '%B %d, %Y').date())
        end_date = str(datetime.strptime(request.form['enddate'], '%B %d, %Y').date())
        email=request.form['email']
        logger.info(username+" "+start_date+" "+end_date+" "+email)
        result = q.enqueue(java_service_invoker, username, start_date, end_date, email, timeout=7200)
        return 'Done'
    except Exception as err:
        logger.error("Errro in request.", exc_info=True)
        return jsonify("Error")

@app.route('/success', methods = ['POST', 'GET'])
def post_test():
    try:
       logger.info("This is write in file.")
       logger.info("Request %s", request)
       request_json=request.get_json()
       return jsonify(request_json)
    except Exception as err:
       logger.error("Errro in request.", exc_info=True)
       return jsonify("Error")

@app.route('/', methods = ['GET'])
def display_form():
    return render_template('index.html')

 
 
if __name__ == '__main__':
   app.run(debug = True, host='0.0.0.0')



Refer Below link for Java program call:

https://utestautomation.blogspot.in/2017/10/python-program-to-invoke-jar-file.html

Python program to invoke jar file with the parameters

import subprocess


def java_service_invoker(username, startdate, enddate, email):
    print("Java service invoked!!!")
    subprocess.call(['java', '-jar', 'JAR FILE PATH', username, startdate, enddate,
                     email])
    print("Java service invoke done!")