Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deepcloudlabs committed Jun 23, 2020
1 parent 84846c6 commit c460305
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 20 deletions.
41 changes: 38 additions & 3 deletions hr-domain/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,44 @@
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="test">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
6 changes: 6 additions & 0 deletions hr-domain/.project
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions hr-domain/.settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
2 changes: 2 additions & 0 deletions hr-domain/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
4 changes: 4 additions & 0 deletions hr-domain/.settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
35 changes: 35 additions & 0 deletions hr-domain/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hr-domain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public interface EmployeeApplication {

void hireEmployee(Employee employee);
boolean hireEmployee(Employee employee);

void fireEmployee(Employee employee);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public void setEventPushlisher(EventPushlisher eventPushlisher) {
}

@Override
public void hireEmployee(Employee employee) {
public boolean hireEmployee(Employee employee) {
employeeRepository.save(employee);
eventPushlisher.publishEvent(new EmployeeHiredEvent("", "employees", employee));
return true;
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions hr-domain/target/classes/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Manifest-Version: 1.0
Built-By: dcl
Build-Jdk: 11.0.7
Created-By: Maven Integration for Eclipse

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Generated by Maven Integration for Eclipse
#Tue Jun 23 17:09:21 EET 2020
m2e.projectLocation=C\:\\DEVEL\\stage\\tmp\\dcl350\\hr-domain
m2e.projectName=hr-domain
groupId=com.example
artifactId=hr-domain
version=0.0.1-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hr-domain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
5 changes: 5 additions & 0 deletions hr-domain/target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Generated by Maven
#Tue Jun 23 17:09:29 EET 2020
groupId=com.example
artifactId=hr-domain
version=0.0.1-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\BirthYear.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\infrastructure\EventPushlisher.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\events\EmployeeHiredEvent.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\application\business\SimpleEmployeeApplication.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\TcKimlikNoRecord.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\events\BusinessEvent.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\FullName.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\Iban.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\application\EmployeeApplication.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\Department.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\TcKimlikNo.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\MoneyCurrency.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\repository\EmployeeRepository.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\Employee.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\Photo.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\domain\Money.java
C:\DEVEL\stage\tmp\dcl350\hr-domain\src\com\example\hr\events\EmployeeFiredEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:\DEVEL\stage\tmp\dcl350\hr-domain\test\com\example\hr\application\business\SimpleEmployeeApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.hr.application.business;

import static org.junit.Assert.assertTrue;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.example.hr.domain.Department;
import com.example.hr.domain.Employee;
import com.example.hr.domain.MoneyCurrency;
import com.example.hr.domain.TcKimlikNo;
import com.example.hr.events.EmployeeHiredEvent;
import com.example.hr.infrastructure.EventPushlisher;
import com.example.hr.repository.EmployeeRepository;

class SimpleEmployeeApplicationTest {

// Unit Testing -> Test Doubles
// 1. Dummy Object
// 2. Stub Object -> Partial Implementation -> Real Object
// 3. Fake Object -> Simple/Full Implementation (HashMap - in-memory implementation)
// 4. Mock Object -> Mocking/Spying Library -> Mockito
// 5. Spy Object
@Test
void hireEmployee_success() {
// 1. test setup / fixture
SimpleEmployeeApplication app = new SimpleEmployeeApplication();
EmployeeRepository empRepo = Mockito.mock(EmployeeRepository.class);
EventPushlisher eventPublisher = Mockito.mock(EventPushlisher.class);
app.setEmployeeRepository(empRepo);
app.setEventPushlisher(eventPublisher);
System.out.println(empRepo.getClass());
Employee jack = new Employee.Builder("40310900232")
.fullname("jack", "bauer")
.birthYear(1956)
.salary(100_000, MoneyCurrency.USD)
.iban("TR820006235455781668847562")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OriCNSTYd+BD51BeTGwP/5iKJ/wL+zNdRU9COwQVGUwN7uQ4NoOvLVmFxqUo+dkPpqTBjrRGow/53H1Wi6bzG8/2PK63rcOX+I/fmSLYDhDwbendMvcsIBuRMDlD6sfUztppKvMgUMnf/Mv8NIVU+cyYl6AAAAAElFTkSuQmCC".getBytes())
.build();
Mockito.doNothing().when(empRepo).save(jack);
Mockito.doNothing().when(eventPublisher).publishEvent(new EmployeeHiredEvent("", "employees", jack) );
// 2. call exercise method
var success = app.hireEmployee(jack);
// 3. Verification
assertTrue(success);
// 4. destroy setup
}

}
18 changes: 10 additions & 8 deletions hr-microservice-hexagonal/pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>hr-microservice-hexagonal</artifactId>
Expand All @@ -19,6 +20,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>hr-domain</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down Expand Up @@ -63,12 +69,8 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
<!-- <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId>
</exclusion> </exclusions> -->
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

import org.hibernate.annotations.DynamicUpdate;

import com.example.hr.domain.Department;
import com.example.validation.Iban;
import com.example.validation.TcKimlikNo;

@Entity
@Table(name = "employees")
@DynamicUpdate
public class EmployeeEntity {
@Id
@Column(name = "identity")
@TcKimlikNo
private String identity;
@Size(min=5)
@Size(min = 5)
private String fullname;
@Min(3_000)
private double salary;
//@Column(columnDefinition = "float default 1000")
private Double salary;
@Iban
private String iban;
//@AssertTrue
private boolean fulltime;
// @AssertTrue
private Boolean fulltime;
@Max(2002)
private int birthYear;
@Lob
Expand Down
Loading

0 comments on commit c460305

Please sign in to comment.