Skip to content

Commit

Permalink
feat(Login): Added login command to check for valid credentials (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndickerson authored Jan 27, 2019
1 parent 4b56f2b commit e7605b5
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/bullhorn/dataloader/enums/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum Command {
HELP("help"),
LOAD("load"),
LOAD_ATTACHMENTS("loadAttachments"),
LOGIN("login"),
TEMPLATE("template");

private final String methodName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public Action getAction(Command command) {
} else if (command.equals(Command.LOAD_ATTACHMENTS)) {
action = new LoadAttachmentsService(printUtil, propertyFileUtil, validationUtil, completeUtil,
restSession, processRunner, inputStream, timer);
} else if (command.equals(Command.LOGIN)) {
action = new LoginService(restSession, printUtil);
} else if (command.equals(Command.TEMPLATE)) {
action = new TemplateService(printUtil, propertyFileUtil, validationUtil, completeUtil,
restSession, processRunner, inputStream, timer);
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/bullhorn/dataloader/service/LoginService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.bullhorn.dataloader.service;

import com.bullhorn.dataloader.rest.RestSession;
import com.bullhorn.dataloader.util.PrintUtil;

/**
* Test user provided credentials to see if login is successful
*/
public class LoginService implements Action {

private final RestSession restSession;
private final PrintUtil printUtil;

LoginService(RestSession restSession, PrintUtil printUtil) {
this.restSession = restSession;
this.printUtil = printUtil;
}

@Override
public void run(String[] args) {
try {
restSession.getRestApi();
printUtil.printAndLog("Login Successful");
} catch (Exception e) {
printUtil.printAndLog("Login Failed");
}
}

@Override
public boolean isValidArguments(String[] args) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,44 @@ public void setup() {
}

@Test
public void testGetActionHelp() {
Class expectedResult = HelpService.class;
Action actualResult = actionFactory.getAction(Command.HELP);
public void testGetActionConvertAttachments() {
Class expectedResult = ConvertAttachmentsService.class;
Action actualResult = actionFactory.getAction(Command.CONVERT_ATTACHMENTS);
Assert.assertThat(actualResult.getClass(), new ReflectionEquals(expectedResult));
}

@Test
public void testGetActionTemplate() {
Class expectedResult = TemplateService.class;
Action actualResult = actionFactory.getAction(Command.TEMPLATE);
public void testGetActionDelete() {
Class expectedResult = DeleteService.class;
Action actualResult = actionFactory.getAction(Command.DELETE);
Assert.assertThat(actualResult.getClass(), new ReflectionEquals(expectedResult));
}

@Test
public void testGetActionConvertAttachments() {
Class expectedResult = ConvertAttachmentsService.class;
Action actualResult = actionFactory.getAction(Command.CONVERT_ATTACHMENTS);
public void testGetActionDeleteAttachments() {
Class expectedResult = DeleteAttachmentsService.class;
Action actualResult = actionFactory.getAction(Command.DELETE_ATTACHMENTS);
Assert.assertThat(actualResult.getClass(), new ReflectionEquals(expectedResult));
}

@Test
public void testGetActionLoad() {
Class expectedResult = LoadService.class;
Action actualResult = actionFactory.getAction(Command.LOAD);
public void testGetActionExport() {
Class expectedResult = ExportService.class;
Action actualResult = actionFactory.getAction(Command.EXPORT);
Assert.assertThat(actualResult.getClass(), new ReflectionEquals(expectedResult));
}

@Test
public void testGetActionExport() {
Class expectedResult = ExportService.class;
Action actualResult = actionFactory.getAction(Command.EXPORT);
public void testGetActionHelp() {
Class expectedResult = HelpService.class;
Action actualResult = actionFactory.getAction(Command.HELP);
Assert.assertThat(actualResult.getClass(), new ReflectionEquals(expectedResult));
}

@Test
public void testGetActionDelete() {
Class expectedResult = DeleteService.class;
Action actualResult = actionFactory.getAction(Command.DELETE);
public void testGetActionLoad() {
Class expectedResult = LoadService.class;
Action actualResult = actionFactory.getAction(Command.LOAD);
Assert.assertThat(actualResult.getClass(), new ReflectionEquals(expectedResult));
}

Expand All @@ -84,9 +84,16 @@ public void testGetActionLoadAttachments() {
}

@Test
public void testGetActionDeleteAttachments() {
Class expectedResult = DeleteAttachmentsService.class;
Action actualResult = actionFactory.getAction(Command.DELETE_ATTACHMENTS);
public void testGetActionLogin() {
Class expectedResult = LoginService.class;
Action actualResult = actionFactory.getAction(Command.LOGIN);
Assert.assertThat(actualResult.getClass(), new ReflectionEquals(expectedResult));
}

@Test
public void testGetActionTemplate() {
Class expectedResult = TemplateService.class;
Action actualResult = actionFactory.getAction(Command.TEMPLATE);
Assert.assertThat(actualResult.getClass(), new ReflectionEquals(expectedResult));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ public void testIsValidArguments() {

Assert.assertTrue(result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.bullhorn.dataloader.service;

import com.bullhorn.dataloader.enums.Command;
import com.bullhorn.dataloader.rest.RestSession;
import com.bullhorn.dataloader.util.PrintUtil;
import com.bullhornsdk.data.exception.RestApiException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class LoginServiceTest {

private RestSession restSessionMock;
private PrintUtil printUtilMock;
private LoginService loginService;

@Before
public void setup() {
restSessionMock = mock(RestSession.class);
printUtilMock = mock(PrintUtil.class);
loginService = new LoginService(restSessionMock, printUtilMock);
}

@Test
public void testRunLoginSuccessful() {
final String[] testArgs = {Command.LOGIN.getMethodName()};

loginService.run(testArgs);

verify(printUtilMock, times(1)).printAndLog(eq("Login Successful"));
}

@Test
public void testRunLoginFailed() {
final String[] testArgs = {Command.LOGIN.getMethodName()};
when(restSessionMock.getRestApi()).thenThrow(new RestApiException("fail"));

loginService.run(testArgs);

verify(printUtilMock, times(1)).printAndLog(eq("Login Failed"));
}


@Test
public void testIsValidArguments() {
final String[] testArgs = {Command.LOGIN.getMethodName()};

boolean result = loginService.isValidArguments(testArgs);

Assert.assertTrue(result);
}
}

0 comments on commit e7605b5

Please sign in to comment.