Search This Blog

Breaking

Sunday, 28 February 2021

February 28, 2021

Print even digit first, odd digit later from a number

Problem statement: We have a number "13246598" where even, odd digits are jumbled. We need to print all even digits first followed by all even digits.

Output23145912

There may be multiple ways of doing it but I am doing it with java8 features. The way, I am trying to solve it will include some basic conversions which can help in any other such type of problems as well.

Steps:

1. Convert this number to an Int array.

2. Create an integer list from that integer array.

3. Use the filter feature on that ArrayList each item to segregate even digits. Store them in an integer list.

4. Use the filter feature on that ArrayList each item to segregate odd digits. Store them in a new integer list.

5. Add all second list items to the first list

6. Convert this integer ArrayList to String and later that string to int Number.

7. print them


Input: Integer number =23145912;


//int number to intArray

1. int[] numberAsTab = String.valueOf(number).chars().map(Character::getNumericValue).toArray();

//int array to Integer list

2. List<Integer> al = Arrays.stream(numberAsTab).boxed().collect(Collectors.toList());

// Filter in integer list

3. List<Integer> al1 = al.stream().filter(i-> i%2 ==0).collect(Collectors.toList());

4. List<Integer> al2 = al.stream().filter(i-> i%2 !=0).collect(Collectors.toList());

5. al1.addAll(al2);

6. int number1 = Integer.parseInt(al1.stream().map(String::valueOf).collect(Collectors.joining()));

7. System.out.println(number1 );


Output: 24231591

So if we understood this solution, we can easily solve one more problem of printing all 0's and all 1's together from a given value.

Input: 10101110

Output: 00011111

We just need to change our filter condition with;

i-> i ==0 

i-> i ==1

For this solution, don't convert the list of integers to int as we did above. Just try to print the list.

Why? For that, just do it and see the difference. :)


Friday, 5 February 2021

February 05, 2021

How to connect winscp with apache commons library

 A true SDET is, who automates everything which can be automated comes in his way. :)  

Recently, I came across a scenario where the Team is using extensively WinSCP for the movement of files from local machine to remote or vice versa for testing purposes. I gone with each step and identified that these steps (in a process to automate everything) which we are doing for ages manually :) can be automated. s this involves, WinSCP, so my journey started with the google search, how we can connect WinSCP with java. I googled some posts and found, that we can use Apache common VFS for this purpose. There are other ways too, but I am a big fan of Apache utilities from the past.




So I added the following jar in my POC project:

<dependency>

    <groupId>org.apache.commons</groupId>

    <artifactId>commons-vfs2</artifactId>

    <version>2.4</version>

</dependency>

<dependency>

    <groupId>com.jcraft</groupId>

    <artifactId>jsch</artifactId>

    <version>0.1.55</version>

</dependency>

And started working towards it.

My use case was :

1. Navigate to a directory

2. Find the latest file with an extension of. Dat

3. Copy it to your local directory

----------------------------------------------------------

4. Edit this .dat file

5. Move this updated file to the remote machine again

6. Hit a job

7. After the job completes, do the validations in DB as per the new entries inserted.


Note: This article is till step 3 only.


        String hostName = "10.164.123.29";

        String username = "developers";

        String password = "E2LQA2@ws";

        String localFilePath = "C:\\FakePath\\TestFile.DAT";

        String remoteFilePath = "/ABS/CDE/AllFiles/ADD/ArchiveFiles/";

You need to fill in your hostname, username and password details along with localFilePath where you will copy that file, and remoteFilePath from where you will copy it.

   existAndDownload(hostName, username, password,localFilePath, remoteFilePath);



import java.io.File;

import java.net.URLEncoder;

import java.util.Arrays;

import java.util.Collections;

import java.util.Vector;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import org.apache.commons.vfs2.FileObject;

import org.apache.commons.vfs2.FileSystemException;

import org.apache.commons.vfs2.FileSystemOptions;

import org.apache.commons.vfs2.Selectors;

import org.apache.commons.vfs2.impl.StandardFileSystemManager;

import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

 // This method first check the file present in the directory and then copy it to your local machine

public static void existAndDownload(String hostName, String username, String password,String localFilePath, String remoteFilePath) {

        StandardFileSystemManager manager = new StandardFileSystemManager();

        FileObject remoteFile = null;

        String remoteFilePathForlatestFile = null;


        try {

            manager.init();

            // Create local file object. Change location if necessary for new downloadFilePath

            FileObject localFile = manager.resolveFile(localFilePath);

            // List all the files in that directory.Try to give the directory path

            FileObject remoteFileObject = manager.resolveFile(createConnectionString(hostName, username, URLEncoder.encode(password,"UTF-8"), remoteFilePath), createDefaultOptions());

            FileObject[] children = remoteFileObject.getChildren();

            Arrays.sort(children, Collections.reverseOrder());

            for ( int i = 0; i < children.length; i++ ){

                System.out.println( children[ i ].getName().getBaseName() );

                if(children[ i ].getName().getBaseName().startsWith("2021")){

                    System.out.println(children[ i ].getName().getBaseName());

                    remoteFilePathForlatestFile =  remoteFilePath  + children[ i ].getName().getBaseName() ;

                    break;

                }

            }

            remoteFile = manager.resolveFile(createConnectionString(hostName, username, URLEncoder.encode(password,"UTF-8"),remoteFilePathForlatestFile ), createDefaultOptions());

            // Copy local file to sftp server

            localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);

            System.out.println("File download success");

        } catch (Exception e) {

            throw new RuntimeException(e);

        } finally {

            manager.close();

        }

    }


This is the first part of that activity. Later, I need to edit this file and place in the same location.

I will share with you, once I will complete that activity.


Issues Encountered: If you are using a password with special characters in it, then you need to Unicode it before using it. Otherwise, you will get an error like this;

Could not find a file with URI "file path" because it is a relative path, and no base URI was provided.

Fix: URLEncoder.encode(password,"UTF-8")


Till then, Happy Learning.