public MinimalTestServiceState buildMinimalTestState(int bytes) { MinimalTestServiceState minState = new MinimalTestServiceState(); minState.id = new Operation().getId() + ""; byte[] body = new byte[bytes]; new Random().nextBytes(body); minState.stringValue = DatatypeConverter.printBase64Binary(body); return minState; }
public MinimalTestServiceState buildMinimalTestState(int bytes) { MinimalTestServiceState minState = new MinimalTestServiceState(); minState.id = new Operation().getId() + ""; byte[] body = new byte[bytes]; new Random().nextBytes(body); minState.stringValue = DatatypeConverter.printBase64Binary(body); return minState; }
/** * If we receive a get with the "headers" query, we serialize the incoming * headers and return this. This allows us to test that the right headers * were sent. */ private void respondWithHeaders(Operation get) { StringBuilder sb = new StringBuilder(); Map<String, String> headers = get.getRequestHeaders(); for (Map.Entry<String, String> header : headers.entrySet()) { sb.append(header.getKey()); sb.append(":"); sb.append(header.getValue()); sb.append("\n"); } MinimalTestServiceState state = new MinimalTestServiceState(); state.stringValue = sb.toString(); get.setBody(state).complete(); }
/** * If we receive a get with the "headers" query, we serialize the incoming * headers and return this. This allows us to test that the right headers * were sent. */ private void respondWithHeaders(Operation get) { StringBuilder sb = new StringBuilder(); Map<String, String> headers = get.getRequestHeaders(); for (Map.Entry<String, String> header : headers.entrySet()) { sb.append(header.getKey()); sb.append(":"); sb.append(header.getValue()); sb.append("\n"); } MinimalTestServiceState state = new MinimalTestServiceState(); state.stringValue = sb.toString(); get.setBody(state).complete(); }
@Test public void testQueryDocument() throws Throwable { this.minimalTestStates = queryDocument("testLink"); assertEquals(0, this.minimalTestStates.size()); MinimalTestServiceState minimalTestState = new MinimalTestServiceState(); minimalTestState.id = this.idValue1; minimalTestState = doPost(minimalTestState); this.minimalTestStates = queryDocument(minimalTestState.documentSelfLink); assertEquals(1, this.minimalTestStates.size()); assertEquals(minimalTestState.documentSelfLink, this.minimalTestStates.get(0).documentSelfLink); assertEquals(this.idValue1, this.minimalTestStates.get(0).id); delete(minimalTestState); this.minimalTestStates = queryDocument(minimalTestState.documentSelfLink); assertEquals(0, this.minimalTestStates.size()); }
@Test public void testQueryDocument() throws Throwable { this.minimalTestStates = queryDocument("testLink"); assertEquals(0, this.minimalTestStates.size()); MinimalTestServiceState minimalTestState = new MinimalTestServiceState(); minimalTestState.id = this.idValue1; minimalTestState = doPost(minimalTestState); this.minimalTestStates = queryDocument(minimalTestState.documentSelfLink); assertEquals(1, this.minimalTestStates.size()); assertEquals(minimalTestState.documentSelfLink, this.minimalTestStates.get(0).documentSelfLink); assertEquals(this.idValue1, this.minimalTestStates.get(0).id); delete(minimalTestState); this.minimalTestStates = queryDocument(minimalTestState.documentSelfLink); assertEquals(0, this.minimalTestStates.size()); }
Operation createMinimalTestServicePatch(String servicePath, TestContext ctx) { MinimalTestServiceState body = new MinimalTestServiceState(); body.id = Utils.buildUUID("foo"); Operation patch = Operation .createPatch(UriUtils.buildUri(this.host, servicePath)) .setBody(body); if (ctx != null) { patch.setCompletion(ctx.getCompletion()); } return patch; }
Operation createMinimalTestServicePatch(String servicePath, TestContext ctx) { MinimalTestServiceState body = new MinimalTestServiceState(); body.id = Utils.buildUUID("foo"); Operation patch = Operation .createPatch(UriUtils.buildUri(this.host, servicePath)) .setBody(body); if (ctx != null) { patch.setCompletion(ctx.getCompletion()); } return patch; }
@Test public void testQueryDocumentWithBaseUri() throws Throwable { try { this.queryHelper.setBaseUri(null); fail("IllegalArgumentException expected with null base URI."); } catch (IllegalArgumentException e) { // expected } this.minimalTestStates = queryDocumentWithBaseUri("testLink"); assertEquals(0, this.minimalTestStates.size()); MinimalTestServiceState minimalTestState = new MinimalTestServiceState(); minimalTestState.id = this.idValue1; minimalTestState = doPost(minimalTestState); this.minimalTestStates = queryDocumentWithBaseUri(minimalTestState.documentSelfLink); assertEquals(1, this.minimalTestStates.size()); assertEquals(minimalTestState.documentSelfLink, this.minimalTestStates.get(0).documentSelfLink); assertEquals(this.idValue1, this.minimalTestStates.get(0).id); delete(minimalTestState); this.minimalTestStates = queryDocumentWithBaseUri(minimalTestState.documentSelfLink); assertEquals(0, this.minimalTestStates.size()); }
@Test public void testQueryDocumentUsingSendWithService() throws Throwable { this.minimalTestStates = queryDocument("testLink"); assertEquals(0, this.minimalTestStates.size()); MinimalTestServiceState minimalTestState = new MinimalTestServiceState(); minimalTestState.id = this.idValue1; minimalTestState = doPost(minimalTestState); // Query document using sendWith(service) assertEquals(1, this.services.size()); this.host.testStart(1); this.queryHelper.setDocumentLink(minimalTestState.documentSelfLink) .setResultHandler(handler()) .sendWith(this.services.get(0)); this.host.testWait(); assertEquals(1, this.minimalTestStates.size()); assertEquals(minimalTestState.documentSelfLink, this.minimalTestStates.get(0).documentSelfLink); assertEquals(this.idValue1, this.minimalTestStates.get(0).id); delete(minimalTestState); this.minimalTestStates = queryDocument(minimalTestState.documentSelfLink); assertEquals(0, this.minimalTestStates.size()); }
@Ignore("https://jira.eng.vmware.com/browse/VRXEN-65") @Test public void testSendWithDeferredResultWithNoResponseBody() throws Throwable { MinimalTestServiceState initialState = this.host.buildMinimalTestState(10); Service service = this.host.doThroughputServiceStart( 1, MinimalTestService.class, initialState, EnumSet.noneOf(Service.ServiceOption.class), null).get(0); // patch with same state to receive status NOT MODIFIED(304) and empty body MinimalTestServiceState patchState = new MinimalTestServiceState(); patchState.documentSelfLink = service.getSelfLink(); patchState.id = initialState.id; this.host.testStart(1); DeferredResult<MinimalTestServiceState> deferredResult = this.host .sendWithDeferredResult( Operation .createPatch(this.host, patchState.documentSelfLink) .setBodyNoCloning(patchState), MinimalTestServiceState.class) .whenComplete(this.host.getCompletionDeferred()); this.host.testWait(); Assert.assertNull(deferredResult.getNow(new MinimalTestServiceState())); }
@Test public void initialStateCorrectness() throws Throwable { setUpHost(false); List<MinimalTestService> services = new ArrayList<>(); TestContext ctx = this.host.testCreate(this.serviceCount); for (int i = 0; i < this.serviceCount; i++) { MinimalTestService s = new MinimalTestService(); s.toggleOption(ServiceOption.PERSISTENCE, true); MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.id = UUID.randomUUID().toString(); Operation post = Operation.createPost(this.host, Utils.getNowMicrosUtc() + "") .setBody(initialState) .setCompletion(ctx.getCompletion()); this.host.startService(post, s); services.add(s); } this.host.testWait(ctx); for (MinimalTestService s : services) { assertTrue(!s.isStateModifiedPostCompletion); } }
@Test public void initialStateCorrectness() throws Throwable { setUpHost(false); List<MinimalTestService> services = new ArrayList<>(); TestContext ctx = this.host.testCreate(this.serviceCount); for (int i = 0; i < this.serviceCount; i++) { MinimalTestService s = new MinimalTestService(); s.toggleOption(ServiceOption.PERSISTENCE, true); MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.id = UUID.randomUUID().toString(); Operation post = Operation.createPost(this.host, Utils.getNowMicrosUtc() + "") .setBody(initialState) .setCompletion(ctx.getCompletion()); this.host.startService(post, s); services.add(s); } this.host.testWait(ctx); for (MinimalTestService s : services) { assertTrue(!s.isStateModifiedPostCompletion); } }
@Test public void serviceStartExpiration() throws Throwable { setUp(false); long maintenanceIntervalMicros = TimeUnit.MILLISECONDS.toMicros(100); // set a small period so its pretty much guaranteed to execute // maintenance during this test this.host.setMaintenanceIntervalMicros(maintenanceIntervalMicros); // start a service but tell it to not complete the start POST. This will induce a timeout // failure from the host MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.id = MinimalTestService.STRING_MARKER_TIMEOUT_REQUEST; this.host.testStart(1); Operation startPost = Operation .createPost(UriUtils.buildUri(this.host, UUID.randomUUID().toString())) .setExpiration(Utils.fromNowMicrosUtc(maintenanceIntervalMicros)) .setBody(initialState) .setCompletion(this.host.getExpectedFailureCompletion()); this.host.startService(startPost, new MinimalTestService()); this.host.testWait(); }
@Test public void operationWithoutContextId() throws Throwable { this.services = this.host.doThroughputServiceStart(1, MinimalTestService.class, this.host.buildMinimalTestState(), EnumSet.noneOf(Service.ServiceOption.class), null); // issue a patch request to verify the contextId is 'null' MinimalTestServiceState body = new MinimalTestServiceState(); body.id = MinimalTestService.STRING_MARKER_HAS_CONTEXT_ID; this.host.testStart(1); this.host.send(Operation .createPatch(this.services.get(0).getUri()) .forceRemote() .setBody(body) .setCompletion((o, e) -> { if (e != null) { this.host.completeIteration(); return; } this.host.failIteration(new IllegalStateException( "Request should have failed due to missing contextId")); })); this.host.testWait(); }
@Test public void serviceStartExpiration() throws Throwable { setUp(false); long maintenanceIntervalMicros = TimeUnit.MILLISECONDS.toMicros(100); // set a small period so its pretty much guaranteed to execute // maintenance during this test this.host.setMaintenanceIntervalMicros(maintenanceIntervalMicros); // start a service but tell it to not complete the start POST. This will induce a timeout // failure from the host MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.id = MinimalTestService.STRING_MARKER_TIMEOUT_REQUEST; this.host.testStart(1); Operation startPost = Operation .createPost(UriUtils.buildUri(this.host, UUID.randomUUID().toString())) .setExpiration(Utils.fromNowMicrosUtc(maintenanceIntervalMicros)) .setBody(initialState) .setCompletion(this.host.getExpectedFailureCompletion()); this.host.startService(startPost, new MinimalTestService()); this.host.testWait(); }
@Test public void testBaseHelperMethods() throws Throwable { MinimalTestServiceState body = new MinimalTestServiceState(); body.id = UUID.randomUUID().toString(); MinimalTestService s = new MinimalTestService(); s = (MinimalTestService) this.host.startServiceAndWait(s, "some/" + body.id, body); assertEquals(body.id, s.getSelfId()); assertEquals(body.id, Service.getId(s.getSelfId())); Operation op = Operation.createPatch(s.getUri()).setBody(body); MinimalTestServiceState bodyFromHelper = s.getBody(op); assertEquals(bodyFromHelper.id, body.id); assertTrue(s.checkForBody(op)); op.setBody(null); assertTrue(!s.checkForBody(op)); assertEquals(Operation.STATUS_CODE_BAD_REQUEST, op.getStatusCode()); }
@Test public void testBaseHelperMethods() throws Throwable { MinimalTestServiceState body = new MinimalTestServiceState(); body.id = UUID.randomUUID().toString(); MinimalTestService s = new MinimalTestService(); s = (MinimalTestService) this.host.startServiceAndWait(s, "some/" + body.id, body); assertEquals(body.id, s.getSelfId()); assertEquals(body.id, Service.getId(s.getSelfId())); Operation op = Operation.createPatch(s.getUri()).setBody(body); MinimalTestServiceState bodyFromHelper = s.getBody(op); assertEquals(bodyFromHelper.id, body.id); assertTrue(s.checkForBody(op)); op.setBody(null); assertTrue(!s.checkForBody(op)); assertEquals(Operation.STATUS_CODE_BAD_REQUEST, op.getStatusCode()); }
@Test public void startServiceSelfLinkWithStar() throws Throwable { setUp(false); MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.id = this.host.nextUUID(); TestContext ctx = this.host.testCreate(1); Operation startPost = Operation .createPost(UriUtils.buildUri(this.host, this.host.nextUUID() + "*")) .setBody(initialState).setCompletion(ctx.getExpectedFailureCompletion()); this.host.startService(startPost, new MinimalTestService()); this.host.testWait(ctx); }
@Test public void startServiceSelfLinkWithStar() throws Throwable { setUp(false); MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.id = this.host.nextUUID(); TestContext ctx = this.host.testCreate(1); Operation startPost = Operation .createPost(UriUtils.buildUri(this.host, this.host.nextUUID() + "*")) .setBody(initialState).setCompletion(ctx.getExpectedFailureCompletion()); this.host.startService(startPost, new MinimalTestService()); this.host.testWait(ctx); }