diff --git a/.github/linters/sun_checks.xml b/.github/linters/sun_checks.xml index 134f0cc..6ad4c4a 100644 --- a/.github/linters/sun_checks.xml +++ b/.github/linters/sun_checks.xml @@ -73,7 +73,7 @@ - + diff --git a/.github/workflows/lintChanges.yml b/.github/workflows/lintChanges.yml index e173f9d..cb08089 100644 --- a/.github/workflows/lintChanges.yml +++ b/.github/workflows/lintChanges.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout Repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 03e0bbb..710766f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout Repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up JDK uses: actions/setup-java@v2 with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7376619..c6b5f9e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -22,7 +22,7 @@ jobs: steps: - name: Checkout Repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up JDK uses: actions/setup-java@v2 with: diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b87e87..408a3a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### Unreleased ### +* Migrated test suite from JUnit4 to JUnit 5 +* Upgrade okhttp to 4.12.0 as a security update + ### 1.0.0 / 2022-12-14 ### #### Major Release Warning: This version includes breaking changes and some experimental features, please keep that in mind when using it. diff --git a/build.gradle b/build.gradle index 634e369..5c65e03 100644 --- a/build.gradle +++ b/build.gradle @@ -25,14 +25,21 @@ dependencies { implementation 'org.jetbrains:annotations:23.0.0' implementation 'io.tus.java.client:tus-java-client:0.4.5' implementation 'joda-time:joda-time:2.12.2' - implementation 'com.squareup.okhttp3:okhttp:4.10.0' + implementation 'com.squareup.okhttp3:okhttp:4.12.0' implementation 'org.json:json:20231013' implementation 'commons-codec:commons-codec:1.15' implementation 'io.socket:socket.io-client:2.1.0' - testImplementation 'junit:junit:4.13.2' - testImplementation 'org.mock-server:mockserver-junit-rule:5.15.0' + testImplementation 'org.mock-server:mockserver-junit-jupiter-no-dependencies:5.15.0' testImplementation 'org.mockito:mockito-core:4.8.0' + testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1' +} + +test { + useJUnitPlatform() + testLogging { + events "passed", "skipped", "failed" + } } tasks.register('sourcesJar', Jar) { diff --git a/src/main/java/com/transloadit/sdk/Assembly.java b/src/main/java/com/transloadit/sdk/Assembly.java index 5d790ec..58b6b5f 100644 --- a/src/main/java/com/transloadit/sdk/Assembly.java +++ b/src/main/java/com/transloadit/sdk/Assembly.java @@ -31,7 +31,7 @@ import java.util.UUID; // CHECKSTYLE:OFF import io.tus.java.client.TusUploader; -// CHECKTYLE:ON +// CHECKSTYLE:ON /** @@ -819,7 +819,7 @@ protected String obtainUploadUrlSuffix() { } /** - * Returns the current Thread List + * Returns the current Thread List. * @return List of Type TusUploadRunnable */ protected ArrayList getThreadList() { diff --git a/src/test/java/com/transloadit/sdk/AssemblyMultiThreadingTest.java b/src/test/java/com/transloadit/sdk/AssemblyMultiThreadingTest.java index 4d3c5aa..26fbd42 100644 --- a/src/test/java/com/transloadit/sdk/AssemblyMultiThreadingTest.java +++ b/src/test/java/com/transloadit/sdk/AssemblyMultiThreadingTest.java @@ -4,13 +4,15 @@ import com.transloadit.sdk.exceptions.RequestException; import com.transloadit.sdk.response.AssemblyResponse; import org.json.JSONObject; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.mockserver.client.MockServerClient; -import org.mockserver.junit.MockServerRule; +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.junit.jupiter.MockServerSettings; import org.mockserver.matchers.Times; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; @@ -21,23 +23,18 @@ import java.nio.file.Files; import java.nio.file.Paths; -import static org.junit.Assert.assertEquals; import static org.mockserver.model.RegexBody.regex; /** * Unit test for the multi threading features of {@link Assembly Assembly.class}. */ +@ExtendWith(MockServerExtension.class) // MockServerExtension is used to start and stop the MockServer +@MockServerSettings(ports = MockHttpService.PORT) // MockServerSettings is used to define the port of the MockServer public class AssemblyMultiThreadingTest extends MockHttpService { - /** - * MockServer can be run using the MockServerRule. - */ - @Rule - public MockServerRule mockServerRule = new MockServerRule(this, true, PORT); - /** * MockServerClient makes HTTP requests to a MockServer instance. */ - private MockServerClient mockServerClient; + private final MockServerClient mockServerClient = new MockServerClient("localhost", PORT); /** * Links to {@link Assembly} instance to perform the tests on. @@ -53,7 +50,7 @@ public class AssemblyMultiThreadingTest extends MockHttpService { * individual test * and resets the mockServerClient. Also sets {@link AssemblyMultiThreadingTest#assemblyFinished} {@code = false}. */ - @Before + @BeforeEach public void setUp() throws Exception { assembly = new MockTusAssemblyMultiThreading(transloadit); assembly.wipeAssemblyID(); @@ -189,8 +186,8 @@ public void onAssemblyResultFinished(String stepName, JSONObject result) { mockServerClient.verify(HttpRequest.request() .withPath("/resumable/files").withMethod("POST"), VerificationTimes.atLeast(3)); - assertEquals(response.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); - assertEquals(response.json().get("ok"), "ASSEMBLY_UPLOADING"); + Assertions.assertEquals(response.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); + Assertions.assertEquals(response.json().get("ok"), "ASSEMBLY_UPLOADING"); } /** @@ -246,7 +243,7 @@ public void onAssemblyResultFinished(String stepName, JSONObject result) { } }; assembly.setRunnableAssemblyListener(listener); - assertEquals(listener, assembly.getRunnableAssemblyListener()); + Assertions.assertEquals(listener, assembly.getRunnableAssemblyListener()); } /** @@ -286,7 +283,7 @@ public void abortUploads() throws LocalOperationException, RequestException, IOE Mockito.verify(spyListener).onError(exceptionArgumentCaptor.capture()); String errorMessage = "Uploads aborted"; String exceptionMessage = exceptionArgumentCaptor.getValue().getMessage(); - assertEquals(exceptionMessage, errorMessage); + Assertions.assertEquals(exceptionMessage, errorMessage); } /** @@ -300,7 +297,7 @@ public void pauseUploads() throws IOException, LocalOperationException, RequestE String uploadSize = "" + new File("LICENSE").length(); - assertEquals(uploadSize, "1077"); // verify, that test sizes can work + Assertions.assertEquals(uploadSize, "1077"); // verify, that test sizes can work String uploadChunkSize = "1"; assembly = new MockTusAssemblyMultiThreading(transloadit); assembly.wipeAssemblyID(); diff --git a/src/test/java/com/transloadit/sdk/AssemblyTest.java b/src/test/java/com/transloadit/sdk/AssemblyTest.java index ee8674a..9a48156 100644 --- a/src/test/java/com/transloadit/sdk/AssemblyTest.java +++ b/src/test/java/com/transloadit/sdk/AssemblyTest.java @@ -4,41 +4,38 @@ import com.transloadit.sdk.exceptions.RequestException; import com.transloadit.sdk.response.AssemblyResponse; import org.json.JSONObject; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockserver.client.MockServerClient; -import org.mockserver.junit.MockServerRule; +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.junit.jupiter.MockServerSettings; import org.mockserver.matchers.Times; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; + import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.Objects; -import static junit.framework.TestCase.assertFalse; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.mockserver.model.RegexBody.regex; /** * Unit test for {@link Assembly} class. Api-Responses are simulated by mocking the server's response. */ +@ExtendWith(MockServerExtension.class) // MockServerExtension is used to start and stop the MockServer +@MockServerSettings(ports = MockHttpService.PORT) // MockServerSettings is used to define the port of the MockServer public class AssemblyTest extends MockHttpService { - /** - * MockServer can be run using the MockServerRule. - */ - @Rule - public MockServerRule mockServerRule = new MockServerRule(this, true, PORT); - /** * MockServerClient makes HTTP requests to a MockServer instance. */ - private MockServerClient mockServerClient; + private final MockServerClient mockServerClient = new MockServerClient("localhost", PORT); /** * Links to {@link Assembly} instance to perform the tests on. @@ -50,11 +47,11 @@ public class AssemblyTest extends MockHttpService { private boolean assemblyFinished; /** - * Assings a new {@link Assembly} instance to the {@link AssemblyTest#assembly} variable before each individual test + * Assigns a new {@link Assembly} instance to the {@link AssemblyTest#assembly} variable before each individual test * and resets the mockServerClient. Also sets {@link AssemblyTest#assemblyFinished} {@code = false}. */ - @Before - public void setUp() throws Exception { + @BeforeEach + public void setUp() { assembly = newAssemblyWithoutID(); assemblyFinished = false; mockServerClient.reset(); @@ -69,42 +66,42 @@ public void addFile() { File file = new File("LICENSE"); assembly.addFile(file, "file_name"); - assertEquals(file, assembly.files.get("file_name")); + Assertions.assertEquals(file, assembly.files.get("file_name")); } /** * Performs a similar test to {@link Assembly#addFile(File, String)}, except that a FileStream is added. - * @throws Exception if File cannot be created. + * @throws IOException if File cannot be created. */ @Test - public void addInputStreamFile() throws FileNotFoundException { - InputStream file = new FileInputStream(new File("LICENSE")); + public void addInputStreamFile() throws IOException { + InputStream file = Files.newInputStream(new File("LICENSE").toPath()); assembly.addFile(file, "file_name"); - assertEquals(file, assembly.fileStreams.get("file_name")); + Assertions.assertEquals(file, assembly.fileStreams.get("file_name")); } /** - * Performs a double test, adding a file and a FileStream wtih the same name to the {@link Assembly} instance. - * Both are added by calling {@link Assembly#addFile}. Test passes if only the later added {@link File} can be found + * Performs a double test, adding a file and a FileStream with the same name to the {@link Assembly} instance. + * Both are added by calling {@link Assembly#addFile}. Test passes if only the latter added {@link File} can be found * in the {@link Assembly} instance. - * @throws Exception + * @throws IOException if File cannot be created. */ @Test - public void addCombinedFiles() throws Exception { - InputStream fileStream = new FileInputStream(new File("LICENSE")); + public void addCombinedFiles() throws IOException { + InputStream fileStream = Files.newInputStream(new File("LICENSE").toPath()); assembly.addFile(fileStream, "file_name"); File file = new File("LICENSE"); assembly.addFile(file, "file_name"); - assertFalse(assembly.fileStreams.containsKey("file_name")); - assertEquals(file, assembly.files.get("file_name")); + Assertions.assertFalse(assembly.fileStreams.containsKey("file_name")); + Assertions.assertEquals(file, assembly.files.get("file_name")); } /** * Performs a test if a file can successfully be removed from an Assembly with {@link Assembly#removeFile(String)}. - * Therefore a file gets added to the Assembly and gets removed afterwards. Tests passes if the file's name + * Therefore, a file gets added to the Assembly and gets removed afterward. Tests passes if the file's name * cannot be found in {@link Assembly#files}. */ @Test @@ -112,10 +109,10 @@ public void removeFile() { File file = new File("LICENSE"); assembly.addFile(file, "file_name"); - assertTrue(assembly.files.containsKey("file_name")); + Assertions.assertTrue(assembly.files.containsKey("file_name")); assembly.removeFile("file_name"); - assertFalse(assembly.files.containsKey("file_name")); + Assertions.assertFalse(assembly.files.containsKey("file_name")); } /** @@ -127,8 +124,9 @@ public void removeFile() { *
  • The {@link Assembly#save(boolean)} methods parameter {@code isResumable = false}, indicating that * the {@link io.tus.java.client.TusUpload TUS Uploader} should not be used.
  • * - * @throws Exception if communication with the server goes wrong, if building the request goes wrong or - * if Test resource "assembly_executing.json" is missing. + * @throws IOException if Test resource "assembly_executing.json" is missing. + * @throws LocalOperationException if building the request goes wrong + * @throws RequestException if communication with the server goes wrong. */ @Test public void save() throws IOException, LocalOperationException, RequestException { @@ -141,7 +139,7 @@ public void save() throws IOException, LocalOperationException, RequestException assembly.addFile(new File("LICENSE"), "file_name"); AssemblyResponse savedAssembly = assembly.save(false); - assertEquals(savedAssembly.json().get("ok"), "ASSEMBLY_EXECUTING"); + Assertions.assertEquals(savedAssembly.json().get("ok"), "ASSEMBLY_EXECUTING"); } /** @@ -158,10 +156,10 @@ public void saveWithInputStream() throws Exception { .withBody(regex("[\\w\\W]*Permission is hereby granted, free of charge[\\w\\W]*"))) .respond(HttpResponse.response().withBody(getJson("assembly.json"))); - assembly.addFile(new FileInputStream(new File("LICENSE")), "file_name"); + assembly.addFile(Files.newInputStream(new File("LICENSE").toPath()), "file_name"); AssemblyResponse savedAssembly = assembly.save(false); - assertEquals(savedAssembly.json().get("ok"), "ASSEMBLY_COMPLETED"); + Assertions.assertEquals(savedAssembly.json().get("ok"), "ASSEMBLY_COMPLETED"); } /** @@ -187,7 +185,7 @@ public void saveTillComplete() throws Exception { assembly.setShouldWaitForCompletion(true); AssemblyResponse savedAssembly = assembly.save(false); - assertEquals(savedAssembly.json().get("ok"), "ASSEMBLY_COMPLETED"); + Assertions.assertEquals(savedAssembly.json().get("ok"), "ASSEMBLY_COMPLETED"); } /** @@ -211,14 +209,14 @@ public void saveWithTus() throws Exception { .respond(HttpResponse.response().withBody(getJson("resumable_assembly.json"))); AssemblyResponse resumableAssembly = assembly.save(true); - assertEquals(resumableAssembly.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); - assertEquals(resumableAssembly.json().get("ok"), "ASSEMBLY_UPLOADING"); + Assertions.assertEquals(resumableAssembly.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); + Assertions.assertEquals(resumableAssembly.json().get("ok"), "ASSEMBLY_UPLOADING"); } /** * This test verifies the functionality of {@link Assembly#save(boolean)}. It is identical to * {@link AssemblyTest#saveWithTus()} except it listens to the Socket and waits until the execution of the - * {@link Assembly} is finished. Therefore it implements an {@link AssemblyListener} and verifies the needed + * {@link Assembly} is finished. Therefore, it implements an {@link AssemblyListener} and verifies the needed * POST and GET requests to the server. * @throws Exception if communication with the server goes wrong, if building the request goes wrong or * if Test resources "resumable_assembly.json" or "resumable_assembly_complete.json" are missing. @@ -231,8 +229,8 @@ public void saveWithTusTillSocketComplete() throws Exception { @Override public void onAssemblyFinished(AssemblyResponse response) { assemblyFinished = true; - assertEquals(response.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); - assertEquals(response.json().get("ok"), "ASSEMBLY_COMPLETED"); + Assertions.assertEquals(response.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); + Assertions.assertEquals(response.json().get("ok"), "ASSEMBLY_COMPLETED"); } @Override @@ -291,18 +289,18 @@ public void onAssemblyResultFinished(String stepName, JSONObject result) { AssemblyResponse response = assembly.save(true); - assertEquals(response.json().get("ok"), "ASSEMBLY_UPLOADING"); - assertFalse(assemblyFinished); - assertTrue(assembly.emitted.containsKey("assembly_connect")); + Assertions.assertEquals(response.json().get("ok"), "ASSEMBLY_UPLOADING"); + Assertions.assertFalse(assemblyFinished); + Assertions.assertTrue(assembly.emitted.containsKey("assembly_connect")); // emit that assembly is complete assembly.getSocket("").emit("assembly_finished"); - assertTrue(assemblyFinished); + Assertions.assertTrue(assemblyFinished); } /** * This Test verifies the functionality of {@link Assembly#save(boolean)}. It is identical to * {@link AssemblyTest#saveWithTus()}, except it waits until the {@link Assembly} execution is finished. - * This is determined by by observing the {@link AssemblyResponse} status. + * This is determined by observing the {@link AssemblyResponse} status. * @see Assembly#shouldWaitWithoutSocket() * @throws Exception if communication with the server goes wrong, if building the request goes wrong or * if Test resources "resumable_assembly.json" or "resumable_assembly_complete.json" are missing. @@ -325,8 +323,8 @@ public void saveWithTusTillComplete() throws Exception { .respond(HttpResponse.response().withBody(getJson("resumable_assembly_complete.json"))); AssemblyResponse resumableAssembly = assembly.save(true); - assertEquals(resumableAssembly.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); - assertEquals(resumableAssembly.json().get("ok"), "ASSEMBLY_COMPLETED"); + Assertions.assertEquals(resumableAssembly.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); + Assertions.assertEquals(resumableAssembly.json().get("ok"), "ASSEMBLY_COMPLETED"); } /** @@ -338,7 +336,7 @@ public void saveWithTusTillComplete() throws Exception { @Test public void saveWithInputStreamAndTus() throws Exception { MockTusAssembly assembly = new MockTusAssembly(transloadit); - assembly.addFile(new FileInputStream(new File("LICENSE")), "file_name"); + assembly.addFile(Files.newInputStream(new File("LICENSE").toPath()), "file_name"); mockServerClient.when(HttpRequest.request() .withPath("/assemblies") @@ -348,7 +346,7 @@ public void saveWithInputStreamAndTus() throws Exception { .respond(HttpResponse.response().withBody(getJson("resumable_assembly.json"))); AssemblyResponse resumableAssembly = assembly.save(true); - assertEquals(resumableAssembly.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); + Assertions.assertEquals(resumableAssembly.json().get("assembly_id"), "02ce6150ea2811e6a35a8d1e061a5b71"); } /** @@ -373,7 +371,7 @@ public void testRetryRateLimit() throws Exception { assembly.addFile(new File("LICENSE"), "file_name"); AssemblyResponse savedAssembly = assembly.save(false); // check if assembly was successfully retried - assertEquals(savedAssembly.json().get("ok"), "ASSEMBLY_EXECUTING"); + Assertions.assertEquals(savedAssembly.json().get("ok"), "ASSEMBLY_EXECUTING"); } @@ -383,7 +381,7 @@ public void testRetryRateLimit() throws Exception { @Test public void getAssemblyId() throws LocalOperationException { assembly.setAssemblyId("68fffff5474d40b8bf7a294cfce4aba5"); - assertEquals("68fffff5474d40b8bf7a294cfce4aba5", assembly.getClientSideGeneratedAssemblyID()); + Assertions.assertEquals("68fffff5474d40b8bf7a294cfce4aba5", assembly.getClientSideGeneratedAssemblyID()); } /** @@ -395,14 +393,11 @@ public void setAssemblyId() throws LocalOperationException { String uuidShort = "6859bd25474d"; String uuidLong = "6859bd25474d40b8bf7a294cfce4aba56859bd25474d40b8bf7a294cfce4aba5"; String uuidWrongChar = "6859bd25474d40b8bf-a294cfce4aba5"; - Assert.assertThrows(LocalOperationException.class, () -> { - assembly.setAssemblyId(uuidShort); }); - Assert.assertThrows(LocalOperationException.class, () -> { - assembly.setAssemblyId(uuidWrongChar); }); - Assert.assertThrows(LocalOperationException.class, () -> { - assembly.setAssemblyId(uuidLong); }); + Assertions.assertThrows(LocalOperationException.class, () -> assembly.setAssemblyId(uuidShort)); + Assertions.assertThrows(LocalOperationException.class, () -> assembly.setAssemblyId(uuidWrongChar)); + Assertions.assertThrows(LocalOperationException.class, () -> assembly.setAssemblyId(uuidLong)); assembly.setAssemblyId(uuid); - assertEquals(assembly.getClientSideGeneratedAssemblyID(), uuid); + Assertions.assertEquals(assembly.getClientSideGeneratedAssemblyID(), uuid); } /** @@ -410,13 +405,13 @@ public void setAssemblyId() throws LocalOperationException { * Test succeeds if a certain pattern is generated and every run generates a different String. */ @Test - public void generateAssemblyID() throws LocalOperationException { + public void generateAssemblyID() { String assemblyID1 = assembly.generateAssemblyID(); String assemblyID2 = assembly.generateAssemblyID(); - assertTrue(assemblyID1.matches("[a-f0-9]{32}")); - assertTrue(assemblyID2.matches("[a-f0-9]{32}")); - assertFalse(assemblyID1.equals(assemblyID2)); + Assertions.assertTrue(assemblyID1.matches("[a-f0-9]{32}")); + Assertions.assertTrue(assemblyID2.matches("[a-f0-9]{32}")); + Assertions.assertNotEquals(assemblyID1, assemblyID2); } /** @@ -424,38 +419,38 @@ public void generateAssemblyID() throws LocalOperationException { */ @Test public void obtainUploadUrlSuffix() throws LocalOperationException { - assertEquals("/assemblies", assembly.obtainUploadUrlSuffix()); + Assertions.assertEquals("/assemblies", assembly.obtainUploadUrlSuffix()); String assemblyID = assembly.generateAssemblyID(); assembly.setAssemblyId(assemblyID); - assertEquals("/assemblies/" + assemblyID, assembly.obtainUploadUrlSuffix()); + Assertions.assertEquals("/assemblies/" + assemblyID, assembly.obtainUploadUrlSuffix()); } /** * Determines if the correct fileSizes are returned by {@link Assembly#getUploadSize()}. - * @throws IOException + * @throws IOException if File cannot be created. */ @Test public void getUploadSize() throws IOException { - File file1 = new File(getClass().getResource("/__files/assembly_executing.json").getFile()); - File file2 = new File(getClass().getResource("/__files/cancel_assembly.json").getFile()); + File file1 = new File(Objects.requireNonNull(getClass().getResource("/__files/assembly_executing.json")).getFile()); + File file2 = new File(Objects.requireNonNull(getClass().getResource("/__files/cancel_assembly.json")).getFile()); assembly.addFile(file1); assembly.addFile(file2); long combinedFilesize = Files.size(file1.toPath()) + Files.size(file2.toPath()); - assertEquals(combinedFilesize, assembly.getUploadSize()); + Assertions.assertEquals(combinedFilesize, assembly.getUploadSize()); } /** * Determines if correct number of upload files is determined. - * @throws FileNotFoundException + * @throws FileNotFoundException if File cannot be found. */ @Test public void getNumberOfFiles() throws FileNotFoundException { - File file1 = new File(getClass().getResource("/__files/assembly_executing.json").getFile()); - FileInputStream file2 = new FileInputStream(getClass().getResource("/__files/cancel_assembly.json").getFile()); + File file1 = new File(Objects.requireNonNull(getClass().getResource("/__files/assembly_executing.json")).getFile()); + FileInputStream file2 = new FileInputStream(Objects.requireNonNull(getClass().getResource("/__files/cancel_assembly.json")).getFile()); assembly.addFile(file1); assembly.addFile(file2); - assertEquals(2, assembly.getNumberOfFiles()); + Assertions.assertEquals(2, assembly.getNumberOfFiles()); } } diff --git a/src/test/java/com/transloadit/sdk/MockHttpService.java b/src/test/java/com/transloadit/sdk/MockHttpService.java index 785bf42..ea63ea2 100644 --- a/src/test/java/com/transloadit/sdk/MockHttpService.java +++ b/src/test/java/com/transloadit/sdk/MockHttpService.java @@ -10,9 +10,8 @@ public class MockHttpService { //CHECKSTYLE:OFF // Checkstyle was turned off here to enable the upper case variable name of the "PORT" variable. - protected final int PORT = 9040; - protected final Transloadit transloadit = new Transloadit("KEY", "SECRET", - "http://localhost:" + PORT); + public final static int PORT = 9040; + protected final Transloadit transloadit = new Transloadit("KEY", "SECRET", "http://localhost:9040"); //CHECKSTYLE:ON /** diff --git a/src/test/java/com/transloadit/sdk/OptionsBuilderTest.java b/src/test/java/com/transloadit/sdk/OptionsBuilderTest.java index 8d054e2..6e95608 100644 --- a/src/test/java/com/transloadit/sdk/OptionsBuilderTest.java +++ b/src/test/java/com/transloadit/sdk/OptionsBuilderTest.java @@ -1,16 +1,13 @@ package com.transloadit.sdk; import org.json.JSONObject; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; /** * Unit test for {@link OptionsBuilder} class. Options built by the OptionsBuilder class are verified with @@ -25,7 +22,7 @@ public class OptionsBuilderTest { /** * Assings a new {@link OptionsBuilder} instance to the optionsBuilder variable before each individual test. */ - @Before + @BeforeEach public void setUp() { optionsBuilder = new OptionsBuilder(); optionsBuilder.steps = new Steps(); @@ -43,8 +40,8 @@ public void addStep() { optionsBuilder.addStep("encode", "/video/encode", new HashMap()); optionsBuilder.addStep("noRobotName", new HashMap()); - assertEquals(optionsBuilder.steps.getStep("encode").robot, "/video/encode"); - assertNotNull(optionsBuilder.steps.getStep("noRobotName")); + Assertions.assertEquals(optionsBuilder.steps.getStep("encode").robot, "/video/encode"); + Assertions.assertNotNull(optionsBuilder.steps.getStep("noRobotName")); } /** @@ -57,10 +54,10 @@ public void addStep() { @Test public void removeStep() { optionsBuilder.addStep("encode", "/video/encode", new HashMap()); - assertTrue(optionsBuilder.steps.all.containsKey("encode")); + Assertions.assertTrue(optionsBuilder.steps.all.containsKey("encode")); optionsBuilder.removeStep("encode"); - assertFalse(optionsBuilder.steps.all.containsKey("encode")); + Assertions.assertFalse(optionsBuilder.steps.all.containsKey("encode")); } /** @@ -74,7 +71,7 @@ public void addOptions() { options.put("red", "color"); optionsBuilder.addOptions(options); - assertEquals(options, optionsBuilder.options); + Assertions.assertEquals(options, optionsBuilder.options); } /** @@ -83,7 +80,7 @@ public void addOptions() { @Test public void addOption() { optionsBuilder.addOption("foo", "bar"); - assertEquals(optionsBuilder.options.get("foo"), "bar"); + Assertions.assertEquals(optionsBuilder.options.get("foo"), "bar"); } /** @@ -100,15 +97,15 @@ public void addFieldAndAddFields() { optionsBuilder.addField("foo", "foo"); optionsBuilder.addField("foo", "bar"); // this should overwrite the value - assertTrue(optionsBuilder.options.containsKey("fields")); - assertTrue(optionsBuilder.options.get("fields") instanceof JSONObject); + Assertions.assertTrue(optionsBuilder.options.containsKey("fields")); + Assertions.assertInstanceOf(JSONObject.class, optionsBuilder.options.get("fields")); JSONObject fields = (JSONObject) optionsBuilder.options.get("fields"); - assertEquals(fields.get("foo"), "bar"); // test overwrite - assertEquals(fields.get("baz"), "qux"); - assertTrue(fields.has("needle")); // test if older entries are preserved - assertFalse(fields.has("haystack")); // test if key - value are not interchanged + Assertions.assertEquals(fields.get("foo"), "bar"); // test overwrite + Assertions.assertEquals(fields.get("baz"), "qux"); + Assertions.assertTrue(fields.has("needle")); // test if older entries are preserved + Assertions.assertFalse(fields.has("haystack")); // test if key - value are not interchanged } } diff --git a/src/test/java/com/transloadit/sdk/RequestTest.java b/src/test/java/com/transloadit/sdk/RequestTest.java index b9f6dc4..9774d6c 100644 --- a/src/test/java/com/transloadit/sdk/RequestTest.java +++ b/src/test/java/com/transloadit/sdk/RequestTest.java @@ -2,12 +2,16 @@ import com.transloadit.sdk.exceptions.LocalOperationException; import com.transloadit.sdk.exceptions.RequestException; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockserver.client.MockServerClient; -import org.mockserver.junit.MockServerRule; + +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.junit.jupiter.MockServerSettings; import org.mockserver.matchers.Times; import org.mockserver.model.HttpRequest; @@ -17,35 +21,29 @@ //CHECKSTYLE:OFF import java.util.Map; // Suppress warning as the Map import is needed for the JavaDoc Comments -import static org.junit.Assert.*; import static org.mockserver.model.HttpError.error; //CHECKSTYLE:ON /** * Unit test for {@link Request} class. Api-Responses are simulated by mocking the server's response. */ +@ExtendWith(MockServerExtension.class) // MockServerExtension is used to start and stop the MockServer +@MockServerSettings(ports = MockHttpService.PORT) // MockServerSettings is used to define the port of the MockServer public class RequestTest extends MockHttpService { /** * Links to {@link Request} instance to perform the tests on. */ private Request request; - - /** - * MockServer can be run using the MockServerRule. - */ - @Rule - public MockServerRule mockServerRule = new MockServerRule(this, true, PORT); - /** * MockServerClient makes HTTP requests to a MockServer instance. */ - private MockServerClient mockServerClient; + private final MockServerClient mockServerClient = new MockServerClient("localhost", MockHttpService.PORT); /** * Assings a new {@link Request} instance to Request variable before each individual test and resets * the mockServerClient. */ - @Before + @BeforeEach public void setUp() throws Exception { request = new Request(transloadit); mockServerClient.reset(); @@ -121,15 +119,15 @@ public void qualifiedForRetry() { transloadit2.setRetryAttemptsRequestException(5); // Test if it works with defined errors Request newRequest = new Request(transloadit2); Exception e = new SocketTimeoutException("connect timed out"); - assertTrue(newRequest.qualifiedForRetry(e)); - assertEquals(4, newRequest.retryAttemptsRequestExceptionLeft); // tests counting logic + Assertions.assertTrue(newRequest.qualifiedForRetry(e)); + Assertions.assertEquals(4, newRequest.retryAttemptsRequestExceptionLeft); // tests counting logic Exception e2 = new ArrayStoreException("foo bar"); // shouldn't work here - assertFalse(newRequest.qualifiedForRetry(e2)); + Assertions.assertFalse(newRequest.qualifiedForRetry(e2)); transloadit2.setRetryAttemptsRequestException(0); newRequest = new Request(transloadit2); - assertFalse(newRequest.qualifiedForRetry(e)); + Assertions.assertFalse(newRequest.qualifiedForRetry(e)); } /** @@ -188,7 +186,7 @@ public void getNonce() { int keyLength = 256; String nonce = request.getNonce(cipher, keyLength); - assertEquals(44, nonce.length()); + Assertions.assertEquals(44, nonce.length()); } /** @@ -203,7 +201,7 @@ public void delayBeforeRetry() throws LocalOperationException { int timeout = request.delayBeforeRetry(); endTime = System.currentTimeMillis(); int delta = (int) (endTime - startTime); - assertTrue(delta >= timeout); + Assertions.assertTrue(delta >= timeout); } } diff --git a/src/test/java/com/transloadit/sdk/StepsTest.java b/src/test/java/com/transloadit/sdk/StepsTest.java index cdfcbfb..c537855 100644 --- a/src/test/java/com/transloadit/sdk/StepsTest.java +++ b/src/test/java/com/transloadit/sdk/StepsTest.java @@ -1,14 +1,12 @@ package com.transloadit.sdk; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; /** * Unit test for {@link Steps} class. @@ -22,7 +20,7 @@ public class StepsTest { /** * Assings a new {@link Steps} instance to Steps variable before each individual test. */ - @Before + @BeforeEach public void setUp() { steps = new Steps(); } @@ -34,14 +32,14 @@ public void setUp() { @Test public void addStepGetStep() { steps.addStep("encode", "/video/encode", new HashMap()); - assertEquals(steps.getStep("encode").robot, "/video/encode"); + Assertions.assertEquals(steps.getStep("encode").robot, "/video/encode"); HashMap stepOptions = new HashMap(); stepOptions.put("width", 1920); stepOptions.put("height", 720); steps.addStep("video", stepOptions); - assertEquals(steps.getStep("video").options, stepOptions); + Assertions.assertEquals(steps.getStep("video").options, stepOptions); } /** @@ -53,9 +51,9 @@ public void addStepGetStep() { public void removeStep() { steps.addStep("encode", "/video/encode", new HashMap()); - assertTrue(steps.all.containsKey("encode")); + Assertions.assertTrue(steps.all.containsKey("encode")); steps.removeStep("encode"); - assertFalse(steps.all.containsKey("encode")); + Assertions.assertFalse(steps.all.containsKey("encode")); } /** @@ -76,7 +74,7 @@ public void toMap() { controlMap.put("encode", encodeStep); controlMap.put("thumbs", thumbStep); - assertEquals(controlMap, steps.toMap()); + Assertions.assertEquals(controlMap, steps.toMap()); } } diff --git a/src/test/java/com/transloadit/sdk/TemplateTest.java b/src/test/java/com/transloadit/sdk/TemplateTest.java index 9ff4ad7..55b3c6b 100644 --- a/src/test/java/com/transloadit/sdk/TemplateTest.java +++ b/src/test/java/com/transloadit/sdk/TemplateTest.java @@ -3,41 +3,38 @@ import com.transloadit.sdk.exceptions.LocalOperationException; import com.transloadit.sdk.exceptions.RequestException; import com.transloadit.sdk.response.Response; -import org.junit.Rule; -import org.junit.Test; - +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockserver.client.MockServerClient; -import org.mockserver.junit.MockServerRule; +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.junit.jupiter.MockServerSettings; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; import java.io.IOException; -import static org.junit.Assert.assertEquals; import static org.mockserver.model.RegexBody.regex; /** * Unit test for {@link Template} class. Api-Responses are simulated by mocking the server's response. */ +@ExtendWith(MockServerExtension.class) // MockServerExtension is used to start and stop the MockServer +@MockServerSettings(ports = MockHttpService.PORT) // MockServerSettings is used to define the port of the MockServer public class TemplateTest extends MockHttpService { - /** - * MockServer can be run using the MockServerRule. - */ - @Rule - public MockServerRule mockServerRule = new MockServerRule(this, true, PORT); - /** - * MockServerClient makes HTTP requests to a MockServer instance. - */ - private MockServerClient mockServerClient; + + private final MockServerClient mockServerClient = new MockServerClient("localhost", PORT); /** * Tests if a new Template returns its correct name by calling {@link Template#getName()}. * The name gets defined by its instantiation. */ + + @Test public void getName() { Template template = transloadit.newTemplate("foo"); - assertEquals(template.getName(), "foo"); + Assertions.assertEquals(template.getName(), "foo"); } /** @@ -47,10 +44,10 @@ public void getName() { @Test public void setName() { Template template = transloadit.newTemplate("foo"); - assertEquals(template.getName(), "foo"); + Assertions.assertEquals(template.getName(), "foo"); template.setName("bar"); - assertEquals(template.getName(), "bar"); + Assertions.assertEquals(template.getName(), "bar"); } /** @@ -70,7 +67,7 @@ public void save() throws LocalOperationException, RequestException, IOException Template template = new Template(transloadit, "template_name"); Response newTemplate = template.save(); - assertEquals(newTemplate.json().get("ok"), "TEMPLATE_FOUND"); + Assertions.assertEquals(newTemplate.json().get("ok"), "TEMPLATE_FOUND"); mockServerClient.reset(); } diff --git a/src/test/java/com/transloadit/sdk/TransloaditTest.java b/src/test/java/com/transloadit/sdk/TransloaditTest.java index 312a05d..04c50af 100644 --- a/src/test/java/com/transloadit/sdk/TransloaditTest.java +++ b/src/test/java/com/transloadit/sdk/TransloaditTest.java @@ -5,11 +5,13 @@ import com.transloadit.sdk.response.AssemblyResponse; import com.transloadit.sdk.response.ListResponse; import com.transloadit.sdk.response.Response; -import org.junit.After; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockserver.client.MockServerClient; -import org.mockserver.junit.MockServerRule; +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.junit.jupiter.MockServerSettings; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; @@ -21,31 +23,24 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; //CHECKSTYLE:ON -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; + /** * Unit test for {@link Transloadit} class. Api-Responses are simulated by mocking the server's response. */ +@ExtendWith(MockServerExtension.class) // MockServerExtension is used to start and stop the MockServer +@MockServerSettings(ports = MockHttpService.PORT) // MockServerSettings is used to define the port of the MockServer public class TransloaditTest extends MockHttpService { - - /** - * MockServer can be run using the MockServerRule. - */ - @Rule - public MockServerRule mockServerRule = new MockServerRule(this, true, PORT); - - /** * MockServerClient makes HTTP requests to a MockServer instance. */ - private MockServerClient mockServerClient; + private final MockServerClient mockServerClient = new MockServerClient("localhost", MockHttpService.PORT); /** * Runs after each test run and resets the mockServerClient. */ - @After + @AfterEach public void tearDown() { mockServerClient.reset(); } @@ -55,7 +50,7 @@ public void tearDown() { */ @Test public void getHostUrl() { - assertEquals(transloadit.getHostUrl(), "http://localhost:" + PORT); + Assertions.assertEquals(transloadit.getHostUrl(), "http://localhost:" + PORT); } /** @@ -74,8 +69,8 @@ public void getAssembly() throws LocalOperationException, RequestException, IOEx AssemblyResponse assembly = transloadit.getAssembly("76fe5df1c93a0a530f3e583805cf98b4"); - assertEquals(assembly.getId(), "76fe5df1c93a0a530f3e583805cf98b4"); - assertEquals(assembly.getUrl(), "http://localhost:9040/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(assembly.getId(), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(assembly.getUrl(), "http://localhost:9040/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); } /** @@ -96,8 +91,8 @@ public void getAssemblyByUrl() throws LocalOperationException, RequestException, AssemblyResponse assembly = transloadit .getAssemblyByUrl(transloadit.getHostUrl() + "/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); - assertEquals(assembly.getId(), "76fe5df1c93a0a530f3e583805cf98b4"); - assertEquals(assembly.getUrl(), "http://localhost:9040/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(assembly.getId(), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(assembly.getUrl(), "http://localhost:9040/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); } /** @@ -116,7 +111,7 @@ public void cancelAssembly() throws LocalOperationException, RequestException, I AssemblyResponse assembly = transloadit .cancelAssembly(transloadit.getHostUrl() + "/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); - assertEquals(assembly.json().getString("ok"), "ASSEMBLY_CANCELED"); + Assertions.assertEquals(assembly.json().getString("ok"), "ASSEMBLY_CANCELED"); } /** @@ -134,7 +129,7 @@ public void listAssemblies() throws RequestException, LocalOperationException, I .respond(HttpResponse.response().withBody(getJson("assemblies.json"))); ListResponse assemblies = transloadit.listAssemblies(); - assertEquals(assemblies.size(), 0); + Assertions.assertEquals(assemblies.size(), 0); } /** @@ -153,8 +148,8 @@ public void getTemplate() throws RequestException, LocalOperationException, IOEx Response template = transloadit.getTemplate("76fe5df1c93a0a530f3e583805cf98b4"); - assertEquals(template.json().get("template_id"), "76fe5df1c93a0a530f3e583805cf98b4"); - assertEquals(template.json().get("ok"), "TEMPLATE_FOUND"); + Assertions.assertEquals(template.json().get("template_id"), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(template.json().get("ok"), "TEMPLATE_FOUND"); } /** @@ -175,7 +170,7 @@ public void updateTemplate() Response template = transloadit.updateTemplate("55c965a063a311e6ba2d379ef10b28f7", new HashMap()); - assertEquals(template.json().get("ok"), "TEMPLATE_UPDATED"); + Assertions.assertEquals(template.json().get("ok"), "TEMPLATE_UPDATED"); } /** @@ -194,7 +189,7 @@ public void deleteTemplate() .respond(HttpResponse.response().withBody(getJson("delete_template.json"))); Response deletedTemplate = transloadit.deleteTemplate("11148db0ec4f11e6a05ca3d04d2a53e6"); - assertEquals(deletedTemplate.json().get("ok"), "TEMPLATE_DELETED"); + Assertions.assertEquals(deletedTemplate.json().get("ok"), "TEMPLATE_DELETED"); } /** @@ -211,7 +206,7 @@ public void listTemplates() throws RequestException, LocalOperationException, IO .respond(HttpResponse.response().withBody(getJson("templates.json"))); ListResponse templates = transloadit.listTemplates(); - assertEquals(templates.size(), 0); + Assertions.assertEquals(templates.size(), 0); } /** @@ -229,7 +224,7 @@ public void getBill() throws LocalOperationException, RequestException, IOExcept .respond(HttpResponse.response().withBody(getJson("bill.json"))); Response bill = transloadit.getBill(9, 2016); - assertEquals(bill.json().get("invoice_id"), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(bill.json().get("invoice_id"), "76fe5df1c93a0a530f3e583805cf98b4"); } /** @@ -242,7 +237,7 @@ public void getAndSetqualifiedErrorsForRetry() { exceptionTemplate.add("java.net.SocketTimeoutException"); exceptionTemplate.add("Socket.blah.Exception"); - assertTrue(transloadit.getQualifiedErrorsForRetry().size() == 1); + Assertions.assertEquals(1, transloadit.getQualifiedErrorsForRetry().size()); ArrayList exceptionsSet = transloadit.getQualifiedErrorsForRetry(); exceptionsSet.add("Socket.blah.Exception"); @@ -251,26 +246,27 @@ public void getAndSetqualifiedErrorsForRetry() { exceptionsSet = transloadit.getQualifiedErrorsForRetry(); for (int i = 0; i < exceptionsSet.size(); i++) { - assertEquals(exceptionTemplate.get(i), exceptionsSet.get(i)); + Assertions.assertEquals(exceptionTemplate.get(i), exceptionsSet.get(i)); } } /** * Test if timeout setting works properly. - * @throws LocalOperationException + * @throws LocalOperationException if setting the timeout goes wrong. */ @Test public void getAndSetTimeoutRetry() throws LocalOperationException { - assertEquals(0, transloadit.getRetryDelay()); - transloadit.setRetryDelay(5); - assertEquals(5, transloadit.getRetryDelay()); + int timeout = 5; + Assertions.assertEquals(0, transloadit.getRetryDelay()); + transloadit.setRetryDelay(timeout); + Assertions.assertEquals(timeout, transloadit.getRetryDelay()); Exception exception = new Exception(); try { - transloadit.setRetryDelay(-5); + transloadit.setRetryDelay(-timeout); } catch (LocalOperationException e) { exception = e; } - assertTrue(exception instanceof LocalOperationException); + Assertions.assertInstanceOf(LocalOperationException.class, exception); } @@ -283,7 +279,7 @@ public void loadVersionInfo() { Pattern versionPattern = Pattern.compile( "^[a-z-]*[:]([0-9]+)\\.([0-9]+)\\.([0-9]+)$", Pattern.CASE_INSENSITIVE); Matcher matcher = versionPattern.matcher(info); - assertTrue(matcher.find()); + Assertions.assertTrue(matcher.find()); } } diff --git a/src/test/java/com/transloadit/sdk/async/AsyncAssemblyTest.java b/src/test/java/com/transloadit/sdk/async/AsyncAssemblyTest.java index 5d5e25a..e0ba765 100644 --- a/src/test/java/com/transloadit/sdk/async/AsyncAssemblyTest.java +++ b/src/test/java/com/transloadit/sdk/async/AsyncAssemblyTest.java @@ -9,40 +9,31 @@ import com.transloadit.sdk.exceptions.LocalOperationException; import com.transloadit.sdk.exceptions.RequestException; import com.transloadit.sdk.response.AssemblyResponse; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockserver.client.MockServerClient; -import org.mockserver.junit.MockServerRule; +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.junit.jupiter.MockServerSettings; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; import java.io.File; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import static org.mockserver.model.RegexBody.regex; /** * Unit Test class for {@link AsyncAssembly}. Api-Responses are simulated by mocking the server's response. */ +@ExtendWith(MockServerExtension.class) // MockServerExtension is used to start and stop the MockServer +@MockServerSettings(ports = MockHttpService.PORT) // MockServerSettings is used to define the port of the MockServer public class AsyncAssemblyTest extends MockHttpService { - - /** - * MockServer can be run using the MockServerRule. - */ - @Rule - public MockServerRule mockServerRule = new MockServerRule(this, true, PORT); - /** * MockServerClient makes HTTP requests to a MockServer instance. */ - private MockServerClient mockServerClient; + private final MockServerClient mockServerClient = new MockServerClient("localhost", PORT); private AsyncAssembly assembly; private AssemblyProgressListener listener; @@ -58,7 +49,7 @@ public class AsyncAssemblyTest extends MockHttpService { * Defines basic Mockserver Expectations to support Assembly creation and status updates. * @throws Exception if Test resources "async_resumable_assembly.json" or "assembly.json" are missing. */ - @Before + @BeforeEach public void setUp() throws Exception { mockServerClient.reset(); listener = new Listener(); @@ -99,12 +90,12 @@ public void save() throws LocalOperationException, RequestException, Interrupted synchronized (listener) { listener.wait(3000); } - assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); - assertTrue(uploadFinished); - assertTrue(assemblyFinished); - assertEquals(1077, totalUploaded); - assertNull(statusUpdateError); - assertNull(uploadError); + Assertions.assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertTrue(uploadFinished); + Assertions.assertTrue(assemblyFinished); + Assertions.assertEquals(1077, totalUploaded); + Assertions.assertNull(statusUpdateError); + Assertions.assertNull(uploadError); } /** @@ -122,12 +113,12 @@ public void saveWithoutWaitForCompletion() throws LocalOperationException, Reque synchronized (listener) { listener.wait(3000); } - assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); - assertTrue(uploadFinished); - assertFalse(assemblyFinished); - assertEquals(1077, totalUploaded); - assertNull(statusUpdateError); - assertNull(uploadError); + Assertions.assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertTrue(uploadFinished); + Assertions.assertFalse(assemblyFinished); + Assertions.assertEquals(1077, totalUploaded); + Assertions.assertNull(statusUpdateError); + Assertions.assertNull(uploadError); mockServerClient.reset(); } @@ -148,14 +139,14 @@ public void saveWithUploadError() throws LocalOperationException, RequestExcepti synchronized (listener) { listener.wait(3000); } - assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); - assertFalse(uploadFinished); - assertFalse(assemblyFinished); - assertNotEquals(1077, totalUploaded); - assertNull(statusUpdateError); - - assertNotNull(uploadError); - assertEquals("some error message", uploadError.getMessage()); + Assertions.assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertFalse(uploadFinished); + Assertions.assertFalse(assemblyFinished); + Assertions.assertNotEquals(1077, totalUploaded); + Assertions.assertNull(statusUpdateError); + + Assertions.assertNotNull(uploadError); + Assertions.assertEquals("some error message", uploadError.getMessage()); } /** @@ -174,14 +165,14 @@ public void saveWithStatusError() throws LocalOperationException, RequestExcepti synchronized (listener) { listener.wait(3000); } - assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); - assertTrue(uploadFinished); - assertEquals(1077, totalUploaded); - assertNull(uploadError); - assertFalse(assemblyFinished); - - assertNotNull(statusUpdateError); - assertEquals("some request exception", statusUpdateError.getMessage()); + Assertions.assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertTrue(uploadFinished); + Assertions.assertEquals(1077, totalUploaded); + Assertions.assertNull(uploadError); + Assertions.assertFalse(assemblyFinished); + + Assertions.assertNotNull(statusUpdateError); + Assertions.assertEquals("some request exception", statusUpdateError.getMessage()); } /** @@ -207,15 +198,15 @@ public void pauseResumeUpload() throws LocalOperationException, RequestException synchronized (listener) { listener.wait(3000); } - assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); - assertEquals(MockAsyncAssembly.State.PAUSED, assembly.state); + Assertions.assertEquals(resumableAssembly.json().get("assembly_id"), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(MockAsyncAssembly.State.PAUSED, assembly.state); // expect the states to not have updated after 5 seconds of wait - assertFalse(uploadFinished); - assertFalse(assemblyFinished); - assertNotEquals(1077, totalUploaded); - assertNull(statusUpdateError); - assertNull(uploadError); + Assertions.assertFalse(uploadFinished); + Assertions.assertFalse(assemblyFinished); + Assertions.assertNotEquals(1077, totalUploaded); + Assertions.assertNull(statusUpdateError); + Assertions.assertNull(uploadError); // resume upload and wait again assembly.resumeUpload(); @@ -224,11 +215,11 @@ public void pauseResumeUpload() throws LocalOperationException, RequestException } // expect the states to have changed as the upload is done this time. - assertTrue(uploadFinished); - assertTrue(assemblyFinished); - assertEquals(1077, totalUploaded); - assertNull(statusUpdateError); - assertNull(uploadError); + Assertions.assertTrue(uploadFinished); + Assertions.assertTrue(assemblyFinished); + Assertions.assertEquals(1077, totalUploaded); + Assertions.assertNull(statusUpdateError); + Assertions.assertNull(uploadError); } /** diff --git a/src/test/java/com/transloadit/sdk/response/AssemblyResponseTest.java b/src/test/java/com/transloadit/sdk/response/AssemblyResponseTest.java index 953ce61..03f0a1c 100644 --- a/src/test/java/com/transloadit/sdk/response/AssemblyResponseTest.java +++ b/src/test/java/com/transloadit/sdk/response/AssemblyResponseTest.java @@ -1,34 +1,30 @@ package com.transloadit.sdk.response; import com.transloadit.sdk.MockHttpService; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockserver.client.MockServerClient; -import org.mockserver.junit.MockServerRule; +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.junit.jupiter.MockServerSettings; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; /** * Unit Test class for {@link AssemblyResponse}. Api-Responses are simulated by mocking the server's response. */ +@ExtendWith(MockServerExtension.class) // MockServerExtension is used to start and stop the MockServer +@MockServerSettings(ports = MockHttpService.PORT) // MockServerSettings is used to define the port of the MockServer public class AssemblyResponseTest extends MockHttpService { - /** - * MockServer can be run using the MockServerRule. - */ - @Rule - public MockServerRule mockServerRule = new MockServerRule(this, true, PORT); - /** * MockServerClient makes HTTP requests to a MockServer instance. */ - private MockServerClient mockServerClient; + private final MockServerClient mockServerClient = new MockServerClient("localhost", MockHttpService.PORT); /** * Links to {@link AssemblyResponse} instance to perform the tests on. @@ -40,7 +36,7 @@ public class AssemblyResponseTest extends MockHttpService { * before each test. * @throws Exception if Test resource "assembly.json" is missing. */ - @Before + @BeforeEach public void setUp() throws Exception { mockServerClient.when(HttpRequest.request() .withPath("/assemblies").withMethod("POST")) @@ -52,7 +48,7 @@ public void setUp() throws Exception { /** * Resets the mockServerClient after each run. */ - @After + @AfterEach public void tearDown() { mockServerClient.reset(); } @@ -62,7 +58,7 @@ public void tearDown() { */ @Test public void getId() { - assertEquals(response.getId(), "76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(response.getId(), "76fe5df1c93a0a530f3e583805cf98b4"); } /** @@ -71,7 +67,7 @@ public void getId() { */ @Test public void getUrl() { - assertEquals(response.getUrl(), "http://localhost:9040/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(response.getUrl(), "http://localhost:9040/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); } /** @@ -80,7 +76,7 @@ public void getUrl() { */ @Test public void getSslUrl() { - assertEquals(response.getSslUrl(), "http://localhost:9040/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); + Assertions.assertEquals(response.getSslUrl(), "http://localhost:9040/assemblies/76fe5df1c93a0a530f3e583805cf98b4"); } /** @@ -88,7 +84,7 @@ public void getSslUrl() { */ @Test public void getStepResult() { - assertEquals(response.getStepResult("resize").getJSONObject(0).getString("url"), + Assertions.assertEquals(response.getStepResult("resize").getJSONObject(0).getString("url"), "http://tmp.jane.transloadit.com/result_url.png"); } @@ -97,7 +93,7 @@ public void getStepResult() { */ @Test public void isCompleted() { - assertTrue(response.isCompleted()); + Assertions.assertTrue(response.isCompleted()); } /** @@ -117,8 +113,8 @@ public void isAborted() throws Exception { AssemblyResponse abortedResponse = newAssemblyWithoutID().save(false); - assertTrue(abortedResponse.isAborted()); - assertFalse(response.isAborted()); + Assertions.assertTrue(abortedResponse.isAborted()); + Assertions.assertFalse(response.isAborted()); } /** @@ -138,8 +134,8 @@ public void isCanceled() throws Exception { AssemblyResponse cancelledResponse = newAssemblyWithoutID().save(false); - assertTrue(cancelledResponse.isCanceled()); - assertFalse(response.isCanceled()); + Assertions.assertTrue(cancelledResponse.isCanceled()); + Assertions.assertFalse(response.isCanceled()); } /** @@ -159,8 +155,8 @@ public void isExecuting() throws Exception { AssemblyResponse executingResponse = newAssemblyWithoutID().save(false); - assertTrue(executingResponse.isExecuting()); - assertFalse(response.isExecuting()); + Assertions.assertTrue(executingResponse.isExecuting()); + Assertions.assertFalse(response.isExecuting()); } /** @@ -180,7 +176,7 @@ public void isFinished() throws Exception { AssemblyResponse executingResponse = newAssemblyWithoutID().save(false); - assertFalse(executingResponse.isFinished()); - assertTrue(response.isFinished()); + Assertions.assertFalse(executingResponse.isFinished()); + Assertions.assertTrue(response.isFinished()); } } diff --git a/src/test/java/com/transloadit/sdk/response/ResponseTest.java b/src/test/java/com/transloadit/sdk/response/ResponseTest.java index f22131d..7d6d826 100644 --- a/src/test/java/com/transloadit/sdk/response/ResponseTest.java +++ b/src/test/java/com/transloadit/sdk/response/ResponseTest.java @@ -1,36 +1,33 @@ package com.transloadit.sdk.response; import com.transloadit.sdk.MockHttpService; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockserver.client.MockServerClient; -import org.mockserver.junit.MockServerRule; +import org.mockserver.junit.jupiter.MockServerExtension; +import org.mockserver.junit.jupiter.MockServerSettings; import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpResponse; -import static org.junit.Assert.assertEquals; /** * Unit Test class for {@link Response}. Api-Responses are simulated by mocking the server's response. */ +@ExtendWith(MockServerExtension.class) // MockServerExtension is used to start and stop the MockServer +@MockServerSettings(ports = MockHttpService.PORT) // MockServerSettings is used to define the port of the MockServer public class ResponseTest extends MockHttpService { - /** - * MockServer can be run using the MockServerRule. - */ - @Rule - public MockServerRule mockServerRule = new MockServerRule(this, true, PORT); - /** * MockServerClient makes HTTP requests to a MockServer instance. */ - private MockServerClient mockServerClient; + private final MockServerClient mockServerClient = new MockServerClient("localhost", PORT); /** * Resets MockserverClient before each Test run. */ - @Before + @BeforeEach public void setUp() { mockServerClient.reset(); } @@ -48,7 +45,7 @@ public void json() throws Exception { .respond(HttpResponse.response().withBody(getJson("assembly.json"))); AssemblyResponse response = newAssemblyWithoutID().save(false); - assertEquals(response.json().getString("ok"), "ASSEMBLY_COMPLETED"); + Assertions.assertEquals(response.json().getString("ok"), "ASSEMBLY_COMPLETED"); } /** @@ -62,7 +59,7 @@ public void status() throws Exception { .respond(HttpResponse.response().withBody(getJson("assembly.json"))); AssemblyResponse response = newAssemblyWithoutID().save(false); - assertEquals(response.status(), 200); + Assertions.assertEquals(response.status(), 200); } }