package com.insightng.thirdparty.primal;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.TestUtil;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.openrdf.model.Literal;
import org.openrdf.model.Model;
import org.openrdf.model.Resource;
import org.openrdf.model.vocabulary.DC;
import org.openrdf.model.vocabulary.RDF;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.*;
import com.insightng.thirdparty.primal.vocabulary.PRIMAL;
import static org.mockito.Mockito.*;
import static java.net.HttpURLConnection.*;
public class PrimalClientTest {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
private PrimalAccount primalAccount;
private HttpClient httpClient;
private PrimalClient primalClient;
@Before
public void before() {
primalAccount = mock(PrimalAccount.class);
httpClient = mock(HttpClient.class);
when(httpClient.getState()).thenReturn(mock(HttpState.class));
when(httpClient.getParams()).thenReturn(mock(HttpClientParams.class));
primalClient = new PrimalClient(primalAccount, httpClient);
}
@After
public void after() {
primalAccount = null;
httpClient = null;
primalClient = null;
}
/**
* @throws Exception
*/
@Test
public void testParseResponse() throws Exception {
when(httpClient.executeMethod(any(HttpMethod.class))).thenAnswer(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
GetMethod method = (GetMethod) invocation.getArguments()[0];
InputStream response = getClass().getResourceAsStream("volcanoes.json");
TestUtil.setResponseStream(method, response);
return HTTP_OK;
}
});
Model model = primalClient.getResult("topic", "contentSource", 0.6f, 20, 100);
assertEquals(10, model.filter(null, RDF.TYPE, PRIMAL.CONTENT_ITEM).subjects().size());
Set<String> refs = new HashSet<String>();
for(Resource item : model.filter(null, RDF.TYPE, PRIMAL.CONTENT_ITEM).subjects()) {
final Literal directReference = model.filter(item, DC.RELATION, null).objectLiteral();
Literal name = model.filter(item, DC.TITLE, null).objectLiteral();
assertNotNull(directReference);
assertTrue(refs.add(directReference.stringValue()));
assertNotNull(name);
logger.debug(name.stringValue());
}
}
}