Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz965890 add tests for upload parameters being used
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Jun 12, 2013
1 parent 6516333 commit 7b56541
Showing 1 changed file with 32 additions and 3 deletions.
Expand Up @@ -29,6 +29,8 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
Expand Down Expand Up @@ -81,6 +83,8 @@ public class FileServiceTest
@Mock private HProject project;
@Mock private HProjectIteration projectIteration;

@Captor private ArgumentCaptor<Optional<String>> paramCaptor;

private FileResource fileService;

@BeforeMethod
Expand Down Expand Up @@ -247,6 +251,22 @@ public void canUploadExistingDocument() throws IOException
assertThat(chunkResponse.getErrorMessage(), is(nullValue()));
}

public void usesGivenParameters() throws IOException
{
MockConfig conf = defaultUpload().build();
mockRequiredServices(conf);
fileService.uploadSourceFile(conf.projectSlug, conf.versionSlug, conf.docId, conf.uploadForm);
assertThat(paramCaptor.getValue().get(), is(conf.params));
}

public void fallsBackOnStoredParameters() throws IOException
{
MockConfig conf = defaultUpload().params(null).build();
mockRequiredServices(conf);
fileService.uploadSourceFile(conf.projectSlug, conf.versionSlug, conf.docId, conf.uploadForm);
assertThat(paramCaptor.getValue().get(), is(conf.storedParams));
}

private static void assertErrorResponse(Response response, Status errorStatus, String errorMessage)
{
assertThat(Status.fromStatusCode(response.getStatus()), is(errorStatus));
Expand All @@ -268,13 +288,15 @@ private void mockRequiredServices(MockConfig conf) throws IOException
when(identity.getCredentials()).thenReturn(creds);
File someFile = File.createTempFile("tests", "something");
when(translationFileService.persistToTempFile(Matchers.<InputStream>any())).thenReturn(someFile);
when(documentDAO.getAdapterParams(conf.projectSlug, conf.versionSlug, conf.docId))
.thenReturn(Optional.fromNullable(conf.storedParams));
when(documentDAO.addRawDocument(Matchers.<HDocument>any(), Matchers.<HRawDocument>any()))
.thenReturn(new HRawDocument());
when(documentDAO.getByProjectIterationAndDocId(conf.projectSlug, conf.versionSlug,
conf.docId)).thenReturn(conf.existingDocument);
Resource document = new Resource();
when(translationFileService.parseUpdatedAdapterDocumentFile(
Matchers.<URI>any(), eq(conf.docId), eq(conf.fileType), eq(Optional.of(conf.params))))
Matchers.<URI>any(), eq(conf.docId), eq(conf.fileType), paramCaptor.capture()))
.thenReturn(document);
when(documentService.saveDocument(eq(conf.projectSlug), eq(conf.versionSlug), Matchers.<Resource>any(),
Matchers.anySet(), Matchers.anyBoolean()))
Expand Down Expand Up @@ -323,7 +345,7 @@ private static class MockConfig
// or just a string
public final InputStream fileStream;
public final String hash;
public final String params;
public final String params, storedParams;

public final DocumentFileUploadForm uploadForm;

Expand All @@ -349,6 +371,7 @@ private MockConfig(Builder builder)
fileStream = builder.fileStream;
hash = builder.hash;
params = builder.params;
storedParams = builder.storedParams;

uploadForm = new DocumentFileUploadForm();
uploadForm.setFileType(fileType);
Expand All @@ -374,7 +397,7 @@ private static class Builder
private long size;
private InputStream fileStream;
private String hash;
private String params;
private String params, storedParams;
public HDocument existingDocument;
private boolean hasImportTemplatePermission, plaintextAdapterAvailable;

Expand Down Expand Up @@ -438,6 +461,11 @@ public Builder params(String params)
this.params = params;
return this;
}
public Builder storedParams(String storedParams)
{
this.storedParams = storedParams;
return this;
}
public Builder existingDocument(HDocument document)
{
this.existingDocument = document;
Expand Down Expand Up @@ -469,6 +497,7 @@ public Builder setSimpleUpload()
fileStream = new ByteArrayInputStream(basicDocumentContent.getBytes());
hash = hashOfBasicDocumentContent;
params = "params";
storedParams = "stored params";

existingDocument = null;

Expand Down

0 comments on commit 7b56541

Please sign in to comment.