Search in sources :

Example 1 with Operation

use of com.google.api.services.appengine.v1.model.Operation in project google-cloud-intellij by GoogleCloudPlatform.

the class GoogleApiClientAppEngineAdminService method createApplication.

@Override
public Application createApplication(@NotNull String locationId, @NotNull final String projectId, @NotNull final Credential credential) throws IOException, GoogleApiException {
    Application arg = new Application();
    arg.setId(projectId);
    arg.setLocationId(locationId);
    Apps.Create createRequest = GoogleApiClientFactory.getInstance().getAppEngineApiClient(credential).apps().create(arg);
    Operation operation;
    try {
        // make the initial request to create the application
        operation = createRequest.execute();
        // poll for updates while the application is being created
        boolean done = false;
        while (!done) {
            try {
                Thread.sleep(CREATE_APPLICATION_POLLING_INTERVAL_MS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            operation = getOperation(projectId, operation.getName(), credential);
            if (operation.getDone() != null) {
                done = operation.getDone();
            }
        }
    } catch (GoogleJsonResponseException e) {
        throw GoogleApiException.from(e);
    }
    if (operation.getError() != null) {
        Status status = operation.getError();
        throw new GoogleApiException(status.getMessage(), status.getCode());
    } else {
        Application result = new Application();
        result.putAll(operation.getResponse());
        return result;
    }
}
Also used : Status(com.google.api.services.appengine.v1.model.Status) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Operation(com.google.api.services.appengine.v1.model.Operation) Application(com.google.api.services.appengine.v1.model.Application) Apps(com.google.api.services.appengine.v1.Appengine.Apps)

Example 2 with Operation

use of com.google.api.services.appengine.v1.model.Operation in project google-cloud-intellij by GoogleCloudPlatform.

the class GoogleApiClientAppEngineAdminServiceTest method testCreateApplication_operationFailed.

@Test
public void testCreateApplication_operationFailed() throws IOException, GoogleApiException {
    String operationName = "apps/-/operations/12345";
    Operation inProgressOperation = buildInProgressOperation(operationName);
    when(appengineClientMock.getAppsCreateQuery().execute()).thenReturn(inProgressOperation);
    String errorMessage = "The operation failed.";
    int errorCode = 400;
    Status status = new Status();
    status.setMessage(errorMessage);
    status.setCode(errorCode);
    Operation failedOperation = new Operation();
    failedOperation.setError(status);
    failedOperation.setDone(true);
    failedOperation.setName(operationName);
    when(appengineClientMock.getAppsCreateQuery().execute()).thenReturn(inProgressOperation);
    when(appengineClientMock.getAppsOperationsGetQuery().execute()).thenReturn(failedOperation);
    try {
        service.createApplication("us-east", "my-project-id", mock(Credential.class));
    } catch (GoogleApiException expected) {
        assertEquals(errorCode, expected.getStatusCode());
        assertEquals(errorMessage, expected.getMessage());
        return;
    }
    fail();
}
Also used : Status(com.google.api.services.appengine.v1.model.Status) Credential(com.google.api.client.auth.oauth2.Credential) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Operation(com.google.api.services.appengine.v1.model.Operation) Test(org.junit.Test)

Example 3 with Operation

use of com.google.api.services.appengine.v1.model.Operation in project google-cloud-intellij by GoogleCloudPlatform.

the class GoogleApiClientAppEngineAdminServiceTest method testCreateApplication.

@Test
public void testCreateApplication() throws IOException, GoogleApiException {
    String operationId = "my-operation-id";
    String operationName = "apps/-/operations/" + operationId;
    Operation inProgressOperation = buildInProgressOperation(operationName);
    when(appengineClientMock.getAppsCreateQuery().execute()).thenReturn(inProgressOperation);
    final String locationId = "us-east1";
    final String projectId = "my-project";
    Map<String, Object> response = new HashMap<>();
    response.put("name", projectId);
    response.put("locationId", locationId);
    Operation doneOperation = new Operation();
    doneOperation.setName(operationName);
    doneOperation.setDone(true);
    doneOperation.setResponse(response);
    // require polling several times
    when(appengineClientMock.getAppsOperationsGetQuery().execute()).thenReturn(inProgressOperation).thenReturn(inProgressOperation).thenReturn(doneOperation);
    Application result = service.createApplication(locationId, projectId, mock(Credential.class));
    // ensure the 'getOperation' API call(s) were made correctly
    verify(appengineClientMock.apps().operations(), atLeastOnce()).get(eq(projectId), eq(operationId));
    // ensure the 'createApplication' API call was made with the correct args
    verify(appengineClientMock.apps(), times(1)).create(argThat(application -> application.getId().equals(projectId) && application.getLocationId().equals(locationId)));
    // ensure the 'createApplication' API call was only made once
    verify(appengineClientMock.getAppsCreateQuery(), times(1)).execute();
    assertEquals(projectId, result.getName());
    assertEquals(locationId, result.getLocationId());
}
Also used : Assert.fail(junit.framework.Assert.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assert.assertEquals(junit.framework.Assert.assertEquals) Arrays(java.util.Arrays) Application(com.google.api.services.appengine.v1.model.Application) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Location(com.google.api.services.appengine.v1.model.Location) Mockito.doThrow(org.mockito.Mockito.doThrow) MockitoAnnotations(org.mockito.MockitoAnnotations) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) Credential(com.google.api.client.auth.oauth2.Credential) Before(org.junit.Before) AppEngineApplicationNotFoundException(com.google.cloud.tools.intellij.appengine.application.GoogleApiClientAppEngineAdminService.AppEngineApplicationNotFoundException) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) HttpTransport(com.google.api.client.http.HttpTransport) ListLocationsResponse(com.google.api.services.appengine.v1.model.ListLocationsResponse) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) GoogleApiClientFactory(com.google.cloud.tools.intellij.resources.GoogleApiClientFactory) Mockito.verify(org.mockito.Mockito.verify) Operation(com.google.api.services.appengine.v1.model.Operation) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) JsonFactory(com.google.api.client.json.JsonFactory) Appengine(com.google.api.services.appengine.v1.Appengine) Status(com.google.api.services.appengine.v1.model.Status) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) BasePluginTestCase(com.google.cloud.tools.intellij.testing.BasePluginTestCase) Credential(com.google.api.client.auth.oauth2.Credential) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Operation(com.google.api.services.appengine.v1.model.Operation) Application(com.google.api.services.appengine.v1.model.Application) Test(org.junit.Test)

Example 4 with Operation

use of com.google.api.services.appengine.v1.model.Operation in project google-cloud-intellij by GoogleCloudPlatform.

the class GoogleApiClientAppEngineAdminServiceTest method buildInProgressOperation.

private Operation buildInProgressOperation(String operationName) {
    Operation inProgressOperation = new Operation();
    inProgressOperation.setName(operationName);
    inProgressOperation.setDone(false);
    return inProgressOperation;
}
Also used : Operation(com.google.api.services.appengine.v1.model.Operation)

Aggregations

Operation (com.google.api.services.appengine.v1.model.Operation)4 Status (com.google.api.services.appengine.v1.model.Status)3 Credential (com.google.api.client.auth.oauth2.Credential)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 Application (com.google.api.services.appengine.v1.model.Application)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 HttpTransport (com.google.api.client.http.HttpTransport)1 JsonFactory (com.google.api.client.json.JsonFactory)1 Appengine (com.google.api.services.appengine.v1.Appengine)1 Apps (com.google.api.services.appengine.v1.Appengine.Apps)1 ListLocationsResponse (com.google.api.services.appengine.v1.model.ListLocationsResponse)1 Location (com.google.api.services.appengine.v1.model.Location)1 AppEngineApplicationNotFoundException (com.google.cloud.tools.intellij.appengine.application.GoogleApiClientAppEngineAdminService.AppEngineApplicationNotFoundException)1 GoogleApiClientFactory (com.google.cloud.tools.intellij.resources.GoogleApiClientFactory)1 BasePluginTestCase (com.google.cloud.tools.intellij.testing.BasePluginTestCase)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1