Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Commit

Permalink
Fix OfflineAuth serializable issue
Browse files Browse the repository at this point in the history
With a test so that this doesn’t happen again.
  • Loading branch information
cassiedoll committed Dec 2, 2014
1 parent e624550 commit 7244aa4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -513,22 +513,17 @@ public static class OfflineAuth implements Serializable {
public String clientSecretsString;
public String apiKey;

private GenomicsFactory factory;

public GenomicsFactory getFactory() throws GeneralSecurityException, IOException {
if (factory == null) {
factory = GenomicsFactory.builder(applicationName).build();
}
return factory;
public GenomicsFactory getDefaultFactory() throws GeneralSecurityException, IOException {
return GenomicsFactory.builder(applicationName).build();
}

/**
* Create a {@link Genomics} stub.
*
* @return The new {@code Genomics} stub
* @param factory The factory used to generate the Genomics object
*/
public Genomics getGenomics() throws IOException, GeneralSecurityException {
GenomicsFactory factory = getFactory();
public Genomics getGenomics(GenomicsFactory factory) throws IOException {
if (clientSecretsString == null) {
return factory.fromApiKey(apiKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

import static org.junit.Assert.assertEquals;

@RunWith(JUnit4.class)
Expand Down Expand Up @@ -58,20 +61,32 @@ public void testOfflineAuth() throws Exception {
GenomicsFactory genomicsFactory = GenomicsFactory.builder("test_client").build();
GenomicsFactory.OfflineAuth auth = genomicsFactory.getOfflineAuth("xyz", null);

assertEquals(0, auth.getFactory().initializedRequestsCount());
GenomicsFactory authFactory = auth.getDefaultFactory();
assertEquals(0, authFactory.initializedRequestsCount());

try {
auth.getGenomics().jobs().get("123").execute();
auth.getGenomics(authFactory).jobs().get("123").execute();
} catch (GoogleJsonResponseException e) {
// Expected
}
assertEquals(1, auth.getFactory().initializedRequestsCount());
assertEquals(1, authFactory.initializedRequestsCount());

try {
auth.getGenomics().jobs().get("123").execute();
auth.getGenomics(authFactory).jobs().get("123").execute();
} catch (GoogleJsonResponseException e) {
// Expected
}
assertEquals(2, auth.getFactory().initializedRequestsCount());
assertEquals(2, authFactory.initializedRequestsCount());
}

@Test
public void testOfflineAuth_isSerializable() throws Exception {
GenomicsFactory genomicsFactory = GenomicsFactory.builder("test_client").build();
GenomicsFactory.OfflineAuth auth = genomicsFactory.getOfflineAuth("xyz", null);

// This mimics the serialization flow used by pipelines
auth.getGenomics(auth.getDefaultFactory());
ObjectOutputStream oos = new ObjectOutputStream(new ByteArrayOutputStream());
oos.writeObject(auth);
}
}

0 comments on commit 7244aa4

Please sign in to comment.