Search in sources :

Example 31 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class BaseModelTest method sendOperationSynchronously.

public Operation sendOperationSynchronously(Operation op) throws Throwable {
    final Operation[] returnedOp = { null };
    TestContext ctx = this.host.testCreate(1);
    op.setCompletion((operation, throwable) -> {
        returnedOp[0] = operation;
        if (throwable != null) {
            ctx.failIteration(throwable);
        } else {
            ctx.completeIteration();
        }
    });
    this.host.send(op);
    this.host.testWait(ctx);
    return returnedOp[0];
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Example 32 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class BaseModelTest method postServiceSynchronously.

public <T extends ServiceDocument> T postServiceSynchronously(String serviceUri, T body, Class<T> type, Class<? extends Throwable> expectedException) throws Throwable {
    AtomicReference<T> responseBody = new AtomicReference<>();
    TestContext ctx = this.host.testCreate(1);
    Operation postOperation = Operation.createPost(UriUtils.buildUri(this.host, serviceUri)).setBody(body).setCompletion((operation, throwable) -> {
        boolean failureExpected = (expectedException != null);
        boolean failureReturned = (throwable != null);
        if (failureExpected ^ failureReturned) {
            Throwable t = throwable == null ? new IllegalArgumentException("Call did not fail as expected") : throwable;
            ctx.failIteration(t);
            return;
        }
        if (failureExpected && expectedException != throwable.getClass()) {
            ctx.failIteration(throwable);
            return;
        }
        if (!failureExpected) {
            responseBody.set(operation.getBody(type));
        }
        ctx.completeIteration();
    });
    this.host.send(postOperation);
    this.host.testWait(ctx);
    return responseBody.get();
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Operation(com.vmware.xenon.common.Operation)

Example 33 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class BaseModelTest method patchServiceSynchronously.

public <T extends ServiceDocument> int patchServiceSynchronously(String serviceUri, DiskInstanceRequest patchBody) throws Throwable {
    TestContext ctx = this.host.testCreate(1);
    Operation patchOperation = Operation.createPatch(UriUtils.buildUri(this.host, serviceUri)).setBody(patchBody).setCompletion(ctx.getCompletion());
    this.host.send(patchOperation);
    this.testWait(ctx);
    return patchOperation.getStatusCode();
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Example 34 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class BaseModelTest method putServiceSynchronously.

public <T extends ServiceDocument> void putServiceSynchronously(String serviceUri, T putBody) throws Throwable {
    TestContext ctx = this.host.testCreate(1);
    Operation putOperation = Operation.createPut(UriUtils.buildUri(this.host, serviceUri)).setBody(putBody).setCompletion(ctx.getCompletion());
    this.host.send(putOperation);
    this.host.testWait(ctx);
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Example 35 with TestContext

use of com.vmware.xenon.common.test.TestContext in project photon-model by vmware.

the class AzurePowerServiceTest method triggerPowerChange.

private void triggerPowerChange(PowerState powerState, URI powerAdapterReference) {
    String taskLink = UUID.randomUUID().toString();
    ComputePowerRequest powerRequest = new ComputePowerRequest();
    powerRequest.isMockRequest = this.isMock;
    powerRequest.powerState = powerState;
    powerRequest.resourceReference = UriUtils.buildUri(this.host, this.vmState.documentSelfLink);
    powerRequest.taskReference = UriUtils.buildUri(this.host, taskLink);
    TestContext ctx = this.host.testCreate(2);
    createTaskResultListener(this.host, taskLink, (u) -> {
        if (u.getAction() != Action.PATCH) {
            return false;
        }
        ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
        if (TaskState.isFailed(response.taskInfo)) {
            ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
        } else if (TaskState.isFinished(response.taskInfo)) {
            ctx.completeIteration();
        }
        return true;
    });
    Operation powerOp = Operation.createPatch(powerAdapterReference).setBody(powerRequest).setReferer("/tests").setCompletion((o, e) -> {
        if (e != null) {
            ctx.failIteration(e);
            return;
        }
        ctx.completeIteration();
    });
    this.host.send(powerOp);
    ctx.await();
}
Also used : ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Aggregations

TestContext (com.vmware.xenon.common.test.TestContext)43 Operation (com.vmware.xenon.common.Operation)37 Test (org.junit.Test)18 ResourceOperationResponse (com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)14 UriUtils (com.vmware.xenon.common.UriUtils)13 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)13 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)12 After (org.junit.After)12 Assert.assertEquals (org.junit.Assert.assertEquals)12 Before (org.junit.Before)12 CompletionException (java.util.concurrent.CompletionException)11 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)10 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)10 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)10 CommandLineArgumentParser (com.vmware.xenon.common.CommandLineArgumentParser)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Duration (java.time.Duration)9 HashMap (java.util.HashMap)9 Level (java.util.logging.Level)9