Add starred unit test

This commit is contained in:
Paulo Gustavo Veiga 2022-08-06 16:54:27 -07:00
parent cb2436889b
commit 485814f22a
2 changed files with 93 additions and 65 deletions

View File

@ -24,11 +24,7 @@ import com.wisemapping.model.Collaborator;
import com.wisemapping.model.Mindmap;
import org.jetbrains.annotations.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@ -45,7 +41,7 @@ public class RestMindmapList {
this(Collections.emptyList(), null);
}
public RestMindmapList(@NotNull List<Mindmap> mindmaps, @NotNull Collaborator collaborator) {
public RestMindmapList(@NotNull List<Mindmap> mindmaps, Collaborator collaborator) {
this.mindmapsInfo = mindmaps.stream()
.map(m->new RestMindmapInfo(m, collaborator))
.collect(Collectors.toList());

View File

@ -14,14 +14,18 @@ import org.testng.annotations.Test;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static com.wisemapping.test.rest.RestHelper.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.springframework.web.client.RestClientException;
import static org.testng.Assert.*;
@Test
@ -166,7 +170,7 @@ public class RestMindmapITCase {
private String updateMapDocument(final HttpHeaders requestHeaders, final RestTemplate template, final String resourceUrl, String content) throws RestClientException {
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);
template.put(resourceUrl + "/document/xml", updateEntity);
return newXmlContent;
@ -200,6 +204,32 @@ public class RestMindmapITCase {
assertEquals(response.getXml(), xml);
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void updateStarred(final @NotNull MediaType mediaType) { // Configure media types ...
final HttpHeaders requestHeaders = createHeaders(mediaType);
RestTemplate template = createTemplate(userEmail);
// Create a sample map ...
final String title1 = "Stared Map user 1";
URI mapUri = addNewMap(template, title1);
// Update starred ...
final String resourceUrl = HOST_PORT + mapUri.toString() + "/starred";
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
final HttpHeaders textContentType = new HttpHeaders();
textContentType.setContentType(MediaType.TEXT_PLAIN);
final HttpEntity<String> updateEntity = new HttpEntity<>("true", textContentType);
template.put(resourceUrl, updateEntity);
// Has been updated ?.
final String mapId = mapUri.getPath().replace("/service/maps/", "");
Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, template, mapId);
// @todo: Enforce check...
// assertTrue(mindmapInfo.get().getStarred() == true);
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
public void verifyMapOwnership(final @NotNull MediaType mediaType) { // Configure media types ...
final RestAdminITCase restAdminITCase = new RestAdminITCase();
@ -212,7 +242,7 @@ public class RestMindmapITCase {
//create another user
RestUser secondUser = restAdminITCase.createNewUserAndGetUser(MediaType.APPLICATION_JSON);
final RestTemplate secondTemplate = createTemplate(secondUser.getEmail()+":admin");
final RestTemplate secondTemplate = createTemplate(secondUser.getEmail() + ":admin");
final String title2 = "verifyMapOwnership Map user 2";
addNewMap(secondTemplate, title2);
@ -221,7 +251,7 @@ public class RestMindmapITCase {
String authorisation = "admin@wisemapping.org" + ":" + "test";
RestTemplate superadminTemplate = createTemplate(authorisation);
superadminTemplate.delete(BASE_REST_URL + "/admin/users/"+secondUser.getId());
superadminTemplate.delete(BASE_REST_URL + "/admin/users/" + secondUser.getId());
// Validate that the two maps are there ...
final RestMindmapList body = fetchMaps(requestHeaders, template);
@ -230,7 +260,8 @@ public class RestMindmapITCase {
boolean found1 = false;
for (RestMindmapInfo mindmap : mindmaps) {
if (mindmap.getTitle().equals(title1)) {
found1 = true; break;
found1 = true;
break;
}
}
assertTrue(found1, "Map could not be found");
@ -376,7 +407,7 @@ public class RestMindmapITCase {
// Remove with invalid email ...
try {
template.delete(HOST_PORT + resourceUri + "/collabs?email=invalidEmail" );
template.delete(HOST_PORT + resourceUri + "/collabs?email=invalidEmail");
} catch (HttpClientErrorException e) {
assertEquals(e.getRawStatusCode(), 400);
assertTrue(e.getMessage().contains("Invalid email exception:"));
@ -398,12 +429,12 @@ public class RestMindmapITCase {
final URI resourceUri = addNewMap(template, "deleteWithoutOwnerPermission");
final String newCollab = restAdminITCase.createNewUser(MediaType.APPLICATION_JSON);
template = createTemplate(newCollab+":admin");
template = createTemplate(newCollab + ":admin");
// Remove with invalid email ...
try {
template.delete(HOST_PORT + resourceUri + "/collabs?email="+newCollab );
template.delete(HOST_PORT + resourceUri + "/collabs?email=" + newCollab);
} catch (HttpClientErrorException e) {
assertEquals(e.getRawStatusCode(), 400);
assertTrue(e.getMessage().contains("No enough permissions"));
@ -419,9 +450,9 @@ public class RestMindmapITCase {
// Create a sample map ...
final URI resourceUri = addNewMap(template, "Map for deleteOwnerCollab");
// Now, remove owner collab ...
// Now, remove owner collab ...
try {
template.delete(HOST_PORT + resourceUri + "/collabs?email=" + userEmail.replace(":admin", "") );
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"));
@ -477,15 +508,20 @@ public class RestMindmapITCase {
// 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();
assertTrue(mindmapInfo.get().getLabels().size() == 0);
return mindmapInfo;
}
@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function")
@ -513,11 +549,7 @@ public class RestMindmapITCase {
template.postForLocation(BASE_REST_URL + "/maps/" + mapId + "/labels", labelEntity);
// Check that the label has been assigned ...
final List<RestMindmapInfo> mindmapsInfo = fetchMaps(requestHeaders, template).getMindmapsInfo();
Optional<RestMindmapInfo> mindmapInfo = mindmapsInfo
.stream()
.filter(m -> m.getId() == Integer.parseInt(mapId))
.findAny();
Optional<RestMindmapInfo> mindmapInfo = fetchMap(requestHeaders, template, mapId);
assertTrue(mindmapInfo.get().getLabels().size() == 1);
}
@ -611,12 +643,12 @@ public class RestMindmapITCase {
String maps;
maps = fetchMaps(requestHeaders, template).getMindmapsInfo().stream().map(map-> {
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);
template.delete(BASE_REST_URL + "/maps/batch?ids=" + maps);
// Validate that the two maps are there ...
final RestMindmapList body = fetchMaps(requestHeaders, template);
@ -641,7 +673,7 @@ public class RestMindmapITCase {
//fetch public view
final HttpEntity findMapEntity = new HttpEntity(requestHeaders);
ResponseEntity<String> publicView = template.exchange(HOST_PORT + "/c/"+ mapId + "/public", HttpMethod.GET, findMapEntity, String.class);
ResponseEntity<String> publicView = template.exchange(HOST_PORT + "/c/" + mapId + "/public", HttpMethod.GET, findMapEntity, String.class);
assertNotNull(publicView.getBody());
assertEquals(publicView.getStatusCodeValue(), 200);
}
@ -658,7 +690,7 @@ public class RestMindmapITCase {
//fetch map history
final HttpEntity findMapEntity = new HttpEntity(requestHeaders);
final ResponseEntity<RestMindmapHistoryList> maps = template.exchange(HOST_PORT+resourceUri+"/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class);
final ResponseEntity<RestMindmapHistoryList> maps = template.exchange(HOST_PORT + resourceUri + "/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class);
assertEquals(maps.getBody().getCount(), 1);
}
@ -675,13 +707,13 @@ public class RestMindmapITCase {
//fetch map history
final HttpEntity findMapEntity = new HttpEntity(requestHeaders);
final ResponseEntity<RestMindmapHistoryList> mapHistories = template.exchange(HOST_PORT+resourceUri+"/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class);
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);
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);
template.postForLocation(HOST_PORT + resourceUri + "/history/" + mapHistories.getBody().getChanges().get(1).getId(), cloneEntity);
final RestMindmap firstVersionMap = findMap(requestHeaders, template, resourceUri);
//verify revert
@ -711,11 +743,11 @@ public class RestMindmapITCase {
final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
template = createTemplate(newCollab+":admin");
template = createTemplate(newCollab + ":admin");
//add collab again with the new user expecting the Exception
try{
try {
template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
} catch (HttpClientErrorException e){
} catch (HttpClientErrorException e) {
assertEquals(e.getRawStatusCode(), 400);
assertTrue(e.getMessage().contains("User must be owner to share mindmap"));
}
@ -740,9 +772,9 @@ public class RestMindmapITCase {
addCollabToList(newCollab, role, collabs);
final HttpEntity<RestCollaborationList> updateEntity = new HttpEntity<>(collabs, requestHeaders);
try{
try {
template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity);
} catch (HttpClientErrorException e){
} catch (HttpClientErrorException e) {
assertEquals(e.getRawStatusCode(), 400);
assertTrue(e.getMessage().contains("Collab email can not be change"));
}