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]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright [2022] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.exceptions;
import com.wisemapping.model.Mindmap;
import com.wisemapping.model.User;
import com.wisemapping.service.LockManager;
import org.jetbrains.annotations.NotNull;
public class LockException
extends ClientException
{
extends ClientException {
private static final String MSG_KEY = "MINDMAP_IS_LOCKED";
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

View File

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

View File

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

View File

@ -1,20 +1,20 @@
/*
* Copyright [2022] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright [2022] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.JsonIgnoreProperties;
import com.wisemapping.model.Collaborator;
import com.wisemapping.model.User;
import com.wisemapping.service.LockInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -39,48 +40,42 @@ import javax.xml.bind.annotation.XmlRootElement;
@JsonIgnoreProperties(ignoreUnknown = true)
public class RestLockInfo {
@NotNull
final private Collaborator user;
@Nullable
final private LockInfo lockInfo;
private long session;
private long timestamp;
private String email;
// 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) {
this.lockInfo = lockInfo;
this.user = collaborator;
}
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 RestLockInfo(@Nullable LockInfo lockInfo, @NotNull User user) {
this.session = lockInfo.getSession();
this.timestamp = lockInfo.getTimestamp();
this.email = user.getEmail();
}
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) {
//
this.timestamp = value;
}
}

View File

@ -74,4 +74,13 @@ public class LockInfo {
this.previousTimestamp = this.timestamp;
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]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright [2022] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.service;
@ -41,5 +41,9 @@ public interface LockManager {
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
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)) {
throw new LockException("Invalid lock, this should not happen");
throw LockException.createLockLost(mindmap, user, this);
}
LockInfo result = lockInfoByMapId.get(mindmap.getId());

View File

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

View File

@ -23,6 +23,7 @@ import static org.testng.Assert.*;
@Test
public class RestMindmapITCase {
private static final int SESSION_ID = 100;
private String userEmail = "admin@wisemapping.com";
private static final String ICON = "glyphicon glyphicon-tag";
@ -217,8 +218,16 @@ public class RestMindmapITCase {
mapToUpdate.setXml("<map>this is not valid</map>");
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 ...
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);
final HttpEntity<RestMindmap> updateEntity = new HttpEntity<>(mapToUpdate, requestHeaders);
template.put(resourceUrl, updateEntity);