Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Wabri/issue-10
Issue 10
- Loading branch information
Showing
7 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
46 changes: 46 additions & 0 deletions
46
exercise-app/src/main/java/org/exercise/app/AppController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
99
exercise-app/src/test/java/org/exercise/app/AppControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...e-core/src/main/java/com/examples/attsw/exercise/core/controller/IEmployeeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.examples.attsw.exercise.core.controller; | ||
|
||
public interface IEmployeeController { | ||
|
||
String getAllEmployees(); | ||
|
||
String getEmployeeById(String id); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters