Compare commits

..

No commits in common. "f86f96a35e9723f8624961f273d0f8a32a694c03" and "1059643b0f38540dbe1670b4c73e080f82384d52" have entirely different histories.

5 changed files with 301 additions and 290 deletions

View File

@ -18,7 +18,7 @@ import static org.springframework.security.config.Customizer.withDefaults;
@SpringBootApplication(scanBasePackageClasses = MindmapController.class) @SpringBootApplication(scanBasePackageClasses = MindmapController.class)
@Import({InterceptorsConfig.class}) @Import({ServletConfig.class, InterceptorsConfig.class})
@EnableWebSecurity @EnableWebSecurity
public class RestAppConfig { public class RestAppConfig {
@Bean @Bean

View File

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

View File

@ -38,7 +38,7 @@ import static com.wisemapping.test.rest.RestHelper.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, MindmapController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, MindmapController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class RestAccountControllerTest { public class RestAccountControllerTest {
private static final String ADMIN_USER = "admin@wisemapping.org"; private static final String ADMIN_USER = "admin@wisemapping.org";
private static final String ADMIN_PASSWORD = "test"; private static final String ADMIN_PASSWORD = "test";
@ -94,8 +94,6 @@ public class RestAccountControllerTest {
result = findUserByEmail(requestHeaders, templateRest, restUser.getEmail()); result = findUserByEmail(requestHeaders, templateRest, restUser.getEmail());
assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same."); assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same.");
// Assign generated id ...
restUser.setId(result.getBody().getId());
return restUser; return restUser;
} }

View File

@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.util.DefaultUriBuilderFactory; import org.springframework.web.util.DefaultUriBuilderFactory;
import java.net.URI; import java.net.URI;
@ -28,7 +29,7 @@ import static com.wisemapping.test.rest.RestHelper.createHeaders;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, LabelController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, LabelController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class RestLabelControllerTest { public class RestLabelControllerTest {
private static final String COLOR = "#000000"; private static final String COLOR = "#000000";

View File

@ -11,7 +11,6 @@ import com.wisemapping.rest.model.*;
import jakarta.annotation.Nullable; import jakarta.annotation.Nullable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -25,12 +24,11 @@ import java.net.URI;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import static com.wisemapping.test.rest.RestHelper.createHeaders; import static com.wisemapping.test.rest.RestHelper.createHeaders;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, MindmapController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, MindmapController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class RestMindmapControllerTest { public class RestMindmapControllerTest {
private RestUser user; private RestUser user;
@ -46,8 +44,7 @@ public class RestMindmapControllerTest {
this.restTemplate = new TestRestTemplate(); this.restTemplate = new TestRestTemplate();
this.restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:8081/")); this.restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:8081/"));
} }
RestAccountControllerTest restAccount = RestAccountControllerTest.create(restTemplate);
final RestAccountControllerTest restAccount = RestAccountControllerTest.create(restTemplate);
this.user = restAccount.createNewUser(); this.user = restAccount.createNewUser();
} }
@ -90,8 +87,7 @@ public class RestMindmapControllerTest {
final URI resourceUri = addNewMap(restTemplate, title1); final URI resourceUri = addNewMap(restTemplate, title1);
// Now remove it ... // Now remove it ...
final ResponseEntity<String> exchange = restTemplate.exchange(resourceUri.toString(), HttpMethod.DELETE, null, String.class); restTemplate.delete(resourceUri.toString());
assertTrue(exchange.getStatusCode().is2xxSuccessful(), "Status code:" + exchange.getStatusCode());
// Check that has been removed ... // Check that has been removed ...
try { try {
@ -172,9 +168,8 @@ public class RestMindmapControllerTest {
assertEquals(response.getXml(), newXmlContent); assertEquals(response.getXml(), newXmlContent);
} }
private String updateMapDocument(final HttpHeaders requestHeaders, final TestRestTemplate template, final String resourceUrl, final String content) throws RestClientException { private String updateMapDocument(final HttpHeaders requestHeaders, final TestRestTemplate template, final String resourceUrl, String content) throws RestClientException {
requestHeaders.setContentType(MediaType.TEXT_PLAIN); requestHeaders.setContentType(MediaType.TEXT_PLAIN);
final String newXmlContent = content != null ? content : "<map>this is not valid</map>"; final String newXmlContent = content != null ? content : "<map>this is not valid</map>";
HttpEntity<String> updateEntity = new HttpEntity<>(newXmlContent, requestHeaders); HttpEntity<String> updateEntity = new HttpEntity<>(newXmlContent, requestHeaders);
template.put(resourceUrl + "/document/xml", updateEntity); template.put(resourceUrl + "/document/xml", updateEntity);
@ -183,7 +178,7 @@ public class RestMindmapControllerTest {
@Test @Test
public void cloneMap() throws IOException { public void cloneMap() throws IOException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON); final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword()); final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
@ -199,10 +194,7 @@ public class RestMindmapControllerTest {
// Create a new map ... // Create a new map ...
final HttpEntity<RestMindmapInfo> cloneEntity = new HttpEntity<>(restMindmap, requestHeaders); final HttpEntity<RestMindmapInfo> cloneEntity = new HttpEntity<>(restMindmap, requestHeaders);
final ResponseEntity<Void> exchange = restTemplate.exchange(newMapUri.toString(), HttpMethod.POST, cloneEntity, Void.class); final URI clonedMapUri = restTemplate.postForLocation(newMapUri, cloneEntity);
assertTrue(exchange.getStatusCode().is2xxSuccessful());
URI clonedMapUri = exchange.getHeaders().getLocation();
// Check that the map has been updated ... // Check that the map has been updated ...
final RestMindmap response = findMap(requestHeaders, restTemplate, clonedMapUri); final RestMindmap response = findMap(requestHeaders, restTemplate, clonedMapUri);
@ -254,8 +246,7 @@ public class RestMindmapControllerTest {
addNewMap(secondTemplate, title2); addNewMap(secondTemplate, title2);
final TestRestTemplate superadminTemplate = this.restTemplate.withBasicAuth("admin@wisemapping.org", "test"); final TestRestTemplate superadminTemplate = this.restTemplate.withBasicAuth("admin@wisemapping.org", "test");
final ResponseEntity<String> exchange = superadminTemplate.exchange("/api/restfull/admin/users/" + secondUser.getId(), HttpMethod.DELETE, null, String.class); superadminTemplate.delete("/admin/users/" + secondUser.getId());
assertTrue(exchange.getStatusCode().is2xxSuccessful(), "Status Code:" + exchange.getStatusCode() + "- " + exchange.getBody());
// Validate that the two maps are there ... // Validate that the two maps are there ...
final RestMindmapList body = fetchMaps(requestHeaders, firstUser); final RestMindmapList body = fetchMaps(requestHeaders, firstUser);
@ -292,8 +283,8 @@ public class RestMindmapControllerTest {
// Check that the map has been updated ... // Check that the map has been updated ...
HttpEntity<RestUser> findMapEntity = new HttpEntity<>(requestHeaders); HttpEntity<RestUser> findMapEntity = new HttpEntity<>(requestHeaders);
final ResponseEntity<RestMindmap> response = restTemplate.exchange(resourceUri.toString(), HttpMethod.GET, findMapEntity, RestMindmap.class); final ResponseEntity<RestMindmap> response = restTemplate.exchange(resourceUri, HttpMethod.GET, findMapEntity, RestMindmap.class);
assertEquals(Objects.requireNonNull(response.getBody()).getXml(), mapToUpdate.getXml()); assertEquals(response.getBody().getXml(), mapToUpdate.getXml());
assertEquals(response.getBody().getProperties(), mapToUpdate.getProperties()); assertEquals(response.getBody().getProperties(), mapToUpdate.getProperties());
// Unlock ... // Unlock ...
@ -354,7 +345,7 @@ public class RestMindmapControllerTest {
// Has been added ? // Has been added ?
final ResponseEntity<RestCollaborationList> afterResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri); final ResponseEntity<RestCollaborationList> afterResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri);
final Optional<RestCollaboration> updatedCollab = Objects.requireNonNull(afterResponse.getBody()).getCollaborations().stream().filter(c -> c.getEmail().equals(newCollab)).findAny(); final Optional<RestCollaboration> updatedCollab = afterResponse.getBody().getCollaborations().stream().filter(c -> c.getEmail().equals(newCollab)).findAny();
assertTrue(updatedCollab.isPresent()); assertTrue(updatedCollab.isPresent());
assertEquals(updatedCollab.get().getRole(), "viewer"); assertEquals(updatedCollab.get().getRole(), "viewer");
} }
@ -377,12 +368,11 @@ public class RestMindmapControllerTest {
assertEquals(responseCollbs.getCount(), 2); assertEquals(responseCollbs.getCount(), 2);
// Now, remove it ... // Now, remove it ...
final ResponseEntity<String> exchange = restTemplate.exchange(resourceUri + "/collabs?email=" + newCollab, HttpMethod.DELETE, null, String.class); restTemplate.delete(resourceUri + "/collabs?email=" + newCollab);
assertTrue(exchange.getStatusCode().is2xxSuccessful());
// Check that it has been removed ... // Check that it has been removed ...
final ResponseEntity<RestCollaborationList> afterDeleteResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri); final ResponseEntity<RestCollaborationList> afterDeleteResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri);
assertEquals(Objects.requireNonNull(afterDeleteResponse.getBody()).getCollaborations().size(), 1); assertEquals(afterDeleteResponse.getBody().getCollaborations().size(), 1);
} }
private String addNewCollaboration(final HttpHeaders requestHeaders, final TestRestTemplate template, final URI resourceUri) throws RestClientException { private String addNewCollaboration(final HttpHeaders requestHeaders, final TestRestTemplate template, final URI resourceUri) throws RestClientException {
@ -412,48 +402,54 @@ public class RestMindmapControllerTest {
assertTrue(exchange.getStatusCode().is4xxClientError()); assertTrue(exchange.getStatusCode().is4xxClientError());
assertTrue(Objects.requireNonNull(exchange.getBody()).contains("Invalid email exception:")); assertTrue(Objects.requireNonNull(exchange.getBody()).contains("Invalid email exception:"));
// Check that it has been removed ... // Check that it has been removed ...
final ResponseEntity<RestCollaborationList> afterDeleteResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri); final ResponseEntity<RestCollaborationList> afterDeleteResponse = fetchCollabs(requestHeaders, restTemplate, resourceUri);
assertEquals(Objects.requireNonNull(afterDeleteResponse.getBody()).getCollaborations().size(), 1); assertEquals(Objects.requireNonNull(afterDeleteResponse.getBody()).getCollaborations().size(), 1);
} }
@Test // @Test
public void deleteCollabsWithoutOwnerPermission() { // public void deleteCollabsWithoutOwnerPermission() {
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword()); // final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final URI resourceUri = addNewMap(restTemplate, "deleteWithoutOwnerPermission"); // final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
//
// Create a new user ... // // Create a sample map ...
final RestAccountControllerTest restAccount = RestAccountControllerTest.create(restTemplate); // final URI resourceUri = addNewMap(restTemplate, "deleteWithoutOwnerPermission");
this.user = restAccount.createNewUser(); //
// final String newCollab = restAdminITCase.createNewUser(MediaType.APPLICATION_JSON);
// Create template ... // template = createTemplate(newCollab + ":admin");
final RestAccountControllerTest accountController = RestAccountControllerTest.create(restTemplate); //
final RestUser anotherUser = accountController.createNewUser(); // // Remove with invalid email ...
final TestRestTemplate anotherTemplate = this.restTemplate.withBasicAuth(anotherUser.getEmail(), anotherUser.getPassword()); // try {
//
// Try to delete but I'm not the owner ... // template.delete(HOST_PORT + resourceUri + "/collabs?email=" + newCollab);
final ResponseEntity<String> exchange = anotherTemplate.exchange(resourceUri + "/collabs?email=" + anotherUser.getEmail(), HttpMethod.DELETE, null, String.class); // } catch (HttpClientErrorException e) {
assertTrue(exchange.getStatusCode().is4xxClientError()); // assertEquals(e.getRawStatusCode(), 400);
assertTrue(Objects.requireNonNull(exchange.getBody()).contains("No enough permissions")); // assertTrue(e.getMessage().contains("No enough permissions"));
// }
} //
// }
@Test //
public void deleteOwnerCollab() { // @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword()); // public void deleteOwnerCollab(final @NotNull MediaType mediaType) {
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// Create a sample map ... // final RestTemplate template = createTemplate(userEmail);
final URI resourceUri = addNewMap(restTemplate, "Map for deleteOwnerCollab"); //
// // Create a sample map ...
// Now, remove owner collab ... // final URI resourceUri = addNewMap(template, "Map for deleteOwnerCollab");
final ResponseEntity<String> exchange = restTemplate.exchange(resourceUri + "/collabs?email=" + user.getEmail().replace(":admin", ""), HttpMethod.DELETE, null, String.class); //
assertTrue(exchange.getStatusCode().is4xxClientError()); // // Now, remove owner collab ...
assertTrue(Objects.requireNonNull(exchange.getBody()).contains("Can not remove owner collab")); // try {
} // template.delete(HOST_PORT + resourceUri + "/collabs?email=" + userEmail.replace(":admin", ""));
// } catch (HttpClientErrorException e) {
// assertEquals(e.getRawStatusCode(), 400);
// assertTrue(e.getMessage().contains("Can not remove owner collab"));
// }
// }
@NotNull @NotNull
private ResponseEntity<RestCollaborationList> fetchCollabs(HttpHeaders requestHeaders, TestRestTemplate template, URI resourceUri) { private ResponseEntity<RestCollaborationList> fetchCollabs(HttpHeaders requestHeaders, TestRestTemplate template, URI resourceUri) {
final HttpEntity<RestCollaborationList> findCollabs = new HttpEntity<>(requestHeaders); final HttpEntity<RestCollaborationList> findCollabs = new HttpEntity(requestHeaders);
return template.exchange(resourceUri + "/collabs", HttpMethod.GET, findCollabs, RestCollaborationList.class); return template.exchange(resourceUri + "/collabs", HttpMethod.GET, findCollabs, RestCollaborationList.class);
} }
@ -479,7 +475,7 @@ public class RestMindmapControllerTest {
} }
@Test @Test
public void removeLabelFromMindmap() { // Configure media types ... public void removeLabelFromMindmap() throws IOException, WiseMappingException { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON); final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword()); final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
@ -498,9 +494,7 @@ public class RestMindmapControllerTest {
restTemplate.postForLocation("/api/restfull/maps/" + mapId + "/labels", labelEntity); restTemplate.postForLocation("/api/restfull/maps/" + mapId + "/labels", labelEntity);
// Remove label from map // Remove label from map
final ResponseEntity<String> exchange = restTemplate.exchange("/api/restfull//maps/" + mapId + "/labels/" + labelId, HttpMethod.DELETE, null, String.class); restTemplate.delete("/api/restfull//maps/" + mapId + "/labels/" + labelId);
assertTrue(exchange.getStatusCode().is2xxSuccessful());
Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, restTemplate, mapId); Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, restTemplate, mapId);
assertEquals(0, mindmapInfo.get().getLabels().size()); assertEquals(0, mindmapInfo.get().getLabels().size());
@ -517,14 +511,14 @@ public class RestMindmapControllerTest {
.findAny(); .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 @Test
@Disabled("missing test: delete map should not affects others labels") public void addLabelToMindmap() throws IOException, WiseMappingException { // Configure media types ...
public void deleteMapAndCheckLabels() { // Configure media types ...
}
@Test
public void addLabelToMindmap() { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON); final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword()); final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
@ -544,220 +538,237 @@ public class RestMindmapControllerTest {
// Check that the label has been assigned ... // Check that the label has been assigned ...
Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, restTemplate, mapId); Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, restTemplate, mapId);
assertEquals(1, mindmapInfo.get().getLabels().size());
} assertTrue(mindmapInfo.get().getLabels().size() == 1);
@Test
public void updateCollabs() {
// Create a sample map ...
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
final URI resourceUri = addNewMap(restTemplate, "Map for updateCollabs");
// Add a new collaboration ...
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
RestCollaborationList collabs = new RestCollaborationList();
collabs.setMessage("Adding new permission");
String newCollab = "new-collab@example.com";
String role = "editor";
addCollabToList(newCollab, role, collabs);
HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
restTemplate.put(resourceUri + "/collabs/", updateEntity);
collabs = fetchAndGetCollabs(requestHeaders, restTemplate, resourceUri);
//delete one collab
collabs.setCollaborations(collabs.getCollaborations().stream().filter(c -> c.getRole().equals("owner")).collect(Collectors.toList()));
//Add another collaborationMediaType
newCollab = "another-collab@example.com";
addCollabToList(newCollab, role, collabs);
//add owner to list
addCollabToList(user.getEmail().replace(":admin", ""), "owner", collabs);
updateEntity = new HttpEntity<>(collabs, requestHeaders);
restTemplate.postForLocation(resourceUri + "/collabs/", updateEntity);
final RestCollaborationList responseCollbs = fetchAndGetCollabs(requestHeaders, restTemplate, resourceUri);
// Has been another-collaboration list updated ?
assertTrue(responseCollbs.getCollaborations().stream().anyMatch(x -> x.getEmail().equals("another-collab@example.com")));
assertEquals(responseCollbs.getCount(), 2);
}
@Test
public void updateProperties() throws IOException, WiseMappingException {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a sample map ...
final String title = "updateProperties map";
final URI resourceUri = addNewMap(restTemplate, title);
// Build map to update ...
final RestMindmap mapToUpdate = new RestMindmap();
mapToUpdate.setXml("<map>this is not valid</map>");
mapToUpdate.setProperties("{zoom:x}");
mapToUpdate.setTitle("new title for map");
mapToUpdate.setDescription("updated map description");
// Update map ...
final String resourceUrl = resourceUri.toString();
final HttpEntity<RestMindmap> updateEntity = new HttpEntity<>(mapToUpdate, requestHeaders);
restTemplate.put(resourceUrl, updateEntity);
// Check that the map has been updated ...
HttpEntity<RestUser> findMapEntity = new HttpEntity<>(requestHeaders);
final ResponseEntity<RestMindmap> response = restTemplate.exchange(resourceUri.toString(), HttpMethod.GET, findMapEntity, RestMindmap.class);
assertEquals(response.getBody().getTitle(), mapToUpdate.getTitle());
assertEquals(response.getBody().getDescription(), mapToUpdate.getDescription());
assertEquals(response.getBody().getXml(), mapToUpdate.getXml());
assertEquals(response.getBody().getProperties(), mapToUpdate.getProperties());
}
@Test
public void batchDelete() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a sample map ...
final String title1 = "Batch delete map 1";
addNewMap(restTemplate, title1);
final String title2 = "Batch delete map 2";
addNewMap(restTemplate, title2);
final String maps = fetchMaps(requestHeaders, restTemplate).getMindmapsInfo().stream().map(map -> String.valueOf(map.getId())).collect(Collectors.joining(","));
final ResponseEntity<String> exchange = restTemplate.exchange("/api/restfull/maps/batch?ids=" + maps, HttpMethod.DELETE, null, String.class);
assertTrue(exchange.getStatusCode().is2xxSuccessful(), "Status code:" + exchange.getStatusCode() + " - " + exchange.getBody());
// Validate that the two maps are there ...
final RestMindmapList body = fetchMaps(requestHeaders, restTemplate);
assertEquals(0, body.getMindmapsInfo().size());
}
@Test
public void updatePublishState() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a sample map ...
final String mapTitle = "updatePublishState";
final URI mindmapUri = addNewMap(restTemplate, mapTitle);
// Change map status ...
requestHeaders.setContentType(MediaType.TEXT_PLAIN);
final HttpEntity<String> updateEntity = new HttpEntity<>(Boolean.TRUE.toString(), requestHeaders);
final ResponseEntity<String> exchange = restTemplate.exchange(mindmapUri + "/publish", HttpMethod.PUT, updateEntity, String.class);
assertTrue(exchange.getStatusCode().is2xxSuccessful());
}
@Test
public void fetchMapHistory() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a sample map ...
final URI resourceUri = addNewMap(restTemplate, "Map to change title");
updateMapDocument(requestHeaders, restTemplate, resourceUri.toString(), null);
//fetch map history
final HttpEntity<RestMindmapHistoryList> findMapEntity = new HttpEntity<>(requestHeaders);
final ResponseEntity<RestMindmapHistoryList> maps = restTemplate.exchange(resourceUri + "/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class);
assertTrue(maps.getStatusCode().is2xxSuccessful(), maps.toString());
assertEquals(1, Objects.requireNonNull(maps.getBody()).getCount());
}
@Test
public void updateRevertMindmap() throws IOException {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a sample map ...
final URI resourceUri = addNewMap(restTemplate, "map to test revert changes");
updateMapDocument(requestHeaders, restTemplate, resourceUri.toString(), "<map><node text='this is an xml to test revert changes service'></map>");
updateMapDocument(requestHeaders, restTemplate, resourceUri.toString(), "<map><node text='this is an xml with modification to be reverted'></map>");
//fetch map history
final HttpEntity<RestMindmapHistoryList> findMapEntity = new HttpEntity<>(requestHeaders);
final ResponseEntity<RestMindmapHistoryList> mapHistories = restTemplate.exchange(resourceUri + "/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class);
//aply revert
final HttpEntity<String> cloneEntity = new HttpEntity<>(requestHeaders);
restTemplate.postForLocation(resourceUri + "/history/latest", cloneEntity);
final RestMindmap latestStoredMap = findMap(requestHeaders, restTemplate, resourceUri);
restTemplate.postForLocation(resourceUri + "/history/" + mapHistories.getBody().getChanges().get(1).getId(), cloneEntity);
final RestMindmap firstVersionMap = findMap(requestHeaders, restTemplate, resourceUri);
//verify revert
assertEquals(firstVersionMap.getXml(), "<map><node text='this is an xml to test revert changes service'></map>");
assertEquals(latestStoredMap.getXml(), "<map><node text='this is an xml with modification to be reverted'></map>");
}
@Test
public void addCollabWhitoutOwnerPermission() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a sample map ...
final URI resourceUri = addNewMap(restTemplate, "MaddCollabWhitoutOwnerPermission");
// Add a new collaboration ...
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
final RestCollaborationList collabs = new RestCollaborationList();
collabs.setMessage("Adding new permission");
final RestAccountControllerTest restAccount = RestAccountControllerTest.create(restTemplate);
final RestUser newCollab = restAccount.createNewUser();
String role = "editor";
addCollabToList(newCollab.getEmail(), role, collabs);
final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
restTemplate.put(resourceUri + "/collabs/", updateEntity);
final TestRestTemplate newCollabTemplate = this.restTemplate.withBasicAuth(newCollab.getEmail(), newCollab.getPassword());
final ResponseEntity<Void> exchange = newCollabTemplate.exchange(resourceUri + "/collabs/", HttpMethod.PUT, updateEntity, Void.class);
assertTrue(exchange.getStatusCode().is4xxClientError(), exchange.toString());
}
@Test
public void addCollabWhitOwnerRole() {
final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON);
final TestRestTemplate restTemplate = this.restTemplate.withBasicAuth(user.getEmail(), user.getPassword());
// Create a sample map ...
final URI resourceUri = addNewMap(restTemplate, "addCollabWhitOwnerRole");
// Add a new collaboration ...
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
final RestCollaborationList collabs = new RestCollaborationList();
collabs.setMessage("Adding new permission");
final String newCollab = "new-collaborator@mail.com";
String role = "owner";
addCollabToList(newCollab, role, collabs);
final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
final ResponseEntity<RestCollaborationList> collabsList = restTemplate.exchange(resourceUri + "/collabs/", HttpMethod.PUT, updateEntity, RestCollaborationList.class);
assertTrue(collabsList.getStatusCode().is4xxClientError());
} }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void updateCollabs(final @NotNull MediaType mediaType) {
//
// // Create a sample map ...
// final RestTemplate template = createTemplate(userEmail);
// final URI resourceUri = addNewMap(template, "Map for updateCollabs - " + mediaType);
//
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// // Add a new collaboration ...
// requestHeaders.setContentType(MediaType.APPLICATION_JSON);
// RestCollaborationList collabs = new RestCollaborationList();
// collabs.setMessage("Adding new permission");
//
// String newCollab = "new-collab@example.com";
// String role = "editor";
//
// addCollabToList(newCollab, role, collabs);
//
// HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
// template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
//
// collabs = fetchAndGetCollabs(requestHeaders, template, resourceUri);
//
// //delete one collab
// collabs.setCollaborations(collabs.getCollaborations().stream().filter(c -> c.getRole().equals("owner")).collect(Collectors.toList()));
//
// //Add another collaborationMediaType
// newCollab = "another-collab@example.com";
// addCollabToList(newCollab, role, collabs);
//
// //add owner to list
// addCollabToList(userEmail.replace(":admin", ""), "owner", collabs);
//
// updateEntity = new HttpEntity<>(collabs, requestHeaders);
// template.postForLocation(HOST_PORT + resourceUri + "/collabs/", updateEntity);
//
//
// RestCollaborationList responseCollbs = fetchAndGetCollabs(requestHeaders, template, resourceUri);
//
// // Has been another-collaboration list updated ?
// assertTrue(responseCollbs.getCollaborations().stream().anyMatch(x -> x.getEmail().equals("another-collab@example.com")));
// assertEquals(responseCollbs.getCount(), 2);
//
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void updateProperties(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail);
//
// // Create a sample map ...
// final String title = "updateProperties map";
// final URI resourceUri = addNewMap(template, title);
//
// // Build map to update ...
// final RestMindmap mapToUpdate = new RestMindmap();
// mapToUpdate.setXml("<map>this is not valid</map>");
// mapToUpdate.setProperties("{zoom:x}");
// mapToUpdate.setTitle("new title for map");
// mapToUpdate.setDescription("updated map description");
//
// // Update map ...
// final String resourceUrl = HOST_PORT + resourceUri.toString();
// final HttpEntity<RestMindmap> updateEntity = new HttpEntity<>(mapToUpdate, requestHeaders);
// template.put(resourceUrl, updateEntity);
//
// // Check that the map has been updated ...
// HttpEntity<RestUser> findMapEntity = new HttpEntity<>(requestHeaders);
// final ResponseEntity<RestMindmap> response = template.exchange(HOST_PORT + resourceUri, HttpMethod.GET, findMapEntity, RestMindmap.class);
// assertEquals(response.getBody().getTitle(), mapToUpdate.getTitle());
// assertEquals(response.getBody().getDescription(), mapToUpdate.getDescription());
// assertEquals(response.getBody().getXml(), mapToUpdate.getXml());
// assertEquals(response.getBody().getProperties(), mapToUpdate.getProperties());
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void batchDelete(final @NotNull MediaType mediaType) { // Configure media types ...
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail);
//
// // Create a sample map ...
// final String title1 = "Batch delete map 1";
// addNewMap(template, title1);
//
// final String title2 = "Batch delete map 2";
// addNewMap(template, title2);
//
//
// String maps;
// maps = fetchMaps(requestHeaders, template).getMindmapsInfo().stream().map(map -> {
// return String.valueOf(map.getId());
// }).collect(Collectors.joining(","));
//
//
// template.delete(BASE_REST_URL + "/maps/batch?ids=" + maps);
//
// // Validate that the two maps are there ...
// final RestMindmapList body = fetchMaps(requestHeaders, template);
// assertEquals(body.getMindmapsInfo().size(), 0);
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void updatePublishState(final @NotNull MediaType mediaType) throws IOException, WiseMappingException { // Configure media types ...
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail);
//
// // Create a sample map ...
// final String mapTitle = "updatePublishState";
// final URI mindmapUri = addNewMap(template, mapTitle);
// final String mapId = mindmapUri.getPath().replace("/api/restfull/maps/", "");
//
// // Change map status ...
// requestHeaders.setContentType(MediaType.TEXT_PLAIN);
// //final String newPublicState = "true";
// final HttpEntity<String> updateEntity = new HttpEntity<>(Boolean.TRUE.toString(), requestHeaders);
// template.put(HOST_PORT + mindmapUri + "/publish", updateEntity);
//
//// //fetch public view
//// final HttpEntity findMapEntity = new HttpEntity(requestHeaders);
//// ResponseEntity<String> publicView = template.exchange(HOST_PORT + mapId + "/public", HttpMethod.GET, findMapEntity, String.class);
//// assertNotNull(publicView.getBody());
//// assertEquals(publicView.getStatusCodeValue(), 200);
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void fetchMapHistory(final @NotNull MediaType mediaType) { // Configure media types ...
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail);
//
// // Create a sample map ...
// final URI resourceUri = addNewMap(template, "Map to change title - " + mediaType);
//
// updateMapDocument(requestHeaders, template, HOST_PORT + resourceUri.toString());
//
// //fetch map history
// final HttpEntity findMapEntity = new HttpEntity(requestHeaders);
// final ResponseEntity<RestMindmapHistoryList> maps = template.exchange(HOST_PORT + resourceUri + "/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class);
// assertEquals(maps.getBody().getCount(), 1);
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void updateRevertMindmap(final @NotNull MediaType mediaType) throws IOException { // Configure media types ...
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// final RestTemplate template = createTemplate(userEmail);
//
// // Create a sample map ...
// final URI resourceUri = addNewMap(template, "map to test revert changes");
// updateMapDocument(requestHeaders, template, HOST_PORT + resourceUri.toString(), "<map><node text='this is an xml to test revert changes service'></map>");
//
// updateMapDocument(requestHeaders, template, HOST_PORT + resourceUri.toString(), "<map><node text='this is an xml with modification to be reverted'></map>");
//
// //fetch map history
// final HttpEntity findMapEntity = new HttpEntity(requestHeaders);
// final ResponseEntity<RestMindmapHistoryList> mapHistories = template.exchange(HOST_PORT + resourceUri + "/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class);
//
// //aply revert
// final HttpEntity<String> cloneEntity = new HttpEntity<>(requestHeaders);
// template.postForLocation(HOST_PORT + resourceUri + "/history/latest", cloneEntity);
// final RestMindmap latestStoredMap = findMap(requestHeaders, template, resourceUri);
// template.postForLocation(HOST_PORT + resourceUri + "/history/" + mapHistories.getBody().getChanges().get(1).getId(), cloneEntity);
// final RestMindmap firstVersionMap = findMap(requestHeaders, template, resourceUri);
//
// //verify revert
// assertEquals(firstVersionMap.getXml(), "<map><node text='this is an xml to test revert changes service'></map>");
// assertEquals(latestStoredMap.getXml(), "<map><node text='this is an xml with modification to be reverted'></map>");
//
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void addCollabWhitoutOwnerPermission(final @NotNull MediaType mediaType) {
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// RestTemplate template = createTemplate(userEmail);
//
// // Create a sample map ...
// final URI resourceUri = addNewMap(template, "MaddCollabWhitoutOwnerPermission");
//
// // Add a new collaboration ...
// requestHeaders.setContentType(MediaType.APPLICATION_JSON);
// final RestCollaborationList collabs = new RestCollaborationList();
// collabs.setMessage("Adding new permission");
//
// final String newCollab = restAdminITCase.createNewUser(MediaType.APPLICATION_JSON);
// String role = "editor";
//
// addCollabToList(newCollab, role, collabs);
//
// final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
// template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
//
// template = createTemplate(newCollab + ":admin");
// //add collab again with the new user expecting the Exception
// try {
// template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
// } catch (HttpClientErrorException e) {
// assertEquals(e.getRawStatusCode(), 400);
// assertTrue(e.getMessage().contains("User must be owner to share mindmap"));
// }
// }
//
// @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
// public void addCollabWhitOwnerRole(final @NotNull MediaType mediaType) {
// final HttpHeaders requestHeaders = createHeaders(mediaType);
// RestTemplate template = createTemplate(userEmail);
//
// // Create a sample map ...
// final URI resourceUri = addNewMap(template, "addCollabWhitOwnerRole");
//
// // Add a new collaboration ...
// requestHeaders.setContentType(MediaType.APPLICATION_JSON);
// final RestCollaborationList collabs = new RestCollaborationList();
// collabs.setMessage("Adding new permission");
//
// final String newCollab = "new-collaborator@mail.com";
// String role = "owner";
//
// addCollabToList(newCollab, role, collabs);
//
// final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
// try {
// template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
// } catch (HttpClientErrorException e) {
// assertEquals(e.getRawStatusCode(), 400);
// assertTrue(e.getMessage().contains("Collab email can not be change"));
// }
// }
//
private String changeMapTitle(final HttpHeaders requestHeaders, final MediaType mediaType, final TestRestTemplate template, final URI resourceUri) throws RestClientException { private String changeMapTitle(final HttpHeaders requestHeaders, final MediaType mediaType, final TestRestTemplate template, final URI resourceUri) throws RestClientException {
requestHeaders.setContentType(MediaType.TEXT_PLAIN); requestHeaders.setContentType(MediaType.TEXT_PLAIN);
final String result = "New map to change title - " + mediaType; final String result = "New map to change title - " + mediaType;
@ -778,7 +789,8 @@ public class RestMindmapControllerTest {
private RestCollaborationList fetchAndGetCollabs(final HttpHeaders requestHeaders, final TestRestTemplate template, final URI resourceUri) { private RestCollaborationList fetchAndGetCollabs(final HttpHeaders requestHeaders, final TestRestTemplate template, final URI resourceUri) {
final ResponseEntity<RestCollaborationList> response = fetchCollabs(requestHeaders, template, resourceUri); final ResponseEntity<RestCollaborationList> response = fetchCollabs(requestHeaders, template, resourceUri);
return response.getBody(); RestCollaborationList responseCollbs = response.getBody();
return responseCollbs;
} }
private RestCollaboration addCollabToList(String newCollab, String role, RestCollaborationList collabs) { private RestCollaboration addCollabToList(String newCollab, String role, RestCollaborationList collabs) {
@ -790,8 +802,8 @@ public class RestMindmapControllerTest {
} }
private RestMindmap findMap(@NotNull HttpHeaders requestHeaders, @NotNull TestRestTemplate template, URI resourceUri) { private RestMindmap findMap(@NotNull HttpHeaders requestHeaders, @NotNull TestRestTemplate template, URI resourceUri) {
final HttpEntity<RestMindmap> findMapEntity = new HttpEntity<>(requestHeaders); final HttpEntity findMapEntity = new HttpEntity(requestHeaders);
final ResponseEntity<RestMindmap> response = template.exchange(resourceUri.toString(), HttpMethod.GET, findMapEntity, RestMindmap.class); final ResponseEntity<RestMindmap> response = template.exchange("http://localhost:8081/" + resourceUri.toString(), HttpMethod.GET, findMapEntity, RestMindmap.class);
if (!response.getStatusCode().is2xxSuccessful()) { if (!response.getStatusCode().is2xxSuccessful()) {
throw new IllegalArgumentException(response.toString()); throw new IllegalArgumentException(response.toString());