added json and xml support to ahc

This commit is contained in:
Sebastian Sdorra
2015-05-03 15:51:21 +02:00
parent 722d2616a8
commit 1f4524bb20
19 changed files with 1222 additions and 81 deletions

View File

@@ -31,26 +31,22 @@
package sonia.scm.net.ahc;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class AdvancedHttpClientTest {
private final AdvancedHttpClient client = new AdvancedHttpClient()
{
@Override
protected AdvancedHttpResponse request(
BaseHttpRequest<?> request) throws IOException
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
@Mock(answer = Answers.CALLS_REAL_METHODS)
private AdvancedHttpClient client;
private static final String URL = "https://www.scm-manager.org";
@@ -101,4 +97,12 @@ public class AdvancedHttpClientTest {
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.HEAD, request.getMethod());
}
@Test
public void testMethod()
{
AdvancedHttpRequestWithBody request = client.method("PROPFIND", URL);
assertEquals(URL, request.getUrl());
assertEquals("PROPFIND", request.getMethod());
}
}

View File

@@ -33,11 +33,14 @@ package sonia.scm.net.ahc;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
@@ -55,6 +58,9 @@ public class AdvancedHttpRequestWithBodyTest {
@Mock
private AdvancedHttpClient ahc;
@Mock
private ContentTransformer transformer;
private AdvancedHttpRequestWithBody request;
@Rule
@@ -118,6 +124,42 @@ public class AdvancedHttpRequestWithBodyTest {
assertThat(request.getContent(), instanceOf(StringContent.class));
}
@Test
public void testXmlContent() throws IOException{
when(ahc.createTransformer(String.class, ContentType.XML)).thenReturn(transformer);
when(transformer.marshall("<root />")).thenReturn(ByteSource.wrap("<root></root>".getBytes(Charsets.UTF_8)));
Content content = request.xmlContent("<root />").getContent();
assertThat(content, instanceOf(ByteSourceContent.class));
ByteSourceContent bsc = (ByteSourceContent) content;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bsc.process(baos);
assertEquals("<root></root>", baos.toString("UTF-8"));
}
@Test
public void testJsonContent() throws IOException{
when(ahc.createTransformer(String.class, ContentType.JSON)).thenReturn(transformer);
when(transformer.marshall("{}")).thenReturn(ByteSource.wrap("{'root': {}}".getBytes(Charsets.UTF_8)));
Content content = request.jsonContent("{}").getContent();
assertThat(content, instanceOf(ByteSourceContent.class));
ByteSourceContent bsc = (ByteSourceContent) content;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bsc.process(baos);
assertEquals("{'root': {}}", baos.toString("UTF-8"));
}
@Test
public void testTransformedContent() throws IOException{
when(ahc.createTransformer(String.class, "text/plain")).thenReturn(transformer);
when(transformer.marshall("hello")).thenReturn(ByteSource.wrap("hello world".getBytes(Charsets.UTF_8)));
Content content = request.transformedContent("text/plain", "hello").getContent();
assertThat(content, instanceOf(ByteSourceContent.class));
ByteSourceContent bsc = (ByteSourceContent) content;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bsc.process(baos);
assertEquals("hello world", baos.toString("UTF-8"));
}
@Test
public void testSelf()
{

View File

@@ -55,6 +55,9 @@ public class AdvancedHttpResponseTest {
@Mock(answer = Answers.CALLS_REAL_METHODS)
private AdvancedHttpResponse response;
@Mock
private ContentTransformer transformer;
@Test
public void testContent() throws IOException
@@ -65,6 +68,12 @@ public class AdvancedHttpResponseTest {
assertEquals("test123", new String(data, Charsets.UTF_8));
}
@Test
public void testContentWithoutByteSource() throws IOException
{
assertNull(response.content());
}
@Test
public void testContentAsString() throws IOException
{
@@ -73,6 +82,12 @@ public class AdvancedHttpResponseTest {
assertEquals("123test", response.contentAsString());
}
@Test
public void testContentAsStingWithoutByteSource() throws IOException
{
assertNull(response.contentAsString());
}
@Test
public void testContentAsReader() throws IOException
{
@@ -81,6 +96,12 @@ public class AdvancedHttpResponseTest {
assertEquals("abc123", CharStreams.toString(response.contentAsReader()));
}
@Test
public void testContentAsReaderWithoutByteSource() throws IOException
{
assertNull(response.contentAsReader());
}
@Test
public void testContentAsStream() throws IOException
{
@@ -90,6 +111,69 @@ public class AdvancedHttpResponseTest {
assertEquals("cde456", new String(data, Charsets.UTF_8));
}
@Test
public void testContentAsStreamWithoutByteSource() throws IOException
{
assertNull(response.contentAsStream());
}
@Test
public void testContentFromJson() throws IOException{
ByteSource bs = ByteSource.wrap("{}".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
when(response.createTransformer(String.class, ContentType.JSON)).thenReturn(transformer);
when(transformer.unmarshall(String.class, bs)).thenReturn("{root: null}");
String c = response.contentFromJson(String.class);
assertEquals("{root: null}", c);
}
@Test
public void testContentFromXml() throws IOException{
ByteSource bs = ByteSource.wrap("<root />".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
when(response.createTransformer(String.class, ContentType.XML)).thenReturn(transformer);
when(transformer.unmarshall(String.class, bs)).thenReturn("<root></root>");
String c = response.contentFromXml(String.class);
assertEquals("<root></root>", c);
}
@Test(expected = ContentTransformerException.class)
public void testContentTransformedWithoutHeader() throws IOException{
Multimap<String,String> map = LinkedHashMultimap.create();
when(response.getHeaders()).thenReturn(map);
response.contentTransformed(String.class);
}
@Test
public void testContentTransformedFromHeader() throws IOException{
Multimap<String,String> map = LinkedHashMultimap.create();
map.put("Content-Type", "text/plain");
when(response.getHeaders()).thenReturn(map);
when(response.createTransformer(String.class, "text/plain")).thenReturn(
transformer);
ByteSource bs = ByteSource.wrap("hello".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
when(transformer.unmarshall(String.class, bs)).thenReturn("hello world");
String v = response.contentTransformed(String.class);
assertEquals("hello world", v);
}
@Test
public void testContentTransformed() throws IOException{
when(response.createTransformer(String.class, "text/plain")).thenReturn(
transformer);
ByteSource bs = ByteSource.wrap("hello".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
when(transformer.unmarshall(String.class, bs)).thenReturn("hello world");
String v = response.contentTransformed(String.class, "text/plain");
assertEquals("hello world", v);
}
@Test
public void testContentTransformedWithoutByteSource() throws IOException{
assertNull(response.contentTransformed(String.class, "text/plain"));
}
@Test
public void testGetFirstHeader() throws IOException
{