Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface ContentRepository extends CrudRepository<cat.udl.eps.softarch.d

boolean existsByName(String name);



//List<Content> findByProjectId(Long projectId);

//List<Content> findByProjectIdAndVisibility(Long projectId, Visibility visibility);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Content;
import cat.udl.eps.softarch.demo.repository.ContentRepository;
import io.cucumber.java.en.*;
import org.junit.jupiter.api.Assertions;

public class ContentRepositoryStepsDefs {

private final ContentRepository contentRepository;
private boolean existsResult;
private Long createdContentId;

public ContentRepositoryStepsDefs(ContentRepository contentRepository) {
this.contentRepository = contentRepository;
}

@Given("there are no Contents in the system")
public void there_are_no_Contents_in_the_system() {
contentRepository.deleteAll();
Assertions.assertEquals(0, contentRepository.count());
}

@When("I create a Content with name {string} and description {string}")
public void iCreateAContentWithNameAndDescription(String name, String description) {
if (!contentRepository.existsByName(name)) {
Content content = new Content();
content.setName(name);
content.setDescription(description);

Content saved = contentRepository.save(content);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the When steps, as you are testing the API to check if it can create Contents, you should be using the API instead of directly creating the intance into de database.

For an example: https://github.com/UdL-EPS-SoftArch/spring-template/blob/c846b30f32b9e1a5d9477616de14edf84e7ae737/src/test/java/cat/udl/eps/softarch/demo/steps/ManageRecordStepDefs.java#L33

createdContentId = saved.getContentId();
existsResult = contentRepository.existsById(createdContentId);
} else {
existsResult = true;
}
}

@Then("Content existsById should return true")
public void exists_by_id_should_return_true() {
Assertions.assertTrue(existsResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions.*;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Report;
import cat.udl.eps.softarch.demo.domain.Content;
import cat.udl.eps.softarch.demo.repository.ReportRepository;
import cat.udl.eps.softarch.demo.repository.ContentRepository;
import io.cucumber.java.ParameterType;
import io.cucumber.java.en.*;
import org.junit.jupiter.api.Assertions;

public class ReportRepositoryStepsDefs {

private final ReportRepository reportRepository;
private boolean existsResult;
private Long createdReportId;
private final ContentRepository contentRepository;

@ParameterType(".*") public Content content(String name) {
return this.contentRepository.findByName(name).orElseThrow(() -> new RuntimeException("Content not found"));
}

@ParameterType("\\d+") public Long Long(String value) {
return Long.valueOf(value);
}

public ReportRepositoryStepsDefs(ReportRepository reportRepository, ContentRepository contentRepository) {
this.reportRepository = reportRepository;
this.contentRepository = contentRepository;
}

@Given("there are no Reports in the system")
public void there_are_no_Reports_in_the_system() {
reportRepository.deleteAll();
Assertions.assertEquals(0, reportRepository.count());
}

@When("I create a Report with reportId {Long}, content {content} and reason {string}")
public void iCreateAReportWithNameAndDescription(Long reportId, Content content, String reason) {
Report report = new Report();
report.setContent(content);
report.setReason(reason);

Report saved = reportRepository.save(report);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the When steps, as you are testing the API to check if it can create reports, you should be using the API instead of directly creating the intance into de database.

createdReportId = saved.getReportId();
existsResult = reportRepository.existsById(createdReportId);
}

@Then("Report existsById should return true")
public void exists_by_id_should_return_true() {
Assertions.assertTrue(existsResult);
}
}