Skip to content

Commit

Permalink
Issue eclipse-rdf4j#80: Allow a null RDFFormat to upload.
Browse files Browse the repository at this point in the history
Allow a null RDFFormat to upload, and pass it through as an
untyped entity.

Signed-off-by: Joseph Walton <joe@kafsemo.org>
  • Loading branch information
josephw committed Oct 5, 2016
1 parent a89c135 commit bed8341
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
12 changes: 12 additions & 0 deletions core/http/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -45,8 +46,8 @@
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.eclipse.rdf4j.IsolationLevel;
import org.eclipse.rdf4j.RDF4JException;
import org.eclipse.rdf4j.OpenRDFUtil;
import org.eclipse.rdf4j.RDF4JException;
import org.eclipse.rdf4j.common.io.IOUtil;
import org.eclipse.rdf4j.http.protocol.Protocol;
import org.eclipse.rdf4j.http.protocol.Protocol.Action;
Expand Down Expand Up @@ -668,7 +669,8 @@ protected void upload(InputStream contents, String baseURI, RDFFormat dataFormat
// Set Content-Length to -1 as we don't know it and we also don't want to
// cache
HttpEntity entity = new InputStreamEntity(contents, -1,
ContentType.parse(dataFormat.getDefaultMIMEType()));
Optional.ofNullable(dataFormat).map(df -> ContentType.parse(df.getDefaultMIMEType())).orElse(
null));
upload(entity, baseURI, overwrite, preserveNodeIds, action, contexts);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*******************************************************************************
* Copyright (c) 2016 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.http.client;

import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicStatusLine;
import org.eclipse.rdf4j.http.protocol.UnauthorizedException;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

public class SesameSessionTest {

@Rule
public MockitoRule wireMocks = MockitoJUnit.rule();

@Mock
HttpClient client;

@Mock
ExecutorService executorService;

@InjectMocks
SesameSession session;

@Test
public void passingNullForDataFormatSendsAnUntypedEntity()
throws UnauthorizedException, RDFParseException, RepositoryException, IOException
{
session.setQueryURL("http://localhost/");

HttpResponse response = mock(HttpResponse.class);

when(response.getEntity()).thenReturn(new StringEntity(""));
when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "Ok"));

ArgumentCaptor<HttpUriRequest> captor = ArgumentCaptor.forClass(HttpUriRequest.class);

when(client.execute(Mockito.<HttpUriRequest> any(), Mockito.<HttpClientContext> any())).thenReturn(
response);

InputStream contents = new ByteArrayInputStream(new byte[0]);
String baseURI = "http://localhost/";
RDFFormat dataFormat = null;
boolean overwrite = true;
boolean preserveNodeIds = true;

session.upload(contents, baseURI, dataFormat, overwrite, preserveNodeIds);

verify(client).execute(captor.capture(), Mockito.<HttpClientContext> any());

assertThat(captor.getValue(), org.hamcrest.Matchers.instanceOf(HttpPut.class));
HttpPut put = (HttpPut)captor.getValue();
assertThat(put.getEntity().getContentType(), Matchers.nullValue());
}
}

0 comments on commit bed8341

Please sign in to comment.