-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
364 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/java/com/chavaillaz/client/common/exception/DeserializationExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.chavaillaz.client.common.exception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.type.TypeFactory; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DeserializationExceptionTest { | ||
|
||
@Test | ||
void testExceptionMessageWithClass() { | ||
Exception root = new Exception("Root"); | ||
Exception exception = new DeserializationException("Content", String.class, root); | ||
|
||
assertTrue(exception.getMessage().contains("Content")); | ||
assertTrue(exception.getMessage().contains("String")); | ||
assertEquals(root, exception.getCause()); | ||
} | ||
|
||
@Test | ||
void testExcepionMessageWithJavaType() { | ||
JavaType type = TypeFactory.defaultInstance() | ||
.constructSimpleType(String.class, new JavaType[]{}); | ||
Exception root = new Exception("Root"); | ||
Exception exception = new DeserializationException("Content", type, root); | ||
|
||
assertTrue(exception.getMessage().contains("Content")); | ||
assertTrue(exception.getMessage().contains("String")); | ||
assertEquals(root, exception.getCause()); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/com/chavaillaz/client/common/exception/ResponseExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.chavaillaz.client.common.exception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ResponseExceptionTest { | ||
|
||
@Test | ||
void testExceptionMessage() { | ||
ResponseException exception = new ResponseException(404, "Not Found"); | ||
assertEquals(404, exception.getStatusCode()); | ||
assertEquals("Not Found", exception.getBody()); | ||
assertTrue(exception.getMessage().contains("404")); | ||
assertTrue(exception.getMessage().contains("Not Found")); | ||
assertNull(exception.getCause()); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/com/chavaillaz/client/common/exception/SerializationExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.chavaillaz.client.common.exception; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SerializationExceptionTest { | ||
|
||
@Test | ||
void testExceptionMessage() { | ||
Exception root = new Exception("Root"); | ||
Exception exception = new SerializationException("Content", root); | ||
|
||
assertTrue(exception.getMessage().contains("Content")); | ||
assertTrue(exception.getMessage().contains("String")); | ||
assertEquals(root, exception.getCause()); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/com/chavaillaz/client/common/security/PasswordAuthenticationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.chavaillaz.client.common.security; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class PasswordAuthenticationTest { | ||
|
||
@Test | ||
void testPasswordAuthentication() { | ||
PasswordAuthentication authentication = new PasswordAuthentication("User", "Password"); | ||
assertEquals("User", authentication.getUsername()); | ||
assertEquals("Password", authentication.getPassword()); | ||
|
||
Map<String, String> headers = new HashMap<>(); | ||
authentication.fillHeaders(headers::put); | ||
assertEquals(1, headers.size()); | ||
|
||
String authorization = headers.get("Authorization"); | ||
assertNotNull(authorization); | ||
assertEquals("Basic VXNlcjpQYXNzd29yZA==", authorization); | ||
|
||
Map<String, String> cookies = new HashMap<>(); | ||
authentication.fillCookies(cookies::put); | ||
assertTrue(cookies.isEmpty()); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/com/chavaillaz/client/common/security/TokenAuthenticationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.chavaillaz.client.common.security; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class TokenAuthenticationTest { | ||
|
||
@Test | ||
void testTokenAuthentication() { | ||
TokenAuthentication authentication = new TokenAuthentication("Token"); | ||
assertEquals("Token", authentication.getToken()); | ||
|
||
Map<String, String> headers = new HashMap<>(); | ||
authentication.fillHeaders(headers::put); | ||
assertEquals(1, headers.size()); | ||
|
||
String authorization = headers.get("Authorization"); | ||
assertNotNull(authorization); | ||
assertEquals("Bearer VG9rZW4=", authorization); | ||
|
||
Map<String, String> cookies = new HashMap<>(); | ||
authentication.fillCookies(cookies::put); | ||
assertTrue(cookies.isEmpty()); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/chavaillaz/client/common/utility/LazyCachedObjectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.chavaillaz.client.common.utility; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class LazyCachedObjectTest { | ||
|
||
private static final String VALUE = "Test"; | ||
|
||
@Test | ||
void testWithParameterAtInstantiation() { | ||
LazyCachedObject<String> cachedObject = new LazyCachedObject<>(() -> VALUE); | ||
|
||
Optional<String> firstCall = cachedObject.get(); | ||
assertTrue(firstCall.isPresent()); | ||
assertEquals(VALUE, firstCall.get()); | ||
|
||
Optional<String> secondCall = cachedObject.get(); | ||
assertTrue(secondCall.isPresent()); | ||
assertEquals(VALUE, secondCall.get()); | ||
|
||
// Check that the same object has been used | ||
assertSame(firstCall.get(), secondCall.get()); | ||
} | ||
|
||
@Test | ||
void testWithParameterAtGet() { | ||
LazyCachedObject<String> cachedObject = new LazyCachedObject<>(); | ||
assertFalse(cachedObject.get().isPresent()); | ||
|
||
String firstCall = cachedObject.get(() -> VALUE); | ||
assertEquals(VALUE, firstCall); | ||
|
||
// Give on purpose a new string instance in parameter | ||
String secondCall = cachedObject.get(() -> "Test"); | ||
assertEquals(VALUE, secondCall); | ||
|
||
// Check that the same object has been used | ||
assertSame(firstCall, secondCall); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/com/chavaillaz/client/common/utility/ProxyConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.chavaillaz.client.common.utility; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ProxyConfigurationTest { | ||
|
||
private final String URL = "https://proxy.mycompany.com:8443"; | ||
|
||
@Test | ||
void testFromUrl() { | ||
ProxyConfiguration proxy = ProxyConfiguration.from(URL); | ||
assertNotNull(proxy); | ||
assertEquals(URL, proxy.getUrl()); | ||
assertEquals("https", proxy.getScheme()); | ||
assertEquals("proxy.mycompany.com", proxy.getHost()); | ||
assertEquals(8443, proxy.getPort()); | ||
} | ||
|
||
@Test | ||
void testFromWrongUrl() { | ||
ProxyConfiguration proxy = ProxyConfiguration.from("Not an URL"); | ||
assertNull(proxy); | ||
} | ||
|
||
@Test | ||
void testFromSchemeAndHostAndPort() { | ||
ProxyConfiguration proxy = ProxyConfiguration.from("https", "proxy.mycompany.com", 8443); | ||
assertNotNull(proxy); | ||
assertEquals(URL, proxy.getUrl()); | ||
assertEquals("https", proxy.getScheme()); | ||
assertEquals("proxy.mycompany.com", proxy.getHost()); | ||
assertEquals(8443, proxy.getPort()); | ||
} | ||
|
||
@Test | ||
void testFromHostAndPort() { | ||
ProxyConfiguration proxy = ProxyConfiguration.from("proxy.mycompany.com", 8443); | ||
assertNotNull(proxy); | ||
assertEquals("proxy.mycompany.com:8443", proxy.getUrl()); | ||
assertNull(proxy.getScheme()); | ||
assertEquals("proxy.mycompany.com", proxy.getHost()); | ||
assertEquals(8443, proxy.getPort()); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/test/java/com/chavaillaz/client/common/utility/UtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.chavaillaz.client.common.utility; | ||
|
||
import static com.chavaillaz.client.common.utility.Utils.encodeBase64; | ||
import static com.chavaillaz.client.common.utility.Utils.encodeQuery; | ||
import static com.chavaillaz.client.common.utility.Utils.getCookieHeader; | ||
import static com.chavaillaz.client.common.utility.Utils.getProperty; | ||
import static com.chavaillaz.client.common.utility.Utils.readInputStream; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.TreeMap; | ||
import java.util.function.BiConsumer; | ||
|
||
import com.chavaillaz.client.common.security.Authentication; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class UtilsTest { | ||
|
||
@Test | ||
void testReadInputStream() { | ||
String expected = "Test"; | ||
InputStream stream = new ByteArrayInputStream(expected.getBytes(UTF_8)); | ||
String result = readInputStream(stream); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void testEncodeQuery() { | ||
Map<Object, Object> map = new TreeMap<>(); | ||
map.put("A", 1); | ||
map.put("B", 2); | ||
map.put("C", 3); | ||
assertEquals("A=1&B=2&C=3", encodeQuery(map)); | ||
} | ||
|
||
@Test | ||
void testEncodeBase64() { | ||
assertEquals("VGVzdA==", encodeBase64("Test")); | ||
} | ||
|
||
@Test | ||
void testCookieHeaders() { | ||
Authentication authentication = new Authentication() { | ||
|
||
@Override | ||
public void fillHeaders(BiConsumer<String, String> addHeader) { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
public void fillCookies(BiConsumer<String, String> addCookie) { | ||
addCookie.accept("FirstKey", "FirstValue"); | ||
addCookie.accept("SecondKey", "SecondValue"); | ||
} | ||
|
||
}; | ||
|
||
Optional<String> cookieHeader = getCookieHeader(authentication); | ||
assertTrue(cookieHeader.isPresent()); | ||
assertEquals("FirstKey=FirstValue; SecondKey=SecondValue", cookieHeader.get()); | ||
} | ||
|
||
@Test | ||
void testProperty() { | ||
System.setProperty("Test", "Value"); | ||
assertNull(getProperty("Unknown")); | ||
assertEquals("Value", getProperty("Test")); | ||
assertEquals("Value", getProperty("Unknown", "Value")); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/com/chavaillaz/client/common/vertx/VertxInputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.chavaillaz.client.common.vertx; | ||
|
||
import static com.chavaillaz.client.common.utility.Utils.readInputStream; | ||
import static io.vertx.core.buffer.Buffer.buffer; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
|
||
import io.vertx.core.buffer.Buffer; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class VertxInputStreamTest { | ||
|
||
@Test | ||
void testReadInputStream() throws IOException { | ||
String expected = "Test"; | ||
Buffer buffer = buffer(expected); | ||
|
||
try (VertxInputStream inputStream = new VertxInputStream(buffer)) { | ||
String result = readInputStream(inputStream); | ||
assertEquals(expected, result); | ||
} | ||
} | ||
|
||
} |