Skip to content

Commit

Permalink
Merge pull request #21 from Wabri/issue-10
Browse files Browse the repository at this point in the history
Issue 10
  • Loading branch information
LorenzoBettini committed Nov 29, 2017
2 parents 61553ba + bd65c35 commit 5092eb3
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 3 deletions.
Empty file.
46 changes: 46 additions & 0 deletions exercise-app/src/main/java/org/exercise/app/AppController.java
@@ -0,0 +1,46 @@
package org.exercise.app;

import java.io.PrintStream;

import com.examples.attsw.exercise.core.controller.IEmployeeController;

public class AppController {

public static final String SHOW_ONE = "showOne";
public static final String SHOW_ALL = "showAll";
private IEmployeeController employeeController;

public AppController(IEmployeeController employeeController) {
this.employeeController = employeeController;
}

public void performAction(String actionCode, String arg, PrintStream out) throws IllegalArgumentException {
switch (actionCode) {
case SHOW_ALL:
caseShowAll(out);
return;
case SHOW_ONE:
caseShowOne(arg, out);
return;
default:
throw new IllegalArgumentException();
}
}

private void caseShowOne(String arg, PrintStream out) {
String employeeById = employeeController.getEmployeeById(arg);
if (employeeById.equals("")) {
out.print("There is no Employee with this id");
}
out.print(employeeById);
}

private void caseShowAll(PrintStream out) {
if (!employeeController.getAllEmployees().equals("")) {
out.print(employeeController.getAllEmployees());
} else {
out.print("There is no Employees");
}
}

}
99 changes: 99 additions & 0 deletions exercise-app/src/test/java/org/exercise/app/AppControllerTest.java
@@ -0,0 +1,99 @@
package org.exercise.app;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.examples.attsw.exercise.core.controller.IEmployeeController;
import com.examples.attsw.exercise.core.model.Employee;

public class AppControllerTest {

@InjectMocks
private AppController appController;
@Mock
private IEmployeeController employeeController;
private String allEmployees;

@Before
public void setUp() throws Exception {
allEmployees = "";
MockitoAnnotations.initMocks(this);
}

@Test
public void testShowAllWhenThereAreNoEmployees() {
assertShowAll("There is no Employees");
}

@Test
public void testShowAllWhenThereIsOneEmployee() {
allEmployees = concatNewEmployee(createNewEmployee("1", "name"));
assertShowAll(allEmployees);
}

@Test
public void testShowAllWhenThereAreTwoEmployees() {
allEmployees = concatNewEmployee(createNewEmployee("1", "name1")
.concat(concatNewEmployee(createNewEmployee("2", "name2"))));
assertShowAll(allEmployees);
}

@Test
public void testShowOneWhenThereAreNoEmployees() {
assertShowOne("There is no Employee with this id", "1", "");
}

@Test
public void testShowOneWheneEmployeeDoesNotExists() {
allEmployees = concatNewEmployee(createNewEmployee("1", "name1"));
assertShowOne("There is no Employee with this id", "2", "");
}

@Test
public void testShowOneWhenEmployeeExists() {
String employee = createNewEmployee("1", "name");
allEmployees = concatNewEmployee(employee);
assertShowOne(employee, "1", employee);
}

@Test(expected = IllegalArgumentException.class)
public void testOtherStringInActionCodeArgument() {
assertShowWhat("", "", "");
}

private String concatNewEmployee(String employee) {
return employee.concat(System.getProperty("line.separator"));
}

private String createNewEmployee(String id, String name) {
return new Employee(id, name).toString();
}

private void assertShowWhat(String expected, String arg, String show) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream out = new PrintStream(baos);
appController.performAction(show, arg, out);
assertEquals(expected, new String(baos.toByteArray(), StandardCharsets.UTF_8));
}

private void assertShowAll(String expected) {
when(employeeController.getAllEmployees()).thenReturn(allEmployees);
assertShowWhat(expected, "", AppController.SHOW_ALL);
}

private void assertShowOne(String expected, String id, String stringToReturn) {
when(employeeController.getEmployeeById(id)).thenReturn(stringToReturn);
assertShowWhat(expected, id, AppController.SHOW_ONE);
}

}
Expand Up @@ -4,7 +4,7 @@

import com.examples.attsw.exercise.core.service.IEmployeeService;

public class EmployeeController {
public class EmployeeController implements IEmployeeController{

private IEmployeeService employeeService;

Expand All @@ -20,7 +20,7 @@ public String getAllEmployees() {
}

public String getEmployeeById(String id) {
return employeeService.getEmployeeById(id).getName();
return employeeService.getEmployeeById(id).toString();
}

}
@@ -0,0 +1,9 @@
package com.examples.attsw.exercise.core.controller;

public interface IEmployeeController {

String getAllEmployees();

String getEmployeeById(String id);

}
Expand Up @@ -58,7 +58,7 @@ public void testGetEmployeeByIdWhenEmployeeDoesntExists() {
public void testGetEmployeeByIdWhenEmployeeExists() {
Employee newEmployee = newEmployee("1", "nameTest");
when(employeeService.getEmployeeById("1")).thenReturn(newEmployee);
assertEquals(newEmployee.getName(), employeeController.getEmployeeById("1"));
assertEquals(newEmployee.toString(), employeeController.getEmployeeById("1"));
}

private String extractAllEmployeesStringFromList(List<Employee> allEmployees) {
Expand Down
6 changes: 6 additions & 0 deletions exercise-parent/pom.xml
Expand Up @@ -24,6 +24,12 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down

0 comments on commit 5092eb3

Please sign in to comment.