Skip to content

Commit

Permalink
move the BDD tests into their own module so we can be dependent on ot…
Browse files Browse the repository at this point in the history
…her modules
  • Loading branch information
ryber committed Sep 15, 2022
1 parent c255245 commit 6c95ea0
Show file tree
Hide file tree
Showing 84 changed files with 475 additions and 243 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>object-mapper-gson</module>
<module>object-mapper-jackson</module>
<module>unirest-mocks</module>
<module>unirest-bdd-tests</module>
<!--<module>bad-maven-test</module>-->
</modules>

Expand Down Expand Up @@ -221,7 +222,7 @@
<limit implementation="org.jacoco.report.check.Limit">
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum>
<minimum>0.40</minimum>
</limit>
</limits>
</rule>
Expand Down
77 changes: 77 additions & 0 deletions unirest-bdd-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>unirest-java-parent</artifactId>
<groupId>com.konghq</groupId>
<version>3.13.12-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>unirest-bdd-tests</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<main.dir>${project.parent.basedir}</main.dir>
</properties>

<dependencies>
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-objectmapper-jackson</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>3.13.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@

package BehaviorTests;

import kong.unirest.JacksonObjectMapper;
import kong.unirest.Unirest;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;

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

class AsBytesTest extends BddTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package BehaviorTests;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
Expand All @@ -41,8 +42,8 @@ void canDoAEmptyRequestThatDoesNotParseBodyAtAll() {
HttpResponse res = Unirest.get(MockServer.GET).asEmpty();

assertNull(res.getBody());
assertEquals(200, res.getStatus());
assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
Assertions.assertEquals(200, res.getStatus());
Assertions.assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
}

@Test
Expand All @@ -51,8 +52,8 @@ void canDoEmptyAsync() throws Exception {
HttpResponse res = Unirest.get(MockServer.GET).asEmptyAsync().get();

assertNull(res.getBody());
assertEquals(200, res.getStatus());
assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
Assertions.assertEquals(200, res.getStatus());
Assertions.assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
}

@Test
Expand All @@ -62,8 +63,8 @@ void canDoEmptyAsyncWithCallback() {
Unirest.get(MockServer.GET)
.asEmptyAsync(res -> {
assertNull(res.getBody());
assertEquals(200, res.getStatus());
assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
Assertions.assertEquals(200, res.getStatus());
Assertions.assertEquals("json;charset=utf-8", res.getHeaders().getFirst("Content-Type"));
asyncSuccess();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

import kong.unirest.HttpResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import kong.unirest.JacksonObjectMapper;
import kong.unirest.Unirest;

import java.io.File;
Expand Down Expand Up @@ -96,7 +96,7 @@ void canSaveContentsIntoFileAsyncWithCallback() throws Exception {
.assertParam("talking", "heads")
.assertParam("param3", "こんにちは")
.assertStatus(200);
assertEquals(test.toFile().getPath(), r.getBody().getPath());
Assertions.assertEquals(test.toFile().getPath(), r.getBody().getPath());
asyncSuccess();
});

Expand All @@ -122,7 +122,7 @@ void byDefaultFailWhenAttemptingToOverride() {
HttpResponse<File> f2 = Unirest.get(MockServer.BINARYFILE)
.asFile(test.toString());

assertTrue(f2.getParsingError().isPresent());
Assertions.assertTrue(f2.getParsingError().isPresent());
assertTrue(f2.getParsingError().get().getCause().getCause() instanceof FileAlreadyExistsException);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import kong.unirest.*;
import kong.unirest.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand All @@ -40,24 +41,24 @@ class AsJsonTest extends BddTest {
void whenNoBodyIsReturned() {
HttpResponse<JsonNode> i = Unirest.get(MockServer.NOBODY).asJson();

assertEquals(HttpStatus.OK, i.getStatus());
assertEquals("{}", i.getBody().toString());
Assertions.assertEquals(HttpStatus.OK, i.getStatus());
Assertions.assertEquals("{}", i.getBody().toString());
}

@Test
void toStringAObject() {
MockServer.setStringResponse(new JSONObject().put("f", 1).put("a", Arrays.asList(2, 3, 4)).toString());
HttpResponse<JsonNode> i = Unirest.get(MockServer.GET).asJson();

assertEquals("{\"f\":1,\"a\":[2,3,4]}", i.getBody().toString());
Assertions.assertEquals("{\"f\":1,\"a\":[2,3,4]}", i.getBody().toString());
}

@Test
void toPrettyStringAObject() {
MockServer.setStringResponse(new JSONObject().put("f", 1).put("a", Arrays.asList(2, 3, 4)).toString());
HttpResponse<JsonNode> i = Unirest.get(MockServer.GET).asJson();

assertEquals("{\n" +
Assertions.assertEquals("{\n" +
" \"f\": 1,\n" +
" \"a\": [\n" +
" 2,\n" +
Expand Down Expand Up @@ -101,12 +102,12 @@ void canGetBinaryResponseAsyncWithCallback() {
void failureToReturnValidJsonWillResultInAnEmptyNode() {
HttpResponse<JsonNode> response = Unirest.get(MockServer.INVALID_REQUEST).asJson();

assertEquals(HttpStatus.BAD_REQUEST, response.getStatus());
Assertions.assertEquals(HttpStatus.BAD_REQUEST, response.getStatus());
assertNull(response.getBody());
assertTrue(response.getParsingError().isPresent());
Assertions.assertTrue(response.getParsingError().isPresent());
UnirestParsingException ex = response.getParsingError().get();
assertEquals("You did something bad", ex.getOriginalBody());
assertEquals("kong.unirest.json.JSONException: Invalid JSON",
Assertions.assertEquals("You did something bad", ex.getOriginalBody());
Assertions.assertEquals("kong.unirest.json.JSONException: Invalid JSON",
response.getParsingError().get().getMessage());

}
Expand Down Expand Up @@ -135,10 +136,10 @@ void doNotEscapeHTML() {
.getBody()
.getObject();

assertEquals("{\"test\":\"it's a && b || c + 1!?\"}", test.toString());
Assertions.assertEquals("{\"test\":\"it's a && b || c + 1!?\"}", test.toString());
}

private void assertJson(HttpResponse<JsonNode> i) {
assertEquals("bar", i.getBody().getObject().getJSONObject("params").getJSONArray("foo").get(0));
Assertions.assertEquals("bar", i.getBody().getObject().getJSONObject("params").getJSONArray("foo").get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.google.gson.Gson;
import kong.unirest.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -60,8 +61,8 @@ void basicJsonObjectMapperIsGoodEnough() {
void whenNoBodyIsReturned() {
HttpResponse<RequestCapture> i = Unirest.get(MockServer.NOBODY).asObject(RequestCapture.class);

assertEquals(200, i.getStatus());
assertEquals(null, i.getBody());
Assertions.assertEquals(200, i.getStatus());
Assertions.assertEquals(null, i.getBody());
}

@Test
Expand Down Expand Up @@ -155,10 +156,10 @@ void ifTheObjectMapperFailsReturnEmptyAndAddToParsingError() {
.asObject(RequestCapture.class);

assertNull(request.getBody());
assertTrue(request.getParsingError().isPresent());
assertEquals("kong.unirest.UnirestException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'You': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" +
Assertions.assertTrue(request.getParsingError().isPresent());
Assertions.assertEquals("kong.unirest.UnirestException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'You': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" +
" at [Source: (String)\"You did something bad\"; line: 1, column: 4]", request.getParsingError().get().getMessage());
assertEquals("You did something bad", request.getParsingError().get().getOriginalBody());
Assertions.assertEquals("You did something bad", request.getParsingError().get().getOriginalBody());

}

Expand All @@ -168,10 +169,10 @@ void ifTheObjectMapperFailsReturnEmptyAndAddToParsingErrorObGenericTypes() {
.asObject(new GenericType<RequestCapture>() {});

assertNull(request.getBody());
assertTrue(request.getParsingError().isPresent());
assertEquals("kong.unirest.UnirestException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'You': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" +
Assertions.assertTrue(request.getParsingError().isPresent());
Assertions.assertEquals("kong.unirest.UnirestException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'You': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" +
" at [Source: (String)\"You did something bad\"; line: 1, column: 4]", request.getParsingError().get().getMessage());
assertEquals("You did something bad", request.getParsingError().get().getOriginalBody());
Assertions.assertEquals("You did something bad", request.getParsingError().get().getOriginalBody());
}

@Test
Expand All @@ -180,10 +181,10 @@ void unirestExceptionsAreAlsoParseExceptions() {
.asObject(new GenericType<RequestCapture>() {});

assertNull(request.getBody());
assertTrue(request.getParsingError().isPresent());
assertEquals("kong.unirest.UnirestException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'You': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" +
Assertions.assertTrue(request.getParsingError().isPresent());
Assertions.assertEquals("kong.unirest.UnirestException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'You': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" +
" at [Source: (String)\"You did something bad\"; line: 1, column: 4]", request.getParsingError().get().getMessage());
assertEquals("You did something bad", request.getParsingError().get().getOriginalBody());
Assertions.assertEquals("You did something bad", request.getParsingError().get().getOriginalBody());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
package BehaviorTests;

import io.javalin.core.util.Header;
import kong.unirest.TestUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import kong.unirest.HttpResponse;
import kong.unirest.TestUtil;
import kong.unirest.Unirest;

import java.util.concurrent.CompletableFuture;
Expand All @@ -48,8 +49,8 @@ void simpleExample() {
void whenNoBodyIsReturned() {
HttpResponse<String> i = Unirest.get(MockServer.NOBODY).asString();

assertEquals(200, i.getStatus());
assertEquals("", i.getBody());
Assertions.assertEquals(200, i.getStatus());
Assertions.assertEquals("", i.getBody());
}

@Test
Expand All @@ -60,7 +61,7 @@ void canParseGzippedStringResponse() {
.asString();

RequestCapture cap = TestUtil.readValue(i.getBody(), RequestCapture.class);
assertEquals(200, i.getStatus());
Assertions.assertEquals(200, i.getStatus());
cap.assertParam("foo", "bar");
}

Expand All @@ -71,7 +72,7 @@ void canParseGzippedResponseAsync() throws Exception {
.asStringAsync().get();

RequestCapture cap = TestUtil.readValue(i.getBody(), RequestCapture.class);
assertEquals(200, i.getStatus());
Assertions.assertEquals(200, i.getStatus());
cap.assertParam("foo", "bar");
}

Expand Down Expand Up @@ -112,7 +113,7 @@ void canGetBinaryResponseAsyncWithCallback() {
void unicodeResponse() {
MockServer.setStringResponse("ěščřžýáíé");

assertEquals("ěščřžýáíé", Unirest.get(MockServer.GET).asString().getBody());
Assertions.assertEquals("ěščřžýáíé", Unirest.get(MockServer.GET).asString().getBody());
}

@Test
Expand All @@ -121,7 +122,7 @@ void unicodeResponseAsync() throws Exception {

Unirest.get(MockServer.GET)
.asStringAsync(r -> {
assertEquals("ěščřžýáíé", r.getBody());
Assertions.assertEquals("ěščřžýáíé", r.getBody());
asyncSuccess();
});

Expand All @@ -134,8 +135,8 @@ void canSetExpectedCharsetOfResponse() {
.responseEncoding("windows-1250")
.asString();

assertEquals(200, response.getStatus());
assertEquals("šžýáíé", response.getBody());
Assertions.assertEquals(200, response.getStatus());
Assertions.assertEquals("šžýáíé", response.getBody());
}

@Test
Expand All @@ -145,7 +146,7 @@ void canSetDefaultCharsetOfResponse() {
HttpResponse<String> response = Unirest.get(MockServer.WINDOWS_LATIN_1_FILE)
.asString();

assertEquals(200, response.getStatus());
assertEquals("šžýáíé", response.getBody());
Assertions.assertEquals(200, response.getStatus());
Assertions.assertEquals("šžýáíé", response.getBody());
}
}
Loading

0 comments on commit 6c95ea0

Please sign in to comment.