-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented filter for subjects (#129)
Written unit tests and e2e tests for the subject service and subject endpoint
- Loading branch information
1 parent
f3a0e10
commit 4f75ede
Showing
5 changed files
with
181 additions
and
4 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
4 changes: 3 additions & 1 deletion
4
server/src/main/java/de/uftos/repositories/database/SubjectRepository.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,12 +1,14 @@ | ||
package de.uftos.repositories.database; | ||
|
||
import de.uftos.entities.Subject; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
import org.springframework.data.repository.ListCrudRepository; | ||
import org.springframework.data.repository.ListPagingAndSortingRepository; | ||
|
||
/** | ||
* The repository for accessing the subject database table. | ||
*/ | ||
public interface SubjectRepository | ||
extends ListPagingAndSortingRepository<Subject, String>, ListCrudRepository<Subject, String> { | ||
extends ListPagingAndSortingRepository<Subject, String>, ListCrudRepository<Subject, String>, | ||
JpaSpecificationExecutor<Subject> { | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package de.uftos.e2e; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
import io.restassured.http.ContentType; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class SubjectsTest { | ||
|
||
private static final String SUBJECT1_NAME = "Subject 1"; | ||
private static final String SUBJECT2_NAME = "Subject 2"; | ||
|
||
static String subject1Id; | ||
static String subject2Id; | ||
|
||
@BeforeAll | ||
static void createTestStudents() throws JSONException { | ||
subject1Id = given().contentType(ContentType.JSON) | ||
.body(generateSubjectJson(SUBJECT1_NAME)) | ||
.when() | ||
.post("/subjects") | ||
.then() | ||
.statusCode(200) | ||
.body("id", notNullValue()) | ||
.body("name", equalTo(SUBJECT1_NAME)) | ||
.log().ifValidationFails() | ||
.extract() | ||
.body().jsonPath().getString("id"); | ||
|
||
subject2Id = given().contentType(ContentType.JSON) | ||
.body(generateSubjectJson(SUBJECT2_NAME)) | ||
.when() | ||
.post("/subjects") | ||
.then() | ||
.statusCode(200) | ||
.body("id", notNullValue()) | ||
.body("name", equalTo(SUBJECT2_NAME)) | ||
.log().ifValidationFails() | ||
.extract() | ||
.body().jsonPath().getString("id"); | ||
} | ||
|
||
@AfterAll | ||
static void deleteCreatedStudents() { | ||
given().contentType(ContentType.JSON) | ||
.when() | ||
.delete("/subjects/{id}", subject1Id) | ||
.then() | ||
.statusCode(200); | ||
|
||
given().contentType(ContentType.JSON) | ||
.when() | ||
.delete("/subjects/{id}", subject2Id) | ||
.then() | ||
.statusCode(200); | ||
} | ||
|
||
private static String generateSubjectJson(String name) | ||
throws JSONException { | ||
return new JSONObject() | ||
.put("name", name) | ||
.put("tags", Collections.emptyList()) | ||
.toString(); | ||
} | ||
|
||
private static String generatePageJson(int page, int size, List<String> sort) | ||
throws JSONException { | ||
return new JSONObject() | ||
.put("page", page) | ||
.put("size", size) | ||
.put("sort", sort) | ||
.toString(); | ||
} | ||
|
||
@Test | ||
void getAllSubjects() throws JSONException { | ||
given().contentType(ContentType.JSON) | ||
.body(generatePageJson(0, 10, Collections.emptyList())) | ||
.when() | ||
.get("/subjects") | ||
.then() | ||
.statusCode(200) | ||
.body("totalElements", equalTo(2)) | ||
.log().ifValidationFails(); | ||
} | ||
|
||
@Test | ||
void getSubjectsWithName() throws JSONException { | ||
given().contentType(ContentType.JSON) | ||
.body(generatePageJson(0, 10, Collections.emptyList())) | ||
.param("name", "ct 1") | ||
.when() | ||
.get("/subjects") | ||
.then() | ||
.statusCode(200) | ||
.body("totalElements", equalTo(1)) | ||
.body("content[0].id", equalTo(subject1Id)) | ||
.log().ifValidationFails(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
server/src/test/java/de/uftos/services/SubjectServiceTests.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,60 @@ | ||
package de.uftos.services; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.Mockito.when; | ||
|
||
import de.uftos.entities.Subject; | ||
import de.uftos.entities.Tag; | ||
import de.uftos.repositories.database.SubjectRepository; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
public class SubjectServiceTests { | ||
|
||
private static final String TAG_NAME = "test tag"; | ||
private static final String TAG_ID = "456"; | ||
private static final String SUBJECT_ID = "123"; | ||
private static final String SUBJECT_NAME = "English"; | ||
|
||
@Mock | ||
private SubjectRepository subjectRepository; | ||
|
||
@InjectMocks | ||
private SubjectService subjectService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
Tag tag = new Tag(); | ||
tag.setId(TAG_ID); | ||
tag.setName(TAG_NAME); | ||
|
||
Subject subject = new Subject(); | ||
subject.setId(SUBJECT_ID); | ||
subject.setName(SUBJECT_NAME); | ||
subject.setTags(List.of(tag)); | ||
|
||
when(subjectRepository.findAll()).thenReturn(List.of(subject)); | ||
when(subjectRepository.findById(SUBJECT_ID)).thenReturn(Optional.of(subject)); | ||
} | ||
|
||
@Test | ||
void lessonsById() { | ||
Subject result = subjectService.getById(SUBJECT_ID); | ||
assertNotNull(result); | ||
assertEquals(SUBJECT_ID, result.getId()); | ||
assertFalse(result.getTags().isEmpty()); | ||
assertEquals(TAG_ID, result.getTags().getFirst().getId()); | ||
} | ||
} |