The new Programmer (Artificial Intelligence) Part 2

TechGandalf
3 min readFeb 5, 2023

--

In the previous story, I demonstrated a use case utilizing ChatGPT for creating a REST service. Imagine if we could use ChatGPT to automate software testing. That would be fantastic, wouldn’t it?

Let’s start by understanding the basics. Software testing is a process that involves various testing techniques, such as unit testing, integration testing, user acceptance testing, and more. In recent years, there has been a shift towards earlier testing, largely due to the rise of DevOps (shift left).

Shift-Left Testing Principles

To speed up the software development life cycle (SDLC), let’s consider automating the creation of unit tests. It is believed that a higher percentage of code coverage in unit testing will result in fewer defects..

Code Coverage Percentage = (No. of lines of code executed by testing algorithm / Total no. of lines of code in a system component) * 100

The idea is that we develop during the day and the robot creates the unit tests during the night :)

This can be viewed like this:

I tested and the result was:

Source Code:

package com.xxxx;

import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl;

public class WSLogin {

public static boolean authenticateUser(String user, String pass) {
LoginContext lc = createLoginContext(user, pass);

if (lc != null) {
try {
lc.login(); // perform login

// get the authenticated subject
lc.getSubject();

return true;
} catch (LoginException e) {
System.err.println("ERROR: login failed with exception: " + e.getMessage());
e.printStackTrace();

// login failed, might want to provide relogin logic
}
}

return false;
}

private static LoginContext createLoginContext(String user, String pass) {
LoginContext lc = null;

try {
lc = new LoginContext("WSLogin", new WSCallbackHandlerImpl(user, pass));

// create a LoginContext and specify a CallbackHandler
// implementation
// CallbackHandler implementation determine how authentication data
// is collected
// in this case, the authentication data is "pushed" to the
// authentication
// mechanism implemented by the LoginModule.
} catch (LoginException e) {
System.err.println("ERROR: failed to instantiate a LoginContext " + "and the exception: " + e.getMessage());
e.printStackTrace();

// may be javax.security.auth.AuthPermission "createLoginContext" is
// not granted
// to the application, or the JAAS login configuration is not
// defined.
}
return lc;
}

}

The Junit Test is:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import org.junit.Before;
import org.junit.Test;

import com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl;
import com.xxxx.WSLogin;

public class WSLoginTest {
LoginContext lc = null;

@Before
public void setUp() {
try {
lc = new LoginContext("WSLogin", new WSCallbackHandlerImpl("test", "test"));
} catch (LoginException e) {
e.printStackTrace();
fail ("LoginContext could not be created");
}
}

@Test
public void testAuthenticateUser() {
assertEquals(true, WSLogin.authenticateUser("test", "test"));
}
}

This is another use case where we can utilize this “new Programmer” in software development. I hope you like the idea.

--

--

TechGandalf

Focused IT professional always looking for, proposing and embodying new and innovative technological solutions