I've got a class that makes a request to the AndroidPublisher API to get the Purchases.Subscriptions.get resource. How does one mock out the OAuth request and call to get the subscription resource using the classes from the package com.google.api.client.testing?
Here is the code to get the subscription:
HttpTransport httpTransport = new NetHttpTransport.Builder().build();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("email")
.setServiceAccountScopes(singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
.setServiceAccountPrivateKeyFromP12File(new File("file.p12"))
.build();
AndroidPublisher androidPublisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("appname")
.build();
AndroidPublisher.Purchases.Subscriptions.Get get = androidPublisher.purchases().subscriptions().get(
"appname",
"productId",
"purchaseToken");
SubscriptionPurchase subscription = get.execute();
System.out.println(subscription.toPrettyString());
Here is how I am currently, but unsuccessfully, trying to mock out the JSON responses:
MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
mockResponse.setStatusCode(200);
mockResponse.setContentType(Json.MEDIA_TYPE);
mockResponse.setContent("{\n" +
" \"autoRenewing\" : true,\n" +
" \"countryCode\" : \"US\",\n" +
" \"developerPayload\" : \"\",\n" +
" \"expiryTimeMillis\" : \"1480019001357\",\n" +
" \"kind\" : \"androidpublisher#subscriptionPurchase\",\n" +
" \"paymentState\" : 1,\n" +
" \"priceAmountMicros\" : \"990000\",\n" +
" \"priceCurrencyCode\" : \"USD\",\n" +
" \"startTimeMillis\" : \"1477333413210\"\n" +
"}");
return new MockHttpTransport.Builder().setLowLevelHttpResponse(mockResponse).build();
When I run the test with the mock classes I get the following stacktrace:
java.lang.IllegalArgumentException: no JSON input found
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:125)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:49)
at com.google.api.client.json.JsonParser.startParsing(JsonParser.java:223)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:380)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:355)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
What am I missing? How does one mock out the OAuth2 response as well as the call to get the subscription?