Skip to content

Commit

Permalink
Rework validators
Browse files Browse the repository at this point in the history
This commit reworks the structure of the validator classes, fixes
incorrect validations and adds missing validations.
  • Loading branch information
mkellnhofer committed Feb 15, 2018
1 parent 8bd4fba commit a906a7e
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 78 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/dracoon/sdk/internal/DracoonNodesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public NodeList getChildNodes(long parentNodeId) throws DracoonNetIOException,
DracoonApiException {
assertServerApiVersion();

NodeValidator.validateGetChildRequest(parentNodeId);

String accessToken = mClient.getAccessToken();
Call<ApiNodeList> call = mService.getChildNodes(accessToken, parentNodeId, 1, null, null,
null);
Expand All @@ -94,6 +96,8 @@ public NodeList getChildNodes(long parentNodeId) throws DracoonNetIOException,
public Node getNode(long nodeId) throws DracoonNetIOException, DracoonApiException {
assertServerApiVersion();

NodeValidator.validateGetRequest(nodeId);

String accessToken = mClient.getAccessToken();
Call<ApiNode> call = mService.getNode(accessToken, nodeId);
Response<ApiNode> response = mHttpHelper.executeRequest(call);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dracoon/sdk/internal/EncFileUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected Node upload() throws DracoonFileIOException, DracoonCryptoException,

String uploadId = createUpload(mRequest.getParentId(), mRequest.getName(),
mRequest.getClassification().getValue(), mRequest.getNotes(),
mRequest.getExpiration());
mRequest.getExpirationDate());

PlainFileKey plainFileKey = createFileKey(mUserPublicKey);

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/dracoon/sdk/internal/FileUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected Node upload() throws DracoonFileIOException, DracoonCryptoException,

String uploadId = createUpload(mRequest.getParentId(), mRequest.getName(),
mRequest.getClassification().getValue(), mRequest.getNotes(),
mRequest.getExpiration());
mRequest.getExpirationDate());

uploadFile(uploadId, mRequest.getName(), mSrcStream, mSrcLength);

Expand All @@ -175,7 +175,7 @@ protected Node upload() throws DracoonFileIOException, DracoonCryptoException,
}

protected String createUpload(long parentNodeId, String name, int classification, String notes,
Date expiration) throws DracoonNetIOException, DracoonApiException,
Date expirationDate) throws DracoonNetIOException, DracoonApiException,
InterruptedException {
String authToken = mClient.getAccessToken();

Expand All @@ -184,10 +184,10 @@ protected String createUpload(long parentNodeId, String name, int classification
request.name = name;
request.classification = classification;
request.notes = notes;
if (expiration != null) {
if (expirationDate != null) {
ApiExpiration apiExpiration = new ApiExpiration();
apiExpiration.enableExpiration = expiration.getTime() != 0L;
apiExpiration.expireAt = expiration;
apiExpiration.enableExpiration = expirationDate.getTime() != 0L;
apiExpiration.expireAt = expirationDate;
request.expiration = apiExpiration;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/dracoon/sdk/internal/mapper/FileMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public static ApiUpdateFileRequest toApiUpdateFileRequest(UpdateFileRequest requ
apiRequest.classification = classification.getValue();
}
apiRequest.notes = request.getNotes();
Date expiration = request.getExpiration();
if (expiration != null) {
Date expirationDate = request.getExpirationDate();
if (expirationDate != null) {
ApiExpiration apiExpiration = new ApiExpiration();
apiExpiration.enableExpiration = expiration.getTime() != 0L;
apiExpiration.expireAt = expiration;
apiExpiration.enableExpiration = expirationDate.getTime() != 0L;
apiExpiration.expireAt = expirationDate;
apiRequest.expiration = apiExpiration;
}
return apiRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.dracoon.sdk.internal.validator;

import java.util.List;

public class BaseValidator {

protected static void validateNodeId(Long id) {
validateId(id, "Node");
}

protected static void validateNodeIds(List<Long> ids) {
validateIds(ids, "Node");
}

protected static void validateParentNodeId(Long id) {
validateId(id, "Parent node");
}

protected static void validateRoomId(Long id) {
validateId(id, "Room");
}

protected static void validateFolderId(Long id) {
validateId(id, "Folder");
}

protected static void validateFileId(Long id) {
validateId(id, "File");
}

protected static void validateUserId(Long id) {
validateId(id, "User");
}

protected static void validateUserIds(List<Long> ids) {
validateIds(ids, "User");
}

protected static void validateGroupId(Long id) {
validateId(id, "Group");
}

protected static void validateGroupIds(List<Long> ids) {
validateIds(ids, "Group");
}

protected static void validateName(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name cannot be empty.");
}
}

protected static void validateQuota(Long quota) {
if (quota == null) {
return;
}
if (quota < 0) {
throw new IllegalArgumentException("Quota cannot be negative.");
}
}

protected static void validatePeriod(Integer period) {
if (period == null) {
return;
}
if (period < 0) {
throw new IllegalArgumentException("Period cannot be negative.");
}
}

private static void validateId(Long id, String type) {
if (id == null) {
throw new IllegalArgumentException(type + " ID cannot be null.");
}
if (id <= 0L) {
throw new IllegalArgumentException(type + " ID cannot be negative or 0.");
}
}

private static void validateIds(List<Long> ids, String type) {
if (ids == null) {
throw new IllegalArgumentException(type + " IDs cannot be null.");
}
if (ids.isEmpty()) {
throw new IllegalArgumentException(type + " IDs cannot be empty.");
}
ids.forEach(id -> validateId(id, type));
}

}
35 changes: 13 additions & 22 deletions src/main/java/com/dracoon/sdk/internal/validator/FileValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,7 @@

import java.io.File;

public class FileValidator {

public static void validateUpdateRequest(UpdateFileRequest request) {
if (request == null) {
throw new IllegalArgumentException("File update request cannot be null.");
}
if (request.getId() == null) {
throw new IllegalArgumentException("File ID cannot be null.");
}
if (request.getId() <= 0L) {
throw new IllegalArgumentException("File ID cannot be negative or 0.");
}
if (request.getName() != null && request.getName().isEmpty()) {
throw new IllegalArgumentException("File name cannot be empty.");
}
}
public class FileValidator extends BaseValidator {

public static void validateUploadRequest(String id, FileUploadRequest request, File file) {
if (id == null || id.isEmpty()) {
Expand All @@ -29,15 +14,21 @@ public static void validateUploadRequest(String id, FileUploadRequest request, F
if (request == null) {
throw new IllegalArgumentException("Upload request cannot be null.");
}
if (request.getParentId() == null) {
throw new IllegalArgumentException("Upload parent ID cannot be null.");
}
if (request.getName() == null || request.getName().isEmpty()) {
throw new IllegalArgumentException("Upload file name cannot be null or empty.");
}
validateParentNodeId(request.getParentId());
validateName(request.getName());
if (file == null) {
throw new IllegalArgumentException("Upload file cannot be null.");
}
}

public static void validateUpdateRequest(UpdateFileRequest request) {
if (request == null) {
throw new IllegalArgumentException("File update request cannot be null.");
}
validateFileId(request.getId());
if (request.getName() != null) {
validateName(request.getName());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,23 @@
import com.dracoon.sdk.model.CreateFolderRequest;
import com.dracoon.sdk.model.UpdateFolderRequest;

public class FolderValidator {
public class FolderValidator extends BaseValidator {

public static void validateCreateRequest(CreateFolderRequest request) {
if (request == null) {
throw new IllegalArgumentException("Folder creation request cannot be null.");
}
if (request.getParentId() == null) {
throw new IllegalArgumentException("Folder parent ID cannot be null.");
}
if (request.getParentId() < 0L) {
throw new IllegalArgumentException("Folder parent ID cannot be negative.");
}
if (request.getName() == null || request.getName().isEmpty()) {
throw new IllegalArgumentException("Folder name cannot be null or empty.");
}
validateParentNodeId(request.getParentId());
validateName(request.getName());
}

public static void validateUpdateRequest(UpdateFolderRequest request) {
if (request == null) {
throw new IllegalArgumentException("Folder update request cannot be null.");
}
if (request.getId() == null) {
throw new IllegalArgumentException("Folder ID cannot be null.");
}
if (request.getId() <= 0L) {
throw new IllegalArgumentException("Folder ID cannot be negative or 0.");
validateFolderId(request.getId());
if (request.getName() != null) {
validateName(request.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import com.dracoon.sdk.model.DeleteNodesRequest;

public class NodeValidator {
public class NodeValidator extends BaseValidator {

public static void validateGetRequest(long id) {
BaseValidator.validateNodeId(id);
}

public static void validateGetChildRequest(long id) {
if (id != 0L) {
BaseValidator.validateParentNodeId(id);
}
}

public static void validateDeleteRequest(DeleteNodesRequest request) {
if (request == null) {
throw new IllegalArgumentException("Nodes delete request cannot be null.");
}
if (request.getIds() == null) {
throw new IllegalArgumentException("Node IDs cannot be null.");
}
if (request.getIds().contains(null)) {
throw new IllegalArgumentException("Node ID cannot be null.");
}
validateNodeIds(request.getIds());
}

}
29 changes: 19 additions & 10 deletions src/main/java/com/dracoon/sdk/internal/validator/RoomValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,39 @@
import com.dracoon.sdk.model.CreateRoomRequest;
import com.dracoon.sdk.model.UpdateRoomRequest;

public class RoomValidator {
public class RoomValidator extends BaseValidator {

public static void validateCreateRequest(CreateRoomRequest request) {
if (request == null) {
throw new IllegalArgumentException("Room creation request cannot be null.");
}
if (request.getParentId() != null && request.getParentId() < 0L) {
throw new IllegalArgumentException("Room parent ID cannot be negative.");
if (request.getParentId() != null) {
validateParentNodeId(request.getParentId());
}
if (request.getName() == null || request.getName().isEmpty()) {
throw new IllegalArgumentException("Room name cannot be null or empty.");
validateName(request.getName());
validateQuota(request.getQuota());
validatePeriod(request.getRecycleBinRetentionPeriod());
if ((request.getAdminUserIds() == null || request.getAdminUserIds().isEmpty()) &&
(request.getAdminGroupIds() == null || request.getAdminGroupIds().isEmpty())) {
throw new IllegalArgumentException("Room must have an admin user or admin group.");
}
if (request.getAdminUserIds() != null) {
validateUserIds(request.getAdminUserIds());
}
if (request.getAdminGroupIds() != null) {
validateGroupIds(request.getAdminGroupIds());
}
}

public static void validateUpdateRequest(UpdateRoomRequest request) {
if (request == null) {
throw new IllegalArgumentException("Room update request cannot be null.");
}
if (request.getId() == null) {
throw new IllegalArgumentException("Room ID cannot be null.");
}
if (request.getId() <= 0L) {
throw new IllegalArgumentException("Room ID cannot be negative or 0.");
if (request.getName() != null) {
validateName(request.getName());
}
validateRoomId(request.getId());
validateQuota(request.getQuota());
}

}
14 changes: 7 additions & 7 deletions src/main/java/com/dracoon/sdk/model/FileUploadRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FileUploadRequest {
private ResolutionStrategy mResolutionStrategy;
private Classification mClassification;
private String mNotes;
private Date mExpiration;
private Date mExpirationDate;

private FileUploadRequest() {

Expand Down Expand Up @@ -71,8 +71,8 @@ public String getNotes() {
*
* @return the expiration date
*/
public Date getExpiration() {
return mExpiration;
public Date getExpirationDate() {
return mExpirationDate;
}

/**
Expand All @@ -86,7 +86,7 @@ public Date getExpiration() {
* - Classification: {@link Builder#classification(Classification)}<br>
* (Default: PUBLIC)<br>
* - Notes: {@link Builder#notes(String)}<br>
* - Expiration date: {@link Builder#expiration(Date)}
* - Expiration date: {@link Builder#expirationDate(Date)}
*/
public static class Builder {

Expand Down Expand Up @@ -145,12 +145,12 @@ public Builder notes(String notes) {
/**
* Sets the expiration date of the new file.
*
* @param expiration The expiration date.
* @param expirationDate The expiration date.
*
* @return a reference to this object
*/
public Builder expiration(Date expiration) {
mRequest.mExpiration = expiration;
public Builder expirationDate(Date expirationDate) {
mRequest.mExpirationDate = expirationDate;
return this;
}

Expand Down
Loading

0 comments on commit a906a7e

Please sign in to comment.