From 53954cdc3b41ac6fc896ef620b89c84ff7424fed Mon Sep 17 00:00:00 2001 From: Torekhan Abayev Date: Fri, 3 Feb 2023 19:26:04 +0100 Subject: [PATCH] Fix unit tests --- .../server/rest/StorageBucketTest.java | 6 +- .../server/rest/StorageObjectTest.java | 154 ++++++++++++------ 2 files changed, 108 insertions(+), 52 deletions(-) diff --git a/application/src/test/java/org/unigrid/hedgehog/server/rest/StorageBucketTest.java b/application/src/test/java/org/unigrid/hedgehog/server/rest/StorageBucketTest.java index f3ec3017..28cf183b 100644 --- a/application/src/test/java/org/unigrid/hedgehog/server/rest/StorageBucketTest.java +++ b/application/src/test/java/org/unigrid/hedgehog/server/rest/StorageBucketTest.java @@ -27,7 +27,6 @@ import net.jqwik.api.Property; import net.jqwik.api.Provide; import net.jqwik.api.Example; -import net.jqwik.api.Disabled; import net.jqwik.api.ForAll; import net.jqwik.api.constraints.AlphaChars; import net.jqwik.api.constraints.Size; @@ -95,7 +94,7 @@ public Arbitrary> provideBuckets(@ForAll assertThat(deleteResponse.getLength(), equalTo(mockDeleteResponse.getLength())); assertThat(deleteResponse.getStatus(), equalTo(mockDeleteResponse.getStatus())); - + for (int i = 0; i < allBuckets.size(); i++) { final TestBucket testBucket = new TestBucket(); @@ -109,7 +108,6 @@ public Arbitrary> provideBuckets(@ForAll return Arbitraries.shuffle(testBuckets); } - @Disabled @Property(tries = 5) @SneakyThrows public void shouldBe1(@ForAll("provideBuckets") List testBuckets) { @@ -119,7 +117,6 @@ public void shouldBe1(@ForAll("provideBuckets") List testBuckets) { } @Example - @Disabled @SneakyThrows public void shouldBe2() { RestClient client = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true); @@ -134,7 +131,6 @@ public void shouldBe2() { } @Example - @Disabled @SneakyThrows public void shouldBe3() { RestClient client = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true); diff --git a/application/src/test/java/org/unigrid/hedgehog/server/rest/StorageObjectTest.java b/application/src/test/java/org/unigrid/hedgehog/server/rest/StorageObjectTest.java index c06a50d1..bec80708 100644 --- a/application/src/test/java/org/unigrid/hedgehog/server/rest/StorageObjectTest.java +++ b/application/src/test/java/org/unigrid/hedgehog/server/rest/StorageObjectTest.java @@ -22,11 +22,22 @@ import jakarta.ws.rs.core.Response; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import lombok.AllArgsConstructor; import lombok.SneakyThrows; -import net.jqwik.api.Disabled; +import lombok.Data; +import lombok.NoArgsConstructor; +import net.jqwik.api.Property; +import net.jqwik.api.lifecycle.AfterProperty; +import net.jqwik.api.lifecycle.BeforeProperty; +import net.jqwik.api.Provide; import net.jqwik.api.Example; -import net.jqwik.api.lifecycle.AfterTry; -import net.jqwik.api.lifecycle.BeforeTry; +import net.jqwik.api.ForAll; +import net.jqwik.api.Arbitrary; +import net.jqwik.api.Arbitraries; +import net.jqwik.api.constraints.AlphaChars; +import net.jqwik.api.constraints.StringLength; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import org.unigrid.hedgehog.client.ResponseOddityException; @@ -38,61 +49,111 @@ public class StorageObjectTest extends BaseRestClientTest { S3Mock api; RestClient client; + RestClient clientMock; - String bucket = "testBucket"; - String key = "testObject"; - String copy = "copied"; + String copySuffix = "_copied"; final int MAX_KEYS = 10000; - @BeforeTry + @BeforeProperty public void beforeTry() { api = new S3Mock.Builder().withPort(8001).withInMemoryBackend().build(); api.start(); client = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true); + clientMock = new RestClient(server.getRest().getHostName(), 8001, false); } - @AfterTry + @AfterProperty public void after() { api.shutdown(); client.close(); + clientMock.close(); } - @Example - @Disabled - @SneakyThrows - public void shouldBe1() { - final RestClient clientMock = new RestClient(server.getRest().getHostName(), 8001, false); + @Data + @NoArgsConstructor + @AllArgsConstructor + public static class TestObject { + private String bucket; + private String key; + } + + @Provide + public Arbitrary nameProvider(@ForAll @AlphaChars @StringLength(max = 20, min = 1) String key) { + return Arbitraries.of(key); + } + + @Provide + public Arbitrary bucketConfigurationProvider(@ForAll("nameProvider") String configurationName) { + return Arbitraries.of(new CreateBucketConfiguration(configurationName)); + } + + @Provide + public Arbitrary bucketProvider(@ForAll("nameProvider") String bucketName, + @ForAll("bucketConfigurationProvider") CreateBucketConfiguration config) throws ResponseOddityException { + + RestClient client2 = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true); + RestClient clientMock2 = new RestClient(server.getRest().getHostName(), 8001, false); + + client2.putXml("/bucket/" + bucketName, config); + clientMock2.putXml("/" + bucketName, config); + + client2.close(); + clientMock2.close(); + + return Arbitraries.of(bucketName); + } + + @Provide + public Arbitrary> keyProvider(@ForAll("nameProvider") String content, + @ForAll("nameProvider") String keyName, @ForAll("bucketProvider") String bucket) throws ResponseOddityException { + + RestClient client2 = new RestClient(server.getRest().getHostName(), server.getRest().getPort(), true); + RestClient clientMock2 = new RestClient(server.getRest().getHostName(), 8001, false); - CreateBucketConfiguration config = new CreateBucketConfiguration("TestConfig"); + InputStream inputStream = new ByteArrayInputStream(content.getBytes()); + InputStream inputStream2 = new ByteArrayInputStream(content.getBytes()); - clientMock.putXml("/" + bucket, config); - client.putXml("/bucket/" + bucket, config); + clientMock2.postInputStream("/" + bucket + "/" + keyName, inputStream); + client2.postInputStream("/storage-object/" + bucket + "/" + keyName, inputStream2); - InputStream inputStream = new ByteArrayInputStream("Hello, World!".getBytes()); - InputStream inputStream2 = new ByteArrayInputStream("Hello, World!".getBytes()); + final List testObjects = new ArrayList<>(); + testObjects.add(new TestObject(bucket, keyName)); + + client2.close(); + clientMock2.close(); + + return Arbitraries.shuffle(testObjects); + } + + @Property(tries = 10) + public void shouldBeAbleToCreateObject(@ForAll("nameProvider") String content, @ForAll("nameProvider") String key, + @ForAll("bucketProvider") String bucket) throws ResponseOddityException { + + InputStream inputStream = new ByteArrayInputStream(content.getBytes()); + InputStream inputStream2 = new ByteArrayInputStream(content.getBytes()); Response mockResponse = clientMock.postInputStream("/" + bucket + "/" + key, inputStream); Response response = client.postInputStream("/storage-object/" + bucket + "/" + key, inputStream2); assertThat(response.getStatus(), equalTo(mockResponse.getStatus())); - - clientMock.close(); } - @Example + @Property(tries = 10) @SneakyThrows - public void shouldBe2() { + public void shouldContainInputStream(@ForAll("bucketConfigurationProvider") CreateBucketConfiguration config, + @ForAll("nameProvider") String key, @ForAll("nameProvider") String bucketName) { + try { - client.post("/storage-object/" + bucket + "/" + key, ""); + client.putXml("/bucket/" + bucketName, config); + client.post("/storage-object/" + bucketName + "/" + key, ""); } catch (Exception e) { assertThat(e, isA(ResponseOddityException.class)); } } - @Example - @Disabled + @Property(tries = 10) @SneakyThrows - public void shouldBe3() { + public void shouldListAllObjects(@ForAll("bucketProvider") String bucket) { ListBucketResult response = client.getEntity("/storage-object/list/" + bucket, ListBucketResult.class); assertThat(response.getName(), equalTo(bucket)); @@ -101,14 +162,16 @@ public void shouldBe3() { assertThat(response.getMaxKeys(), equalTo(MAX_KEYS)); } - @Example - @Disabled + @Property(tries = 10) @SneakyThrows - public void shouldBe4() { + public void shouldCopyObject(@ForAll("keyProvider") List testObject) { + String bucket = testObject.get(0).getBucket(); + String key = testObject.get(0).getKey(); + MultivaluedMap headers = new MultivaluedHashMap<>(); headers.add("x-amz-copy-source", "/" + bucket + "/" + key); - Response response = client.putWithHeaders("/storage-object/" + bucket + "/" + copy, headers); + Response response = client.putWithHeaders("/storage-object/" + bucket + "/" + key + copySuffix, headers); CopyObjectResult result = response.readEntity(CopyObjectResult.class); assertThat(response.getStatus(), equalTo(200)); @@ -116,35 +179,32 @@ public void shouldBe4() { assertThat(result.getLastModified().toString(), is(notNullValue())); } - @Example + @Property(tries = 10) @SneakyThrows - public void shouldBe5() { + public void shouldContainHeader(@ForAll("nameProvider") String bucketName, @ForAll("nameProvider") String key) { MultivaluedMap headers = new MultivaluedHashMap<>(); try { - client.putWithHeaders("/storage-object/testBukcet/testObjecyt", headers); + client.putWithHeaders("/storage-object/" + bucketName + "/" + key, headers); } catch (Exception e) { assertThat(e.getMessage(), containsString("400 Bad Request")); } } - @Example - @Disabled + @Property(tries = 10) @SneakyThrows - public void shouldBe6() { - Response response = client.get("/storage-object/" + bucket + "/" + key); + public void shouldGetAnObject(@ForAll("keyProvider") List testObject) { + Response response = client.get("/storage-object/" + testObject.get(0).getBucket() + "/" + + testObject.get(0).getKey()); assertThat(response.getStatus(), equalTo(200)); - // file should not be empty - assertThat(response.getLength(), greaterThan(0)); } - @Example - @Disabled + @Property(tries = 10) @SneakyThrows - public void shouldBe7() { + public void shouldObjectExist(@ForAll("nameProvider") String bucketName) { try { - client.get("/storage-object/" + bucket + "/testtest"); + client.get("/storage-object/" + bucketName + "/testtest"); } catch (Exception e) { assertThat(e, isA(ResponseOddityException.class)); assertThat(e.getMessage(), containsString("404 Not Found")); @@ -152,14 +212,14 @@ public void shouldBe7() { } @Example - @Disabled @SneakyThrows - public void shouldBe8() { - Response response = client.delete("/storage-object/" + bucket + "/" + copy); + public void shouldDeleteAnObject(@ForAll("keyProvider") List testObject) { + Response response = client.delete("/storage-object/" + testObject.get(0).getBucket() + "/" + + testObject.get(0).getKey()); assertThat(response.getLength(), equalTo(-1)); assertThat(response.getStatus(), equalTo(204)); - client.delete("/bucket/" + bucket); + client.delete("/bucket/" + testObject.get(0).getBucket()); } }