-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5635fcd
commit 0eade48
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
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
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
97 changes: 97 additions & 0 deletions
97
src/test/java/com/ionutradu/mongodb/employeeapp/tests/EmployeeControllerApplicationTest.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,97 @@ | ||
package com.ionutradu.mongodb.employeeapp.tests; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.ionutradu.mongodb.employeeapp.documents.Employee; | ||
import com.ionutradu.mongodb.employeeapp.services.IdGeneratorService; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
|
||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class EmployeeControllerApplicationTest { | ||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
|
||
@Autowired | ||
IdGeneratorService idGeneratorService; | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
private String getRootUrl() { | ||
return "http://localhost:" + port; | ||
} | ||
|
||
@Test | ||
public void contextLoads() { | ||
|
||
} | ||
|
||
@Test | ||
public void testGetAllEmployees() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
HttpEntity<String> entity = new HttpEntity<String>(null, headers); | ||
ResponseEntity<String> response = restTemplate.exchange(getRootUrl() + "/employees", | ||
HttpMethod.GET, entity, String.class); | ||
assertNotNull(response.getBody()); | ||
} | ||
|
||
@Test | ||
public void testGetEmployeeById() { | ||
Employee employee = restTemplate.getForObject(getRootUrl() + "/employees/1", Employee.class); | ||
System.out.println(employee.getFirstName()); | ||
assertNotNull(employee); | ||
} | ||
|
||
@Test | ||
public void testCreateEmployee() { | ||
Employee employee = new Employee(); | ||
employee.setId(idGeneratorService.generateSequence(Employee.SEQUENCE_NAME)); | ||
employee.setDepartment("[email protected]"); | ||
employee.setFirstName("admin"); | ||
employee.setLastName("admin"); | ||
ResponseEntity<Employee> postResponse = restTemplate.postForEntity(getRootUrl() + "/api/employees", employee, Employee.class); | ||
assertNotNull(postResponse); | ||
assertNotNull(postResponse.getBody()); | ||
} | ||
|
||
@Test | ||
public void testUpdateEmployee() { | ||
int id = 1; | ||
Employee employee = restTemplate.getForObject(getRootUrl() + "/api/employees/" + id, Employee.class); | ||
employee.setFirstName("admin1"); | ||
employee.setLastName("admin2"); | ||
restTemplate.put(getRootUrl() + "/employees/" + id, employee); | ||
Employee updatedEmployee = restTemplate.getForObject(getRootUrl() + "/api/employees/" + id, Employee.class); | ||
assertNotNull(updatedEmployee); | ||
} | ||
|
||
@Test | ||
public void testDeleteEmployee() { | ||
int id = 2; | ||
Employee employee = restTemplate.getForObject(getRootUrl() + "/api/employees/" + id, Employee.class); | ||
assertNotNull(employee); | ||
restTemplate.delete(getRootUrl() + "/api/employees/" + id); | ||
try { | ||
employee = restTemplate.getForObject(getRootUrl() + "/api/employees/" + id, Employee.class); | ||
} catch (final HttpClientErrorException e) { | ||
assertEquals(e.getStatusCode(), HttpStatus.NOT_FOUND); | ||
} | ||
} | ||
} |