From 485814f22ab1e356b919e0ccc9f24cfd595a68b8 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sat, 6 Aug 2022 16:54:27 -0700 Subject: [PATCH] Add starred unit test --- .../rest/model/RestMindmapList.java | 6 +- .../test/rest/RestMindmapITCase.java | 152 +++++++++++------- 2 files changed, 93 insertions(+), 65 deletions(-) diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/model/RestMindmapList.java b/wise-webapp/src/main/java/com/wisemapping/rest/model/RestMindmapList.java index e2a642a8..aaef046c 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/model/RestMindmapList.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/model/RestMindmapList.java @@ -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 mindmaps, @NotNull Collaborator collaborator) { + public RestMindmapList(@NotNull List mindmaps, Collaborator collaborator) { this.mindmapsInfo = mindmaps.stream() .map(m->new RestMindmapInfo(m, collaborator)) .collect(Collectors.toList()); diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapITCase.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapITCase.java index 6b5bb5cc..cf4f2586 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapITCase.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestMindmapITCase.java @@ -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,12 +170,12 @@ 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 : "this is not valid"; + final String newXmlContent = content != null ? content : "this is not valid"; HttpEntity updateEntity = new HttpEntity<>(newXmlContent, requestHeaders); template.put(resourceUrl + "/document/xml", updateEntity); return newXmlContent; } - + private String updateMapDocument(final HttpHeaders requestHeaders, final RestTemplate template, final String resourceUrl) throws RestClientException { return updateMapDocument(requestHeaders, template, resourceUrl, null); } @@ -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 updateEntity = new HttpEntity<>("true", textContentType); + template.put(resourceUrl, updateEntity); + + // Has been updated ?. + final String mapId = mapUri.getPath().replace("/service/maps/", ""); + Optional 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"); @@ -364,7 +395,7 @@ public class RestMindmapITCase { template.put(HOST_PORT + resourceUri + "/collabs/", updateEntity); return newCollab; } - + @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") public void deleteCollabsWithInvalidEmail(final @NotNull MediaType mediaType) { final HttpHeaders requestHeaders = createHeaders(mediaType); @@ -375,22 +406,22 @@ 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:")); + assertTrue(e.getMessage().contains("Invalid email exception:")); } // Check that it has been removed ... final ResponseEntity afterDeleteResponse = fetchCollabs(requestHeaders, template, resourceUri); assertEquals(afterDeleteResponse.getBody().getCollaborations().size(), 1); } - + @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") public void deleteCollabsWithoutOwnerPermission(final @NotNull MediaType mediaType) { - - + + final HttpHeaders requestHeaders = createHeaders(mediaType); RestTemplate template = createTemplate(userEmail); @@ -398,15 +429,15 @@ 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")); + assertTrue(e.getMessage().contains("No enough permissions")); } } @@ -419,15 +450,15 @@ public class RestMindmapITCase { // Create a sample map ... final URI resourceUri = addNewMap(template, "Map for deleteOwnerCollab"); - // Now, remove owner collab ... - try { - template.delete(HOST_PORT + resourceUri + "/collabs?email=" + userEmail.replace(":admin", "") ); + // Now, 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")); + assertTrue(e.getMessage().contains("Can not remove owner collab")); } } - + @NotNull private ResponseEntity fetchCollabs(HttpHeaders requestHeaders, RestTemplate template, URI resourceUri) { final HttpEntity findCollabs = new HttpEntity(requestHeaders); @@ -477,15 +508,20 @@ public class RestMindmapITCase { // Remove label from map template.delete(BASE_REST_URL + "/maps/" + mapId + "/labels/" + labelId); + Optional mindmapInfo = fetchMap(requestHeaders, template, mapId); + assertTrue(mindmapInfo.get().getLabels().size() == 0); + + } + + @NotNull + private Optional fetchMap(HttpHeaders requestHeaders, RestTemplate template, @NotNull String mapId) { // Check that the label has been removed ... final List mindmapsInfo = fetchMaps(requestHeaders, template).getMindmapsInfo(); Optional 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 mindmapsInfo = fetchMaps(requestHeaders, template).getMindmapsInfo(); - Optional mindmapInfo = mindmapsInfo - .stream() - .filter(m -> m.getId() == Integer.parseInt(mapId)) - .findAny(); + Optional mindmapInfo = fetchMap(requestHeaders, template, mapId); assertTrue(mindmapInfo.get().getLabels().size() == 1); } @@ -544,17 +576,17 @@ public class RestMindmapITCase { 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); @@ -566,7 +598,7 @@ public class RestMindmapITCase { 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); @@ -609,20 +641,20 @@ public class RestMindmapITCase { final String title2 = "Batch delete map 2"; addNewMap(template, title2); - + 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); 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); @@ -641,11 +673,11 @@ public class RestMindmapITCase { //fetch public view final HttpEntity findMapEntity = new HttpEntity(requestHeaders); - ResponseEntity publicView = template.exchange(HOST_PORT + "/c/"+ mapId + "/public", HttpMethod.GET, findMapEntity, String.class); + ResponseEntity publicView = template.exchange(HOST_PORT + "/c/" + 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); @@ -655,10 +687,10 @@ public class RestMindmapITCase { 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 maps = template.exchange(HOST_PORT+resourceUri+"/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class); + final ResponseEntity maps = template.exchange(HOST_PORT + resourceUri + "/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class); assertEquals(maps.getBody().getCount(), 1); } @@ -670,26 +702,26 @@ public class RestMindmapITCase { // Create a sample map ... final URI resourceUri = addNewMap(template, "map to test revert changes"); updateMapDocument(requestHeaders, template, HOST_PORT + resourceUri.toString(), ""); - + updateMapDocument(requestHeaders, template, HOST_PORT + resourceUri.toString(), ""); - + //fetch map history final HttpEntity findMapEntity = new HttpEntity(requestHeaders); - final ResponseEntity mapHistories = template.exchange(HOST_PORT+resourceUri+"/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class); - + final ResponseEntity mapHistories = template.exchange(HOST_PORT + resourceUri + "/history/", HttpMethod.GET, findMapEntity, RestMindmapHistoryList.class); + //aply revert final HttpEntity 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 assertEquals(firstVersionMap.getXml(), ""); assertEquals(latestStoredMap.getXml(), ""); - + } - + @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") public void addCollabWhitoutOwnerPermission(final @NotNull MediaType mediaType) { final HttpHeaders requestHeaders = createHeaders(mediaType); @@ -710,17 +742,17 @@ public class RestMindmapITCase { final HttpEntity 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")); } } - + @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") public void addCollabWhitOwnerRole(final @NotNull MediaType mediaType) { final HttpHeaders requestHeaders = createHeaders(mediaType); @@ -740,14 +772,14 @@ public class RestMindmapITCase { addCollabToList(newCollab, role, collabs); final HttpEntity 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")); } } - + private String changeMapTitle(final HttpHeaders requestHeaders, final MediaType mediaType, final RestTemplate template, final URI resourceUri) throws RestClientException { // Change map title ... requestHeaders.setContentType(MediaType.TEXT_PLAIN); @@ -762,7 +794,7 @@ public class RestMindmapITCase { final ResponseEntity maps = template.exchange(BASE_REST_URL + "/maps/", HttpMethod.GET, findMapEntity, RestMindmapList.class); return maps.getBody(); } - + private RestCollaborationList fetchAndGetCollabs(final HttpHeaders requestHeaders, final RestTemplate template, final URI resourceUri) { final ResponseEntity response = fetchCollabs(requestHeaders, template, resourceUri); RestCollaborationList responseCollbs = response.getBody();