-
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.
Merge pull request #101 from UdL-EPS-SoftArch/100-test-create-students
Test create students
- Loading branch information
Showing
4 changed files
with
152 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
5 changes: 5 additions & 0 deletions
5
src/main/java/cat/udl/eps/softarch/demo/repository/StudentRepository.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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package cat.udl.eps.softarch.demo.repository; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Property; | ||
import cat.udl.eps.softarch.demo.domain.Student; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface StudentRepository extends CrudRepository<Student, Long> { | ||
Student findByEmail(@Param("email") String email); | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/cat/udl/eps/softarch/demo/steps/CreateStudentStepDefs.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,74 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import cat.udl.eps.softarch.demo.domain.Student; | ||
import cat.udl.eps.softarch.demo.repository.StudentRepository; | ||
import io.cucumber.java.en.And; | ||
import io.cucumber.java.en.When; | ||
import org.json.JSONObject; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
public class CreateStudentStepDefs { | ||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
private StudentRepository studentRepository; | ||
|
||
@When("^I create a student with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\" and phoneNumber \"([^\"]*)\" and name \"([^\"]*)\"$") | ||
public void createStudent(String username, String password, String email, String phoneNumber, String name) throws Throwable { | ||
Student studentTest = new Student(); | ||
|
||
studentTest.setUsername(assignValueInput(username)); | ||
studentTest.setEmail(assignValueInput(email)); | ||
studentTest.setPhoneNumber(assignValueInput(phoneNumber)); | ||
studentTest.setName(assignValueInput(name)); | ||
|
||
|
||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/students") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(new JSONObject( | ||
stepDefs.mapper.writeValueAsString(studentTest) | ||
).put("password", assignValueInput(password)).toString()) | ||
.characterEncoding(StandardCharsets.UTF_8)) | ||
.andDo(print()); | ||
|
||
} | ||
|
||
private String assignValueInput(String value) { | ||
if(value.equals("null")){ | ||
return null; | ||
} | ||
else { | ||
return value; | ||
} | ||
} | ||
|
||
@And("^There is 0 Student created$") | ||
public void thereIs0StudentCreated() throws Throwable { | ||
assertEquals(studentRepository.count(), 0); | ||
} | ||
|
||
@And("^There is 1 Student created with username \"([^\"]*)\" and email \"([^\"]*)\" and phoneNumber \"([^\"]*)\" and name \"([^\"]*)\"$") | ||
public void thereIs1StudentCreated(String username, String email, String phoneNumber, String name) throws Throwable { | ||
assertEquals(studentRepository.count(), 1); | ||
Student studentToCheck = this.studentRepository.findByEmail(email); | ||
assertEquals(studentToCheck.getUsername(), username); | ||
assertEquals(studentToCheck.getName(), name); | ||
assertEquals(studentToCheck.getPhoneNumber(), phoneNumber); | ||
assertEquals(studentToCheck.getEmail(), email); | ||
} | ||
} |
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,72 @@ | ||
Feature: Create Student | ||
In order to look for properties as an student | ||
I want to create an student profile | ||
|
||
|
||
Scenario: Create a Student with valid data | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "password" and email "[email protected]" and phoneNumber "123123123" and name "Student" | ||
Then The response code is 201 | ||
And There is 1 Student created with username "student" and email "[email protected]" and phoneNumber "123123123" and name "Student" | ||
|
||
|
||
Scenario: Create a student with a blank email: | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "password" and email "" and phoneNumber "123123123" and name "Student" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
Scenario: Create a student with a blank password: | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "" and email "[email protected]" and phoneNumber "123123123" and name "Student" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
Scenario: Create a student with an empty phoneNumber: | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "password" and email "[email protected]" and phoneNumber "" and name "Student" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
Scenario: Create a student with an empty name: | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "password" and email "[email protected]" and phoneNumber "123123123" and name "" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
Scenario: Create a student with an empty username: | ||
Given I'm not logged in | ||
When I create a student with username "" and password "password" and email "[email protected]" and phoneNumber "123123123" and name "" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
Scenario: Create a student with a null email: | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "password" and email "null" and phoneNumber "123123123" and name "Student" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
Scenario: Create a student with a null password: | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "null" and email "[email protected]" and phoneNumber "123123123" and name "Student" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
Scenario: Create a student with an null phoneNumber: | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "password" and email "[email protected]" and phoneNumber "null" and name "Student" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
Scenario: Create a student with an null name: | ||
Given I'm not logged in | ||
When I create a student with username "student" and password "password" and email "[email protected]" and phoneNumber "123123123" and name "null" | ||
Then The response code is 400 | ||
And There is 0 Student created | ||
|
||
|
||
Scenario: Create a student with a null username: | ||
Given I'm not logged in | ||
When I create a student with username "null" and password "password" and email "[email protected]" and phoneNumber "123123123" and name "" | ||
Then The response code is 400 | ||
And There is 0 Student created |