Fix test.

This commit is contained in:
Paulo Gustavo Veiga 2024-01-23 22:20:10 -08:00
parent ccfcb3b335
commit 740da238fa
8 changed files with 248 additions and 244 deletions

View File

@ -57,12 +57,6 @@
</exclusions>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-devtools</artifactId>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
@ -219,6 +213,10 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>

View File

@ -73,7 +73,7 @@ public class Label implements Serializable {
return title;
}
public void setTitle(@NotNull String title) {
public void setTitle(String title) {
this.title = title;
}
@ -85,7 +85,7 @@ public class Label implements Serializable {
this.id = id;
}
@NotNull
@Nullable
public String getColor() {
return color;
}

View File

@ -30,7 +30,7 @@ public class RestLabel {
this.label = label;
}
public void setParent(@NotNull final Label parent) {
public void setParent(final Label parent) {
this.label.setParent(parent);
}

View File

@ -55,14 +55,23 @@ public class LabelValidator implements Validator {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", Messages.FIELD_REQUIRED);
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "color", Messages.FIELD_REQUIRED);
final String title = label.getTitle();
ValidatorUtils.rejectIfExceeded(
errors,
"title",
"The description must have less than " + Constants.MAX_LABEL_NAME_LENGTH + " characters.",
title,
Constants.MAX_LABEL_NAME_LENGTH);
ValidatorUtils.rejectIfEmptyOrWhitespace(
errors,
"title",
"Label title can not be empty",
title);
final User user = com.wisemapping.security.Utils.getUser();
assert user != null;
final Label foundLabel = service.getLabelByTitle(title, user);
if (foundLabel != null) {
errors.rejectValue("title", Messages.LABEL_TITLE_ALREADY_EXISTS);

View File

@ -12,7 +12,7 @@ spring.sql.init.mode=always
spring.sql.init.platform=hsqldb
# LOG
logging.level.root=INFO
logging.level.root=DEBUG
logging.level.org.apache.tomcat=INFO
##################################################################################

View File

@ -0,0 +1,145 @@
package com.wisemapping.test.rest;
import com.wisemapping.config.common.CommonConfig;
import com.wisemapping.config.rest.RestAppConfig;
import com.wisemapping.rest.AdminController;
import com.wisemapping.rest.LabelController;
import com.wisemapping.rest.UserController;
import com.wisemapping.rest.model.RestLabel;
import com.wisemapping.rest.model.RestLabelList;
import com.wisemapping.rest.model.RestUser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.util.DefaultUriBuilderFactory;
import java.net.URI;
import java.util.List;
import java.util.Objects;
import static com.wisemapping.test.rest.RestHelper.BASE_REST_URL;
import static com.wisemapping.test.rest.RestHelper.createHeaders;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, LabelController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class RestLabelControllerTest {
private static final String COLOR = "#000000";
private RestUser user;
@Autowired
private TestRestTemplate restTemplate;
@BeforeEach
void createUser() {
// Remote debug ...
if (restTemplate == null) {
this.restTemplate = new TestRestTemplate();
this.restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:8081/"));
}
RestAccountControllerTest restAccount = RestAccountControllerTest.create(restTemplate);
this.user = restAccount.createNewUser();
}
static RestLabelList getLabels(HttpHeaders requestHeaders, @NotNull TestRestTemplate template) {
final HttpEntity<RestLabelList> findLabelEntity = new HttpEntity<>(requestHeaders);
final ResponseEntity<RestLabelList> response = template.exchange(BASE_REST_URL + "/labels/", HttpMethod.GET, findLabelEntity, RestLabelList.class);
return response.getBody();
}
@Test
public void createLabel() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a new label
final String title1 = "Label 1 - ";
addNewLabel(requestHeaders, restTemplate, title1, COLOR);
// Create a new label
final String title2 = "Label 2 - ";
addNewLabel(requestHeaders, restTemplate, title2, COLOR);
// Check that the label has been created ...
final RestLabelList restLabelList = getLabels(requestHeaders, restTemplate);
// Validate that the two labels are there ...
final List<RestLabel> labels = restLabelList.getLabels();
long count = labels.stream().filter(l -> Objects.equals(l.getTitle(), title1) || Objects.equals(l.getTitle(), title2)).count();
assertEquals(2, count, "Labels could not be found");
}
@Test
public void createLabelWithoutRequiredField() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
requestHeaders.set(HttpHeaders.ACCEPT_LANGUAGE, "en");
try {
addNewLabel(requestHeaders, restTemplate, null, COLOR);
fail("Wrong response");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("Required field cannot be left blank"));
}
try {
addNewLabel(requestHeaders, restTemplate, "title12345", null);
fail("Wrong response");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("Required field cannot be left blank"));
}
}
@Test
public void deleteLabel() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
final String title = "title to delete";
final URI resourceUri = addNewLabel(requestHeaders, restTemplate, title, COLOR);
// Now remove it ...
restTemplate.delete(resourceUri.toString());
final RestLabelList restLabelList = getLabels(requestHeaders, restTemplate);
for (RestLabel restLabel : restLabelList.getLabels()) {
if (title.equals(restLabel.getTitle())) {
fail("Label could not be removed:" + resourceUri);
}
}
}
static URI addNewLabel(@NotNull HttpHeaders requestHeaders, @NotNull TestRestTemplate template, @Nullable String title, @Nullable String color) {
final RestLabel restLabel = new RestLabel();
if (title != null) {
restLabel.setTitle(title);
}
if (color != null) {
restLabel.setColor(color);
}
// Create a new label ...
final HttpEntity<RestLabel> createUserEntity = new HttpEntity<>(restLabel, requestHeaders);
final ResponseEntity<String> result = template.exchange("/api/restfull/labels", HttpMethod.POST, createUserEntity, String.class);
if (!result.getStatusCode().is2xxSuccessful()) {
throw new IllegalStateException(result.toString());
}
;
return result.getHeaders().getLocation();
}
}

View File

@ -1,146 +0,0 @@
package com.wisemapping.test.rest;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.rest.model.RestLabel;
import com.wisemapping.rest.model.RestLabelList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.http.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import static com.wisemapping.test.rest.RestHelper.BASE_REST_URL;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.fail;
@Test
public class RestLabelITCase {
private String userEmail;
private static final String COLOR = "#000000";
@BeforeClass
void createUser() {
final RestAdminITCase restAdminITCase = new RestAdminITCase();
userEmail = restAdminITCase.createNewUser(MediaType.APPLICATION_JSON);
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void createLabel(final @NotNull MediaType mediaType) throws IOException { // Configure media types ...
final HttpHeaders requestHeaders = RestHelper.createHeaders(mediaType);
final RestTemplate template = RestHelper.createTemplate(userEmail + ":" + "admin");
// Create a new label
final String title1 = "Label 1 - " + mediaType;
addNewLabel(requestHeaders, template, title1, COLOR);
// Create a new label
final String title2 = "Label 2 - " + mediaType;
addNewLabel(requestHeaders, template, title2, COLOR);
// Check that the label has been created ...
final RestLabelList restLabelList = getLabels(requestHeaders, template);
// Validate that the two labels are there ...
final List<RestLabel> labels = restLabelList.getLabels();
boolean found1 = false;
boolean found2 = false;
for (RestLabel label : labels) {
if (title1.equals(label.getTitle())) {
found1 = true;
}
if (title2.equals(label.getTitle())) {
found2 = true;
}
}
assertTrue(found1 && found2, "Labels could not be found");
}
static RestLabelList getLabels(HttpHeaders requestHeaders, RestTemplate template) {
final HttpEntity findLabelEntity = new HttpEntity(requestHeaders);
final ResponseEntity<RestLabelList> response = template.exchange(BASE_REST_URL + "/labels/", HttpMethod.GET, findLabelEntity, RestLabelList.class);
return response.getBody();
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void createLabelWithoutRequiredField(final @NotNull MediaType mediaType) throws IOException {
final HttpHeaders requestHeaders = RestHelper.createHeaders(mediaType);
requestHeaders.set(HttpHeaders.ACCEPT_LANGUAGE, "en");
final RestTemplate template = RestHelper.createTemplate(userEmail + ":" + "admin");
try {
addNewLabel(requestHeaders, template, null, COLOR);
fail("Wrong response");
} catch (HttpClientErrorException e) {
final String responseBodyAsString = e.getResponseBodyAsString();
assertTrue(responseBodyAsString.contains("Required field cannot be left blank"));
}
try {
addNewLabel(requestHeaders, template, "title12345", null);
fail("Wrong response");
} catch (HttpClientErrorException e) {
final String responseBodyAsString = e.getResponseBodyAsString();
assert (responseBodyAsString.contains("Required field cannot be left blank"));
}
try {
addNewLabel(requestHeaders, template, "title12345", COLOR);
fail("Wrong response");
} catch (HttpClientErrorException e) {
final String responseBodyAsString = e.getResponseBodyAsString();
assert (responseBodyAsString.contains("Required field cannot be left blank"));
}
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void validateLabelsUserIsolation() { // Configure media types ...
throw new SkipException("missing test: labels belong to users");
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void deleteLabel(final @NotNull MediaType mediaType) throws IOException {
final HttpHeaders requestHeaders = RestHelper.createHeaders(mediaType);
final RestTemplate template = RestHelper.createTemplate(userEmail + ":" + "admin");
final String title = "title to delete";
final URI resourceUri = addNewLabel(requestHeaders, template, title, COLOR);
// Now remove it ...
template.delete(RestHelper.HOST_PORT + resourceUri.toString());
final RestLabelList restLabelList = getLabels(requestHeaders, template);
for (RestLabel restLabel : restLabelList.getLabels()) {
if (title.equals(restLabel.getTitle())) {
Assert.fail("Label could not be removed:" + resourceUri);
}
}
}
static URI addNewLabel(@NotNull HttpHeaders requestHeaders, @NotNull RestTemplate template, @Nullable String title, @Nullable String color) throws IOException {
final RestLabel restLabel = new RestLabel();
if (title != null) {
restLabel.setTitle(title);
}
if (color != null) {
restLabel.setColor(color);
}
// Create a new label ...
HttpEntity<RestLabel> createUserEntity = new HttpEntity<RestLabel>(restLabel, requestHeaders);
return template.postForLocation(BASE_REST_URL + "/labels", createUserEntity);
}
}

View File

@ -16,7 +16,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.util.DefaultUriBuilderFactory;
@ -37,8 +36,6 @@ public class RestMindmapControllerTest {
@Autowired
private TestRestTemplate restTemplate;
private RestAccountControllerTest restAccount;
@BeforeEach
void createUser() {
@ -47,7 +44,7 @@ public class RestMindmapControllerTest {
this.restTemplate = new TestRestTemplate();
this.restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:8081/"));
}
this.restAccount = RestAccountControllerTest.create(restTemplate);
RestAccountControllerTest restAccount = RestAccountControllerTest.create(restTemplate);
this.user = restAccount.createNewUser();
}
@ -405,6 +402,7 @@ public class RestMindmapControllerTest {
assertTrue(exchange.getStatusCode().is4xxClientError());
assertTrue(Objects.requireNonNull(exchange.getBody()).contains("Invalid email exception:"));
// Check that it has been removed ...
final ResponseEntity<RestCollaborationList> afterDeleteResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri);
assertEquals(Objects.requireNonNull(afterDeleteResponse.getBody()).getCollaborations().size(), 1);
@ -455,95 +453,95 @@ public class RestMindmapControllerTest {
return template.exchange(resourceUri + "/collabs", HttpMethod.GET, findCollabs, RestCollaborationList.class);
}
//
// @Test(dataProviderClass = RestHelper.class, expectedExceptions = {HttpClientErrorException.class}, dataProvider = "ContentType-Provider-Function")
// public void addCollabsInvalidOwner(final @NotNull MediaType mediaType) {
//
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail);
//
// // Create a sample map ...fetchAndGetCollabs(requestHeaders, template, resourceUri);
// final URI resourceUri = addNewMap(template, "Map for Collaboration - " + mediaType);
//
// // Add a new collaboration ...
// requestHeaders.setContentType(MediaType.APPLICATION_JSON);
// final RestCollaborationList collabs = new RestCollaborationList();
// collabs.setMessage("Adding new permission");
//
// // Validate that owner can not be added.
// addCollabToList("newCollab@example", "owner", collabs);
//
// final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
// template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void removeLabelFromMindmap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail);
//
// // Create a new label
// final String titleLabel = "removeLabelFromMindmap";
// final URI labelUri = RestLabelITCase.addNewLabel(requestHeaders, template, titleLabel, COLOR);
//
// // Create a sample map ...
// final String mapTitle = "removeLabelFromMindmap";
// final URI mindmapUri = addNewMap(template, mapTitle);
// final String mapId = mindmapUri.getPath().replace("/api/restfull/maps/", "");
//
// // Assign label to map ...
// String labelId = labelUri.getPath().replace("/api/restfull/labels/", "");
// HttpEntity<String> labelEntity = new HttpEntity<>(labelId, requestHeaders);
// template.postForLocation(BASE_REST_URL + "/maps/" + mapId + "/labels", labelEntity);
//
// // Remove label from map
// template.delete(BASE_REST_URL + "/maps/" + mapId + "/labels/" + labelId);
//
// Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, template, mapId);
// assertTrue(mindmapInfo.get().getLabels().size() == 0);
//
// }
//
// @NotNull
// private Optional<RestMindmapInfo> fetchMap(HttpHeaders requestHeaders, RestTemplate template, @NotNull String mapId) {
// // Check that the label has been removed ...
// final List<RestMindmapInfo> mindmapsInfo = fetchMaps(requestHeaders, template).getMindmapsInfo();
// Optional<RestMindmapInfo> mindmapInfo = mindmapsInfo
// .stream()
// .filter(m -> m.getId() == Integer.parseInt(mapId))
// .findAny();
// return mindmapInfo;
// }
@Test
public void addCollabsInvalidOwner() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a sample map ...fetchAndGetCollabs(requestHeaders, template, resourceUri);
final URI resourceUri = addNewMap(restTemplate, "Map for Collaboration");
// Add a new collaboration ...
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
final RestCollaborationList collabs = new RestCollaborationList();
collabs.setMessage("Adding new permission");
// Validate that owner can not be added.
addCollabToList("newCollab@example", "owner", collabs);
final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
restTemplate.put(resourceUri + "/collabs/", updateEntity);
}
@Test
public void removeLabelFromMindmap() throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a new label
final String titleLabel = "removeLabelFromMindmap";
final URI labelUri = RestLabelControllerTest.addNewLabel(requestHeaders, restTemplate, titleLabel, "red");
// Create a sample map ...
final String mapTitle = "removeLabelFromMindmap";
final URI mindmapUri = addNewMap(restTemplate, mapTitle);
final String mapId = mindmapUri.getPath().replace("/api/restfull/maps/", "");
// Assign label to map ...
String labelId = labelUri.getPath().replace("/api/restfull/labels/", "");
HttpEntity<String> labelEntity = new HttpEntity<>(labelId, requestHeaders);
restTemplate.postForLocation("/api/restfull/maps/" + mapId + "/labels", labelEntity);
// Remove label from map
restTemplate.delete("/api/restfull//maps/" + mapId + "/labels/" + labelId);
Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, restTemplate, mapId);
assertEquals(0, mindmapInfo.get().getLabels().size());
}
@NotNull
private Optional<RestMindmapInfo> fetchMap(HttpHeaders requestHeaders, TestRestTemplate template, @NotNull String mapId) {
// Check that the label has been removed ...
final List<RestMindmapInfo> mindmapsInfo = fetchMaps(requestHeaders, template).getMindmapsInfo();
return mindmapsInfo
.stream()
.filter(m -> m.getId() == Integer.parseInt(mapId))
.findAny();
}
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void deleteMapAndCheckLabels(final @NotNull MediaType mediaType) { // Configure media types ...
// throw new SkipException("missing test: delete map should not affects others labels");
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void addLabelToMindmap(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail);
//
// // Create a new label
// final String titleLabel = "Label 1 - " + mediaType;
// final URI labelUri = RestLabelITCase.addNewLabel(requestHeaders, template, titleLabel, COLOR);
//
// // Create a sample map ...
// final String mapTitle = "Maps 1 - " + mediaType;
// final URI mindmapUri = addNewMap(template, mapTitle);
// final String mapId = mindmapUri.getPath().replace("/api/restfull/maps/", "");
//
// // Assign label to map ...
// String labelId = labelUri.getPath().replace("/api/restfull/labels/", "");
// HttpEntity<String> labelEntity = new HttpEntity<>(labelId, requestHeaders);
// template.postForLocation(BASE_REST_URL + "/maps/" + mapId + "/labels", labelEntity);
//
// // Check that the label has been assigned ...
// Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, template, mapId);
//
// assertTrue(mindmapInfo.get().getLabels().size() == 1);
// }
@Test
public void addLabelToMindmap() throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a new label
final String titleLabel = "Label 1 - ";
final URI labelUri = RestLabelControllerTest.addNewLabel(requestHeaders, restTemplate, titleLabel, "COLOR");
// Create a sample map ...
final String mapTitle = "Maps 1 - ";
final URI mindmapUri = addNewMap(restTemplate, mapTitle);
final String mapId = mindmapUri.getPath().replace("/api/restfull/maps/", "");
// Assign label to map ...
String labelId = labelUri.getPath().replace("/api/restfull/labels/", "");
HttpEntity<String> labelEntity = new HttpEntity<>(labelId, requestHeaders);
restTemplate.postForLocation("/api/restfull/maps/" + mapId + "/labels", labelEntity);
// Check that the label has been assigned ...
Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, restTemplate, mapId);
assertTrue(mindmapInfo.get().getLabels().size() == 1);
}
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void updateCollabs(final @NotNull MediaType mediaType) {