Skip to content

Commit

Permalink
Implemented filter for subjects (#129)
Browse files Browse the repository at this point in the history
Written unit tests and e2e tests for the subject service and subject endpoint
  • Loading branch information
SuperMarcomen authored Jul 14, 2024
1 parent f3a0e10 commit 4f75ede
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 4 deletions.
3 changes: 2 additions & 1 deletion server/src/main/java/de/uftos/dto/SubjectRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.uftos.entities.Subject;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -19,6 +20,6 @@ public record SubjectRequestDto(@NotEmpty String name, String color, @NotNull Li
* @return the new subject entity.
*/
public Subject map() {
return new Subject(this.name, this.color, this.tagIds);
return new Subject(this.name, this.color, this.tagIds == null ? Collections.emptyList() : this.tagIds);
}
}
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> {
}
10 changes: 8 additions & 2 deletions server/src/main/java/de/uftos/services/SubjectService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package de.uftos.services;

import de.uftos.builders.SpecificationBuilder;
import de.uftos.dto.SubjectRequestDto;
import de.uftos.entities.Subject;
import de.uftos.repositories.database.SubjectRepository;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
Expand All @@ -32,11 +34,15 @@ public SubjectService(SubjectRepository repository) {
* Gets a page of entries of the subject table.
*
* @param pageable contains the parameters for the page.
* @param name the name filter.
* @param name the name filter.
* @return the page of the entries fitting the parameters.
*/
public Page<Subject> get(Pageable pageable, Optional<String> name) {
return this.repository.findAll(pageable);
Specification<Subject> specification = new SpecificationBuilder<Subject>()
.optionalOrEquals(name, "name")
.build();

return this.repository.findAll(specification, pageable);
}

/**
Expand Down
108 changes: 108 additions & 0 deletions server/src/test/java/de/uftos/e2e/SubjectsTest.java
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 server/src/test/java/de/uftos/services/SubjectServiceTests.java
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());
}
}

0 comments on commit 4f75ede

Please sign in to comment.