Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
raduionut90 committed Mar 24, 2020
1 parent 5635fcd commit 0eade48
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface EmployeeRepository extends MongoRepository<Employee, Integer>{

List<Employee> findByDepartmentIs(String department);
List<Employee> findByDepartmentIsAndSalaryIs(String department, double salary);
Employee findByFirstNameIs(String firstName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class EmployeeAppApplicationTests {



@Test
void contextLoads() {


}


}
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);
}
}
}

0 comments on commit 0eade48

Please sign in to comment.