@Override public void handleStart(Operation post) { if (!post.hasBody()) { post.fail(new IllegalArgumentException("body is required")); return; } TenantState newState = post.getBody(TenantState.class); if (newState.id == null || newState.id.isEmpty()) { newState.id = UUID.randomUUID().toString(); } post.setBody(newState).complete(); }
private void sendHostServiceRequest(Object body, URI adapterReference) { sendRequest(Operation.createPatch(adapterReference).setBody(body) .setCompletion((o, e) -> { if (e != null) { failTask(e); return; } })); }
private void failTask(Throwable e, Operation directOp, CompletionHandler c) { QueryTask t = new QueryTask(); // self patch to failure t.taskInfo.stage = TaskStage.FAILED; t.taskInfo.failure = Utils.toServiceErrorResponse(e); if (directOp != null) { directOp.setBody(t).fail(e); return; } sendRequest(Operation.createPatch(getUri()).setBody(t).setCompletion(c)); }
private void updateContainerHostInfo(String documentSelfLink) { URI uri = UriUtils.buildUri(getHost(), ContainerHostDataCollectionService.HOST_INFO_DATA_COLLECTION_LINK); ContainerHostDataCollectionState state = new ContainerHostDataCollectionState(); state.createOrUpdateHost = true; state.computeContainerHostLinks = Collections.singleton(documentSelfLink); sendRequest(Operation.createPatch(uri) .setBody(state) .setCompletion((o, ex) -> { if (ex != null) { logWarning("Failed to update host data collection: %s", ex.getMessage()); } })); }
@Test public void testGetDataNotExisting() { LongURIRequest body = new LongURIRequest(); body.uri = "/resources/fake?%24filter=documentSelfLink%20eq%20'%2Fresources%2Fpools%2Fdefault-placement-zone'&documentType=true&expand=true"; host.sendAndWaitExpectFailure(Operation .createPost(UriUtils.buildUri(host.getUri(), LongURIGetService.SELF_LINK)) .setBody(body), Operation.STATUS_CODE_NOT_FOUND); }
@Test public void testFactoryPostAndDelete() { UserState state = new UserState(); state.email = "jane@doe.com"; Operation op = Operation.createPost(this.host, ServiceUriPaths.CORE_AUTHZ_USERS).setBody(state); UserState outState = this.sender.sendAndWait(op, UserState.class); assertEquals(state.email, outState.email); }
private void sendSynchronizationRequest( String factoryLink, Operation factorySynchPost, Operation op) { URI uri = UriUtils.buildUri(factorySynchPost.getUri(), factoryLink); uri = UriUtils.extendUri(uri, ServiceHost.SERVICE_URI_SUFFIX_SYNCHRONIZATION); factorySynchPost.setUri(uri); factorySynchPost.setCompletion((o, e) -> { if (e != null) { this.log(Level.WARNING, "Failed to start factory synchronization: %s", e); op.fail(e); return; } op.setBody(o.getBodyRaw()); op.complete(); }).sendWith(this); }
protected void startInitialBootService( Class<? extends AbstractInitialBootService> serviceClass, String bootServiceSelfLink) throws Throwable { // simulate a restart of the service host host.startServiceAndWait(serviceClass, bootServiceSelfLink); TestContext ctx = testCreate(1); // start initialization of system documents host.sendRequest(Operation.createPost( UriUtils.buildUri(host, serviceClass)) .setReferer(host.getUri()) .setBody(new ServiceDocument()) .setCompletion(ctx.getCompletion())); ctx.await(); } }
private void postEventlogError(ComputeState hostState, String message) { EventLogState eventLog = new EventLogState(); eventLog.description = message == null ? "Unexpected error" : message; eventLog.eventLogType = EventLogType.ERROR; eventLog.resourceType = getClass().getName(); eventLog.tenantLinks = hostState.tenantLinks; sendRequest(Operation.createPost(this, EventLogService.FACTORY_LINK) .setBody(eventLog) .setCompletion((o, e) -> { if (e != null) { logWarning("Failed to create event log: %s", Utils.toString(e)); } })); }
public <T extends ServiceDocument> T verifyPost(Class<T> documentType, String factoryLink, T state, int expectedStatusCode) { URI uri = UriUtils.buildUri(this, factoryLink); Operation op = Operation.createPost(uri).setBody(state); Operation response = this.sender.sendAndWait(op); String message = String.format("Status code expected: %s, actual: %s", expectedStatusCode, response.getStatusCode()); assertEquals(message, expectedStatusCode, response.getStatusCode()); return response.hasBody() ? response.getBody(documentType) : null; }
@Override public void handlePatch(Operation patch) { QueryValidationServiceState body = patch .getBody(QueryValidationServiceState.class); QueryValidationServiceState currentState = getState(patch); currentState.documentExpirationTimeMicros = body.documentExpirationTimeMicros; currentState.serviceLink = body.serviceLink; currentState.serviceLinks = body.serviceLinks; patch.setBody(null).complete(); }
private void deleteServices(List<URI> targetServiceURIs) throws Throwable { this.host.testStart(targetServiceURIs.size()); for (URI u : targetServiceURIs) { this.host.send(Operation.createDelete(u) .setBody(new ServiceDocument()) .setCompletion(this.host.getCompletion())); } this.host.testWait(); }
private void doPost(VerificationHost h, ExampleServiceState doc, Consumer<Throwable> callback) { h.send(Operation .createPost(this.factoryUri) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_FORCE_INDEX_UPDATE) .setBody(doc) .setCompletion((o, e) -> { if (e != null) { callback.accept(e); } else { callback.accept(null); } })); }
@Test public void testEmptyStage() { EventTopicState state = createEventTopicState(null, EVENT_NAME, EVENT_TASK, null, DefaultSubStage.COMPLETED.name(), false, new String()); URI uri = UriUtils.buildUri(host, EventTopicService.FACTORY_LINK); Operation op = Operation .createPost(uri) .setBody(state); FailureResponse failure = sender.sendAndWaitFailure(op); assertNotNull(failure); assertEquals("'Stage' is required.", failure.failure.getMessage()); }
@Test public void testFactoryPostFailure() { UserState state = new UserState(); state.email = "not an email"; Operation op = Operation.createPost(this.host, ServiceUriPaths.CORE_AUTHZ_USERS).setBody(state); FailureResponse response = this.sender.sendAndWaitFailure(op); assertEquals(Operation.STATUS_CODE_FAILURE_THRESHOLD, response.op.getStatusCode()); assertEquals("email is invalid", response.failure.getMessage()); }
private void performPut(Operation put, CompositeDescription putBody) { try { AssertUtil.assertTrue(putBody.getClass().equals(CompositeDescription.class), "State should be instance of CompositeDescription, not of any subclass."); this.setState(put, putBody); put.setBody(putBody).complete(); } catch (Throwable e) { put.fail(e); } }
private void getExternalPopularImages(Operation get, Runnable notFoundHandler) { sendRequest(Operation.createGet(UriUtils.buildUri(getHost(), EXTERNAL_LINK)) .setCompletion((op, ex) -> { if (ex != null || op.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) { notFoundHandler.run(); } else { get.setBody(op.getBodyRaw()); get.setStatusCode(op.getStatusCode()); get.setContentType(Operation.MEDIA_TYPE_APPLICATION_JSON); get.complete(); } })); }
private void sendHostServiceRequest(Object body, URI adapterReference) { sendRequest(Operation.createPatch(adapterReference).setBody(body) .setCompletion((o, e) -> { if (e != null) { failTask(e); return; } })); }
private void failTask(Throwable e, Operation directOp, Operation.CompletionHandler c) { QueryTask t = new QueryTask(); // self patch to failure t.taskInfo.stage = TaskState.TaskStage.FAILED; t.taskInfo.failure = Utils.toServiceErrorResponse(e); if (directOp != null) { directOp.setBody(t).fail(e); return; } sendRequest(Operation.createPatch(getUri()).setBody(t).setCompletion(c)); }
private void sendSynchRequest(URI parentUri, String documentSelfLink, CompletionHandler ch) { ServiceDocument synchState = new ServiceDocument(); synchState.documentSelfLink = documentSelfLink; Operation synchOp = Operation .createPost(this.host, parentUri.getPath()) .setBody(synchState) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_SYNCH_OWNER) .setReferer(this.host.getUri()) .setExpiration(Utils.fromNowMicrosUtc(NodeGroupService.PEER_REQUEST_TIMEOUT_MICROS)) .setCompletion((o, e) -> ch.handle(o, e)); this.host.handleRequest(null, synchOp); }