Skip to content

Commit

Permalink
Add new tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chavjoh committed Dec 31, 2023
1 parent 88d390d commit 373e41a
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ public static ProxyConfiguration from(String path) {
* @return The full proxy URL
*/
public String getUrl() {
return scheme + "://" + host + ":" + port;
StringBuilder url = new StringBuilder();
if (scheme != null) {
url.append(scheme);
url.append("://");
}
url.append(host);
if (port != null) {
url.append(":");
url.append(port);
}
return url.toString();
}

}
18 changes: 14 additions & 4 deletions src/main/java/com/chavaillaz/client/common/utility/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,22 @@ public static String encodeBase64(String value) {
* @return The optional header value
*/
public static Optional<String> getCookieHeader(Authentication authentication) {
List<Pair<String, String>> values = new ArrayList<>();
authentication.fillCookies((key, value) -> values.add(Pair.of(key, value)));
if (values.isEmpty()) {
List<Pair<String, String>> cookies = new ArrayList<>();
authentication.fillCookies((key, value) -> cookies.add(Pair.of(key, value)));
return getCookieHeader(cookies);
}

/**
* Gets the value of the {@code Cookie} header for the given cookies.
*
* @param cookies The cookies with for each its key and value
* @return The optional header value
*/
public static Optional<String> getCookieHeader(List<Pair<String, String>> cookies) {
if (cookies.isEmpty()) {
return Optional.empty();
}
return Optional.of(values.stream()
return Optional.of(cookies.stream()
.map(pair -> pair.getKey() + "=" + pair.getValue())
.collect(joining("; ")));
}
Expand Down
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());
}

}
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());
}

}
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());
}

}
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());
}

}
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());
}

}
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);
}

}
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 src/test/java/com/chavaillaz/client/common/utility/UtilsTest.java
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"));
}

}
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);
}
}

}

0 comments on commit 373e41a

Please sign in to comment.