如何模拟Google API AndroidPublisher请求

时间:2016-10-25 22:03:34

标签: java android google-api-client google-api-java-client

我有一个类向AndroidPublisher API发出请求以获取Purchases.Subscriptions.get资源。如何使用com.google.api.client.testing包中的类来模拟OAuth请求并调用以获取订阅资源?

以下是获取订阅的代码:

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();

以下是我目前的情况,但未成功,试图模拟JSON响应:

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)

当我使用模拟类运行测试时,我得到以下stacktrace:

{{1}}

我错过了什么?如何模拟OAuth2响应以及获取订阅的调用?

2 个答案:

答案 0 :(得分:2)

我能够找出问题所在。除了嘲笑HttpTransport之外,我还必须提供一个模拟GoogleCredential,如:

new MockGoogleCredential.Builder().build();

答案 1 :(得分:0)

@Sukhmeet Singh我不确定你的部署环境是什么,但我们正在使用Spring Boot。我在@Configuration文件中声明了以下spring bean。

@Bean
public HttpTransport httpTransport() {
    return new NetHttpTransport.Builder().build();
}

@Bean
public JsonFactory jsonFactory() {
    return JacksonFactory.getDefaultInstance();
}

@Bean
public GoogleCredential googleCredential(
        final HttpTransport httpTransport,
        final JsonFactory jsonFactory) {
    final String serviceAccountUserName = env.getRequiredProperty("<redacted>");
    final String serviceAccountPrivateKeyFile = env.getRequiredProperty("<redacted>");
    try {
        return new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(serviceAccountUserName)
                .setServiceAccountScopes(singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
                .setServiceAccountPrivateKeyFromP12File(new File(serviceAccountPrivateKeyFile))
                .build();
    } catch (final Exception ex) {
        throw new RuntimeException(ex);
    }
}

@Bean
public AndroidPublisher androidPublisher(
        final GoogleAppPackageName googleAppPackageName,
        final HttpTransport httpTransport,
        final JsonFactory jsonFactory,
        final GoogleCredential googleCredential) {
    return new AndroidPublisher.Builder(httpTransport, jsonFactory, googleCredential)
            .setApplicationName(googleAppPackageName.getValue())
            .build();
}

然后在我的集成测试中,特别是我们使用的是cucumber-jvm,我有另一个测试特定的@Configuration文件,它使用spring注释@Primary覆盖那些bean。

@Primary
@Bean
public GoogleCredential googleCredential() {
    return new MockGoogleCredential.Builder().build();
}

@Primary
@Bean
public HttpTransport httpTransport() {
    final MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
    mockResponse.setStatusCode(200);
    mockResponse.setContentType(Json.MEDIA_TYPE);
    mockResponse.setContent("<desired JSON response>");

    final MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url);
    request.setResponse(mockResponse);
    return request;
}

现在我有一些其他的课程可以帮助我模拟不同场景的多个响应,但这应该给你足够的想法来解决它。

希望它有所帮助。