Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: File rename #69

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions app/src/main/java/gateway/controller/CtrlFileRename.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package gateway.controller;

import gateway.config.Config;
import gateway.services.ServiceAuth;
import gateway.services.UtilValidator;
import gateway.soap.request.ReqFileRename;
import gateway.soap.response.ResStatus;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.util.UUID;
import org.json.JSONObject;

public class CtrlFileRename
{
public static ResStatus file_rename (ReqFileRename args)
{
ResStatus s = new ResStatus ();
UUID userUUID;

// validations

ResStatus resValidate = UtilValidator.validate (args);
if (resValidate.error) {
return resValidate;
}

ResStatus resAuth = ServiceAuth.authenticate (args.token);
if (resAuth.error) {
return resAuth;
}
userUUID = UUID.fromString (ServiceAuth.tokenGetClaim (args.token, "uuid"));

// rename

JSONObject body = new JSONObject ();
body.put ("name", args.newName);

// post

try {
HttpResponse<String> res = HttpClient.newHttpClient ().send (
HttpRequest.newBuilder ()
.uri (URI.create (String.format (
"%s/files/rename/%s/%s", Config.getMetadataBaseUrl (), userUUID.toString (),
args.fileUUID.toString ())))
.PUT (BodyPublishers.ofString (body.toString ()))
.build (),
HttpResponse.BodyHandlers.ofString ());

// response
s.code = res.statusCode ();
s.error = true;

if (s.code == 204) {
s.error = false;
s.msg = "Rename successful";
} else {
s.msg = new JSONObject (res.body ()).getString ("message");
}
} catch (Exception e) {
e.printStackTrace ();
s.code = 500;
s.error = true;
s.msg = "Internal server error. Try again later";
}

return s;
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/gateway/soap/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public interface Service {

@WebMethod ResStatus file_move (ReqFileMove args);

@WebMethod ResStatus file_rename (ReqFileRename args);

// sharing

@WebMethod ResStatus share_file (ReqShareFile args);
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/gateway/soap/ServiceImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@

@WebMethod public ResStatus file_move (ReqFileMove args) { return null; }

@WebMethod public ResStatus file_rename (ReqFileRename args)
{
return CtrlFileRename.file_rename (args);
}

// sharing

@WebMethod public ResStatus share_file (ReqShareFile args)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/gateway/soap/request/ReqFileRename.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gateway.soap.request;

import jakarta.validation.constraints.NotNull;
import java.util.UUID;

public class ReqFileRename extends Authorization
{
@NotNull public UUID fileUUID;
@NotNull public String newName;
}
10 changes: 0 additions & 10 deletions app/src/main/java/gateway/soap/request/ReqRenameFile.java

This file was deleted.

70 changes: 70 additions & 0 deletions app/src/test/java/gateway/ITFileManagment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gateway;

import static org.junit.jupiter.api.Assertions.*;

import gateway.config.Config;
import gateway.controller.CtrlAccountRegister;
import gateway.controller.CtrlFileRename;
import gateway.controller.CtrlFileUpload;
import gateway.soap.request.Credentials;
import gateway.soap.request.ReqFileRename;
import gateway.soap.request.ReqFileUpload;
import gateway.soap.response.ResFileNew;
import gateway.soap.response.ResSession;
import gateway.testutils.TestUtilConfig;
import gateway.testutils.TestUtilGenerator;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder (OrderAnnotation.class) class ITFileManagment
{
@BeforeEach void setup () { Config.initializeFromEnv (); }

@Test @Order (1) void renameFile () throws InterruptedException
{
// register

ResSession resR = CtrlAccountRegister.account_register (
new Credentials (UUID.randomUUID ().toString (), "pass"));
assertEquals (201, resR.code, "Register successful");

// upload file

ReqFileUpload reqU = new ReqFileUpload ();
reqU.fileContent = TestUtilGenerator.randomBytes (1);
reqU.fileName = UUID.randomUUID ().toString ();
reqU.location = null;
reqU.token = resR.auth.token;

ResFileNew resU = CtrlFileUpload.file_upload (reqU);
assertEquals (201, resU.code, "File upload success");

Thread.sleep (1000); // wait for upload

// rename file

ReqFileRename reqN = new ReqFileRename ();
reqN.token = resR.auth.token;
reqN.newName = UUID.randomUUID ().toString ();
reqN.fileUUID = resU.fileUUID;
assertEquals (204, CtrlFileRename.file_rename (reqN).code, "Rename success");

// errors

assertEquals (
409, CtrlFileRename.file_rename (reqN).code, "A file with that name already exists");

TestUtilConfig.makeInvalidMetadata ();
assertEquals (500, CtrlFileRename.file_rename (reqN).code, "Can't reach Metadata server");

reqN.token = "invalid-token";
assertEquals (401, CtrlFileRename.file_rename (reqN).code, "Authorization failed");

reqN.newName = null;
assertEquals (400, CtrlFileRename.file_rename (reqN).code, "Field validation failed");
}
}
13 changes: 11 additions & 2 deletions cli-client/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
session = client.service.auth_login({"username": "woynert", "password": "password"})
print(session)

result = client.service.auth_refresh({'token': session.auth.token})
print(result)
res = client.service.auth_refresh({'token': session.auth.token})
print(res)

# upload
# NOTE: Put a file here
Expand Down Expand Up @@ -53,3 +53,12 @@

res.fileContent = f"{len(res.fileContent)} bytes"
print(res)

# rename file

res = client.service.file_rename({
"fileUUID": resU.fileUUID,
"newName": f"woysticker{random.random()}.png",
"token": session.auth.token
})
print(res)
42 changes: 39 additions & 3 deletions docs/spec.openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,44 @@ paths:
/file_move:
put:
tags: ["File"]
description: Move or rename the given file. If `targetDirectoryUUID` is provided the file will be moved. If `newName` is provided the name will be renamed.
description: Moves the given file. If `targetDirectoryUUID` is null the file will be moved to the user's root directory, otherwise it'll be moved to the specified directory.
requestBody:
content:
Object:
schema:
$ref: '#/components/schemas/ReqFileMove'
responses:
"200":
description: The file was moved / renamed.
description: The file was moved.
content:
object:
schema:
$ref: "#/components/schemas/ResStatus"
'default':
description: An error occurred. Returns error code and message. See possible responses at [Metadata API Spec endpoint `PUT /files/rename/{user_uuid}/{file_uuid}` and `PUT /files/move/{user_uuid}/{file_uuid}`](https://github.com/hawks-atlanta/metadata-scala/blob/main/docs/spec.openapi.yaml)
description: An error occurred. Returns error code and message. See possible responses at [Metadata API Spec endpoint `PUT /files/move/{user_uuid}/{file_uuid}`](https://github.com/hawks-atlanta/metadata-scala/blob/main/docs/spec.openapi.yaml)
content:
Object:
schema:
$ref: '#/components/schemas/ResStatus'

/file_rename:
put:
tags: ["File"]
description: Rename the given file.
requestBody:
content:
Object:
schema:
$ref: '#/components/schemas/ReqFileRename'
responses:
"200":
description: The file was renamed.
content:
object:
schema:
$ref: "#/components/schemas/ResStatus"
'default':
description: An error occurred. Returns error code and message. See possible responses at [Metadata API Spec endpoint `PUT /files/rename/{user_uuid}/{file_uuid}`](https://github.com/hawks-atlanta/metadata-scala/blob/main/docs/spec.openapi.yaml)
content:
Object:
schema:
Expand Down Expand Up @@ -610,6 +633,19 @@ components:
targetDirectoryUUID:
type: string
example: 5295d524-aafc-407c-96ed-adae2cd5047a
- $ref: '#/components/schemas/Authorization'

ReqFileRename:
allOf:
- type: object
required:
- fileUUID
- newName
- token
properties:
fileUUID:
type: string
example: 5295d524-aafc-407c-96ed-adae2cd5047a
newName:
type: string
example: picture_new_name.png
Expand Down