Fix unit tests

This commit is contained in:
Paulo Gustavo Veiga 2022-03-23 10:56:57 -03:00
parent 63f83e879d
commit b8eadb7533
9 changed files with 144 additions and 113 deletions

View File

@ -1,32 +1,39 @@
/* /*
* Copyright [2022] [wisemapping] * Copyright [2022] [wisemapping]
* *
* Licensed under WiseMapping Public License, Version 1.0 (the "License"). * Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the * It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page; * "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the license at * You may obtain a copy of the license at
* *
* http://www.wisemapping.org/license * http://www.wisemapping.org/license
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.wisemapping.exceptions; package com.wisemapping.exceptions;
import com.wisemapping.model.Mindmap;
import com.wisemapping.model.User;
import com.wisemapping.service.LockManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class LockException public class LockException
extends ClientException extends ClientException {
{
private static final String MSG_KEY = "MINDMAP_IS_LOCKED"; private static final String MSG_KEY = "MINDMAP_IS_LOCKED";
public LockException(@NotNull String message) { public LockException(@NotNull String message) {
super(message,Severity.INFO); super(message, Severity.INFO);
}
public static LockException createLockLost(@NotNull Mindmap mindmap, @NotNull User user, @NotNull LockManager manager) {
return new LockException("Lock can not be granted to " + user.getEmail() + ". The lock is assigned to " + manager.getLockInfo(mindmap));
} }
@NotNull @NotNull

View File

@ -31,10 +31,12 @@ import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.validation.BeanPropertyBindingResult; import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
@ -449,19 +451,23 @@ public class MindmapController extends BaseController {
mindmapService.updateCollaboration(user, collaboration.get()); mindmapService.updateCollaboration(user, collaboration.get());
} }
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/lock", consumes = {"text/plain"}, produces = {"application/json", "application/xml"}) @RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/locks/{lockid}", consumes = {"text/plain"}, produces = {"application/json", "application/xml"})
@ResponseStatus(value = HttpStatus.NO_CONTENT) public ResponseEntity<RestLockInfo> lockMindmap(@RequestBody String value, @PathVariable int id, @PathVariable long lockid) throws WiseMappingException {
public void updateMapLock(@RequestBody String value, @PathVariable int id) throws WiseMappingException {
final User user = Utils.getUser(); final User user = Utils.getUser();
final LockManager lockManager = mindmapService.getLockManager(); final LockManager lockManager = mindmapService.getLockManager();
final Mindmap mindmap = findMindmapById(id); final Mindmap mindmap = findMindmapById(id);
final boolean lock = Boolean.parseBoolean(value); ResponseEntity<RestLockInfo> result = new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
if (!lock) { if (Boolean.parseBoolean(value)) {
lockManager.unlock(mindmap, user); if (!lockManager.isLocked(mindmap)) {
} else { final LockInfo lockInfo = lockManager.lock(mindmap, user, lockid);
throw new UnsupportedOperationException("REST lock must be implemented."); final RestLockInfo restLockInfo = new RestLockInfo(lockInfo, user);
result = new ResponseEntity<>(restLockInfo, HttpStatus.OK);
} }
} else {
lockManager.unlock(mindmap, user);
}
return result;
} }
@RequestMapping(method = RequestMethod.DELETE, value = "/maps/batch") @RequestMapping(method = RequestMethod.DELETE, value = "/maps/batch")

View File

@ -1,20 +1,20 @@
/* /*
* Copyright [2022] [wisemapping] * Copyright [2022] [wisemapping]
* *
* Licensed under WiseMapping Public License, Version 1.0 (the "License"). * Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the * It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page; * "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the license at * You may obtain a copy of the license at
* *
* http://www.wisemapping.org/license * http://www.wisemapping.org/license
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.wisemapping.rest; package com.wisemapping.rest;
@ -23,11 +23,11 @@ import com.wisemapping.exceptions.WiseMappingException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
public class ValidationException extends WiseMappingException{ public class ValidationException extends WiseMappingException {
private final Errors errors; private final Errors errors;
public ValidationException(@NotNull Errors errors) { public ValidationException(@NotNull Errors errors) {
super("Validation Exceptions:"+errors); super("Validation Exceptions:" + errors);
this.errors = errors; this.errors = errors;
} }

View File

@ -1,20 +1,20 @@
/* /*
* Copyright [2022] [wisemapping] * Copyright [2022] [wisemapping]
* *
* Licensed under WiseMapping Public License, Version 1.0 (the "License"). * Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the * It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page; * "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the license at * You may obtain a copy of the license at
* *
* http://www.wisemapping.org/license * http://www.wisemapping.org/license
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.wisemapping.rest.model; package com.wisemapping.rest.model;
@ -22,6 +22,7 @@ package com.wisemapping.rest.model;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.wisemapping.model.Collaborator; import com.wisemapping.model.Collaborator;
import com.wisemapping.model.User;
import com.wisemapping.service.LockInfo; import com.wisemapping.service.LockInfo;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -39,48 +40,42 @@ import javax.xml.bind.annotation.XmlRootElement;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class RestLockInfo { public class RestLockInfo {
@NotNull private long session;
final private Collaborator user; private long timestamp;
private String email;
@Nullable
final private LockInfo lockInfo;
// This is required only for compliance with the JAXB serializer. // This is required only for compliance with the JAXB serializer.
public RestLockInfo(){ public RestLockInfo() {
this.lockInfo = null;
//noinspection ConstantConditions
this.user = null;
} }
public RestLockInfo(@Nullable LockInfo lockInfo, @NotNull Collaborator collaborator) { public RestLockInfo(@Nullable LockInfo lockInfo, @NotNull User user) {
this.session = lockInfo.getSession();
this.lockInfo = lockInfo; this.timestamp = lockInfo.getTimestamp();
this.user = collaborator; this.email = user.getEmail();
}
public boolean isLocked() {
return lockInfo != null;
}
public void setLocked(boolean locked) {
// Ignore ...
}
public boolean isLockedByMe() {
return isLocked() && lockInfo != null && lockInfo.getUser().identityEquality(user);
}
public void setLockedByMe(boolean lockedForMe) {
// Ignore ...
} }
public long getTimestamp() { public long getTimestamp() {
return lockInfo != null ? lockInfo.getTimestamp() : -1; return this.timestamp;
}
public long getSession() {
return this.session;
}
public void setSession(long session) {
this.session = session;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} }
public void setTimestamp(long value) { public void setTimestamp(long value) {
// this.timestamp = value;
} }
} }

View File

@ -74,4 +74,13 @@ public class LockInfo {
this.previousTimestamp = this.timestamp; this.previousTimestamp = this.timestamp;
this.timestamp = mindmap.getLastModificationTime().getTimeInMillis(); this.timestamp = mindmap.getLastModificationTime().getTimeInMillis();
} }
@Override
public String toString() {
return "LockInfo{" +
"user=" + user +
", session=" + session +
", timestamp=" + timestamp +
'}';
}
} }

View File

@ -1,20 +1,20 @@
/* /*
* Copyright [2022] [wisemapping] * Copyright [2022] [wisemapping]
* *
* Licensed under WiseMapping Public License, Version 1.0 (the "License"). * Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the * It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page; * "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the license at * You may obtain a copy of the license at
* *
* http://www.wisemapping.org/license * http://www.wisemapping.org/license
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.wisemapping.service; package com.wisemapping.service;
@ -41,5 +41,9 @@ public interface LockManager {
long generateSession(); long generateSession();
long verifyAndUpdateLock(@NotNull Mindmap mindmap, @NotNull User user, long session, long timestamp) throws LockException, SessionExpiredException; @NotNull
LockInfo lock(@NotNull Mindmap mindmap, @NotNull User user, long session) throws LockException;
long verifyAndUpdateLock(@NotNull Mindmap mindmap, @NotNull User user, long session, long timestamp) throws
LockException, SessionExpiredException;
} }

View File

@ -110,9 +110,10 @@ class LockManagerImpl implements LockManager {
} }
@NotNull @NotNull
private LockInfo lock(@NotNull Mindmap mindmap, @NotNull User user, long session) throws LockException { @Override
public LockInfo lock(@NotNull Mindmap mindmap, @NotNull User user, long session) throws LockException {
if (isLocked(mindmap) && !isLockedBy(mindmap, user)) { if (isLocked(mindmap) && !isLockedBy(mindmap, user)) {
throw new LockException("Invalid lock, this should not happen"); throw LockException.createLockLost(mindmap, user, this);
} }
LockInfo result = lockInfoByMapId.get(mindmap.getId()); LockInfo result = lockInfoByMapId.get(mindmap.getId());

View File

@ -21,7 +21,7 @@ public class RestHelper {
public static final String COLOR = "#000000"; public static final String COLOR = "#000000";
static HttpHeaders createHeaders(@NotNull MediaType mediaType) { static HttpHeaders createHeaders(@NotNull MediaType mediaType) {
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>(); List<MediaType> acceptableMediaTypes = new ArrayList<>();
acceptableMediaTypes.add(mediaType); acceptableMediaTypes.add(mediaType);
final HttpHeaders result = new HttpHeaders(); final HttpHeaders result = new HttpHeaders();

View File

@ -23,6 +23,7 @@ import static org.testng.Assert.*;
@Test @Test
public class RestMindmapITCase { public class RestMindmapITCase {
private static final int SESSION_ID = 100;
private String userEmail = "admin@wisemapping.com"; private String userEmail = "admin@wisemapping.com";
private static final String ICON = "glyphicon glyphicon-tag"; private static final String ICON = "glyphicon glyphicon-tag";
@ -217,8 +218,16 @@ public class RestMindmapITCase {
mapToUpdate.setXml("<map>this is not valid</map>"); mapToUpdate.setXml("<map>this is not valid</map>");
mapToUpdate.setProperties("{zoom:x}"); mapToUpdate.setProperties("{zoom:x}");
// Create lock ...
final HttpHeaders lockHeaders = createHeaders(mediaType);
lockHeaders.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> lockEntity = new HttpEntity<>("true", lockHeaders);
final ResponseEntity<RestLockInfo> lockResponse = template.exchange(HOST_PORT + resourceUri + "/locks/{lockid}", HttpMethod.PUT, lockEntity, RestLockInfo.class, SESSION_ID);
final RestLockInfo lockInfo = lockResponse.getBody();
// Update map ... // Update map ...
final String resourceUrl = HOST_PORT + resourceUri.toString() + "/document"; final String resourceUrl = HOST_PORT + resourceUri.toString() + "/document?session=" + lockInfo.getSession() + "&timestamp=" + lockInfo.getTimestamp();
requestHeaders.setContentType(MediaType.APPLICATION_XML); requestHeaders.setContentType(MediaType.APPLICATION_XML);
final HttpEntity<RestMindmap> updateEntity = new HttpEntity<>(mapToUpdate, requestHeaders); final HttpEntity<RestMindmap> updateEntity = new HttpEntity<>(mapToUpdate, requestHeaders);
template.put(resourceUrl, updateEntity); template.put(resourceUrl, updateEntity);