Skip to content

Commit b9c7dd7

Browse files
authored
Merge pull request #9 from UdL-EPS-SoftArch/Content&ReportTest
Content&report test
2 parents ad74506 + 3341442 commit b9c7dd7

9 files changed

Lines changed: 200 additions & 11 deletions

File tree

src/main/java/cat/udl/eps/softarch/demo/domain/Content.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import jakarta.persistence.GeneratedValue;
1212
import jakarta.persistence.GenerationType;
1313
import jakarta.persistence.Id;
14-
//import jakarta.persistence.JoinColumn;
15-
//import jakarta.persistence.ManyToOne;
14+
import jakarta.persistence.JoinColumn;
15+
import jakarta.persistence.ManyToOne;
1616
import jakarta.persistence.OneToMany;
1717
import lombok.Data;
18-
//import lombok.EqualsAndHashCode;
19-
/*
18+
import lombok.EqualsAndHashCode;
19+
2020
import jakarta.persistence.ManyToOne;
21-
import com.fasterxml.jackson.annotation.JsonIdentityReference; */
21+
import com.fasterxml.jackson.annotation.JsonIdentityReference;
2222
import java.util.List;
2323

2424

@@ -40,10 +40,6 @@ public class Content {
4040
@JsonIdentityReference(alwaysAsId = true)
4141
@Column(nullable = false)
4242
private User user;*/
43-
44-
@OneToMany(mappedBy = "content")
45-
@JsonIdentityReference(alwaysAsId = true)
46-
private List<Report> reports;
4743

4844
@Column(unique = true, nullable = false)
4945
private String name;

src/main/java/cat/udl/eps/softarch/demo/domain/Report.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class Report {
3434

3535
@ManyToOne
3636
@JoinColumn(name = "content_id", nullable = false)
37-
private Content content;
37+
private Content content;
3838

3939
@org.hibernate.validator.constraints.Length(max = 100)
4040
@Column(length = 100)

src/main/java/cat/udl/eps/softarch/demo/repository/ContentRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
88
import java.util.Optional;
99

10+
import java.util.List;
11+
1012

1113
//import java.util.List;
1214

@@ -16,6 +18,8 @@ public interface ContentRepository extends CrudRepository<cat.udl.eps.softarch.d
1618

1719
boolean existsByName(String name);
1820

21+
List<Content> findAll();
22+
1923
//List<Content> findByProjectId(Long projectId);
2024

2125
//List<Content> findByProjectIdAndVisibility(Long projectId, Visibility visibility);

src/test/java/cat/udl/eps/softarch/demo/CucumberTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.cucumber.junit.platform.engine.Cucumber;
44

5+
56
@Cucumber
67
public class CucumberTest {
78

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package cat.udl.eps.softarch.demo.steps;
2+
3+
import cat.udl.eps.softarch.demo.domain.Content;
4+
import cat.udl.eps.softarch.demo.repository.ContentRepository;
5+
import io.cucumber.java.en.*;
6+
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
10+
11+
import org.springframework.http.MediaType;
12+
13+
import java.nio.charset.StandardCharsets;
14+
import java.time.ZonedDateTime;
15+
import cat.udl.eps.softarch.demo.domain.Visibility;
16+
17+
import org.junit.jupiter.api.Assertions;
18+
19+
public class ContentRepositoryStepsDefs {
20+
21+
private final ContentRepository contentRepository;
22+
private Long createdContentId;
23+
private final StepDefs stepDefs;
24+
25+
public ContentRepositoryStepsDefs(
26+
ContentRepository contentRepository,
27+
StepDefs stepDefs) {
28+
this.contentRepository = contentRepository;
29+
this.stepDefs = stepDefs;
30+
}
31+
32+
@Given("there are no Contents in the system")
33+
public void there_are_no_Contents_in_the_system() {
34+
contentRepository.deleteAll();
35+
Assertions.assertEquals(0, contentRepository.count());
36+
}
37+
38+
@Given("^There is a Content with name \"([^\"]*)\" and description \"([^\"]*)\"$")
39+
public void thereIsAContentWithNameAndDescription(String name, String description) {
40+
Content content = new Content();
41+
content.setName(name);
42+
content.setDescription(description);
43+
content.setBody("Sample body");
44+
content.setCreatedAt(ZonedDateTime.now());
45+
content.setModifiedAt(ZonedDateTime.now());
46+
content.setVisibility(Visibility.PUBLIC);
47+
48+
contentRepository.save(content);
49+
}
50+
51+
@When("^I create a new content with name \"([^\"]*)\" and description \"([^\"]*)\"$")
52+
public void iCreateANewContent(String name, String description) throws Throwable {
53+
Content content = new Content();
54+
content.setName(name);
55+
content.setDescription(description);
56+
content.setBody("Sample body");
57+
content.setCreatedAt(ZonedDateTime.now());
58+
content.setModifiedAt(ZonedDateTime.now());
59+
content.setVisibility(Visibility.PUBLIC);
60+
61+
stepDefs.result = stepDefs.mockMvc.perform(
62+
post("/contents")
63+
.contentType(MediaType.APPLICATION_JSON)
64+
.content(stepDefs.mapper.writeValueAsString(content))
65+
.characterEncoding(StandardCharsets.UTF_8)
66+
.accept(MediaType.APPLICATION_JSON)
67+
.with(AuthenticationStepDefs.authenticate()))
68+
.andDo(print());
69+
70+
String location = stepDefs.result.andReturn().getResponse().getHeader("Location");
71+
createdContentId = Long.valueOf(location.substring(location.lastIndexOf("/") + 1));
72+
}
73+
74+
@Then("Content existsById should return true")
75+
public void exists_by_id_should_return_true() {
76+
Assertions.assertNotNull(createdContentId);
77+
Assertions.assertTrue(contentRepository.existsById(createdContentId));
78+
}
79+
}

src/test/java/cat/udl/eps/softarch/demo/steps/RegisterStepDefs.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import io.cucumber.java.en.Given;
1515
import io.cucumber.java.en.When;
1616
import org.json.JSONObject;
17-
import org.junit.jupiter.api.Assertions.*;
1817
import org.springframework.http.MediaType;
1918

2019
import java.nio.charset.StandardCharsets;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package cat.udl.eps.softarch.demo.steps;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
7+
8+
import java.nio.charset.StandardCharsets;
9+
import java.time.ZonedDateTime;
10+
11+
import org.junit.jupiter.api.Assertions;
12+
import org.springframework.http.MediaType;
13+
14+
import cat.udl.eps.softarch.demo.domain.Content;
15+
import cat.udl.eps.softarch.demo.domain.Report;
16+
17+
import cat.udl.eps.softarch.demo.repository.ContentRepository;
18+
import cat.udl.eps.softarch.demo.repository.ReportRepository;
19+
import io.cucumber.core.internal.com.fasterxml.jackson.databind.node.ObjectNode;
20+
import io.cucumber.java.ParameterType;
21+
import io.cucumber.java.en.Given;
22+
import io.cucumber.java.en.Then;
23+
import io.cucumber.java.en.When;
24+
25+
public class ReportRepositoryStepsDefs {
26+
27+
private final ReportRepository reportRepository;
28+
private final ContentRepository contentRepository;
29+
private final StepDefs stepDefs;
30+
private Long createdReportId;
31+
32+
@ParameterType(".*")
33+
public Content content(String name) {
34+
return this.contentRepository.findByName(name)
35+
.orElseThrow(() -> new RuntimeException("Content not found"));
36+
}
37+
38+
@ParameterType("\\d+")
39+
public Long Long(String value) {
40+
return Long.valueOf(value);
41+
}
42+
43+
public ReportRepositoryStepsDefs(
44+
ReportRepository reportRepository,
45+
ContentRepository contentRepository,
46+
StepDefs stepDefs) {
47+
this.reportRepository = reportRepository;
48+
this.contentRepository = contentRepository;
49+
this.stepDefs = stepDefs;
50+
}
51+
52+
@Given("there are no Reports in the system")
53+
public void there_are_no_Reports_in_the_system() {
54+
reportRepository.deleteAll();
55+
Assertions.assertEquals(0, reportRepository.count());
56+
}
57+
58+
@When("^I create a Report with the last created content and reason \"([^\"]*)\"$")
59+
public void iCreateAReportWithTheLastCreatedContent(String reason) throws Exception {
60+
Content content = contentRepository.findAll()
61+
.stream()
62+
.reduce((first, second) -> second)
63+
.orElseThrow(() -> new RuntimeException("No contents found"));
64+
65+
String contentUri = "/contents/" + content.getContentId();
66+
67+
com.fasterxml.jackson.databind.node.ObjectNode json = stepDefs.mapper.createObjectNode();
68+
json.put("reason", reason);
69+
json.put("createdAt", ZonedDateTime.now().toString());
70+
json.put("content", contentUri);
71+
72+
stepDefs.result = stepDefs.mockMvc.perform(
73+
post("/reports")
74+
.contentType(MediaType.APPLICATION_JSON)
75+
.content(json.toString())
76+
.characterEncoding(StandardCharsets.UTF_8)
77+
.accept(MediaType.APPLICATION_JSON)
78+
.with(AuthenticationStepDefs.authenticate()))
79+
.andDo(print())
80+
.andExpect(status().isCreated());
81+
82+
// Leer el ID real desde el header Location
83+
String location = stepDefs.result.andReturn().getResponse().getHeader("Location");
84+
createdReportId = Long.valueOf(location.substring(location.lastIndexOf("/") + 1));
85+
}
86+
87+
@Then("Report existsById should return true")
88+
public void exists_by_id_should_return_true() {
89+
Assertions.assertNotNull(createdReportId);
90+
Assertions.assertTrue(reportRepository.existsById(createdReportId));
91+
}
92+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Feature: Content management
2+
3+
Scenario: Create a new Content
4+
Given there are no Contents in the system
5+
And There is a registered user with username "maria" and password "passwordmaria" and email "maria@gmail.com"
6+
And I login as "maria" with password "passwordmaria"
7+
When I create a new content with name "Fulp Piction" and description "Directed by Tuentin Qarantino"
8+
Then Content existsById should return true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Feature: Report management
2+
3+
Scenario: Create a new Report
4+
Given there are no Reports in the system
5+
And there are no Contents in the system
6+
And There is a registered user with username "eric" and password "passworderic" and email "eric@gmail.com"
7+
And I login as "eric" with password "passworderic"
8+
And There is a Content with name "Test Content" and description "Some description"
9+
When I create a Report with the last created content and reason "Inappropriate content"
10+
Then Report existsById should return true

0 commit comments

Comments
 (0)