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()); }
@Test public void documentGetStats() throws Throwable { setUpHost(false); List<String> docLinks = new ArrayList<String>(); MinimalFactoryTestService f = new MinimalFactoryTestService(); MinimalFactoryTestService factoryService = (MinimalFactoryTestService) this.host .startServiceAndWait(f, UUID.randomUUID().toString(), null); factoryService.setChildServiceCaps( EnumSet.of(ServiceOption.PERSISTENCE)); for (int i = 0; i < this.serviceCount; i++) { MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.documentSelfLink = initialState.id = UUID.randomUUID().toString(); this.host.sendAndWaitExpectSuccess( Operation.createPost(factoryService.getUri()).setBody(initialState)); docLinks.add(initialState.documentSelfLink); } for (String selfLink: docLinks) { this.host.sendAndWaitExpectSuccess(Operation.createGet(this.host, UriUtils.buildUriPath(factoryService.getUri().getPath(), selfLink))); } Map<String, ServiceStat> stats = this.host.getServiceStats(this.host.getDocumentIndexServiceUri()); String statKey = String.format( LuceneDocumentIndexService.STAT_NAME_SINGLE_QUERY_BY_FACTORY_COUNT_FORMAT, factoryService.getUri().getPath()); ServiceStat kindStat = stats.get(statKey); assertNotNull(kindStat); assertEquals(this.serviceCount, kindStat.latestValue, 0); }
@Test public void documentGetStats() throws Throwable { setUpHost(false); List<String> docLinks = new ArrayList<String>(); MinimalFactoryTestService f = new MinimalFactoryTestService(); MinimalFactoryTestService factoryService = (MinimalFactoryTestService) this.host .startServiceAndWait(f, UUID.randomUUID().toString(), null); factoryService.setChildServiceCaps( EnumSet.of(ServiceOption.PERSISTENCE)); for (int i = 0; i < this.serviceCount; i++) { MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.documentSelfLink = initialState.id = UUID.randomUUID().toString(); this.host.sendAndWaitExpectSuccess( Operation.createPost(factoryService.getUri()).setBody(initialState)); docLinks.add(initialState.documentSelfLink); } for (String selfLink: docLinks) { this.host.sendAndWaitExpectSuccess(Operation.createGet(this.host, UriUtils.buildUriPath(factoryService.getUri().getPath(), selfLink))); } Map<String, ServiceStat> stats = this.host.getServiceStats(this.host.getDocumentIndexServiceUri()); String statKey = String.format( LuceneDocumentIndexService.STAT_NAME_SINGLE_QUERY_BY_FACTORY_COUNT_FORMAT, factoryService.getUri().getPath()); ServiceStat kindStat = stats.get(statKey); assertNotNull(kindStat); assertEquals(this.serviceCount, kindStat.latestValue, 0); }
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 testQueryDocumentWithFactoryPath() throws Throwable { try { this.queryHelper.setFactoryPath(null); fail("IllegalArgumentException expected with null factoryPath."); } catch (IllegalArgumentException e) { // expected } this.minimalTestStates = queryDocumentWithFactoryPath("testLink"); assertEquals(0, this.minimalTestStates.size()); MinimalTestServiceState minimalTestState = new MinimalTestServiceState(); minimalTestState.id = this.idValue1; minimalTestState = doPost(minimalTestState); this.minimalTestStates = queryDocumentWithFactoryPath(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 = queryDocumentWithFactoryPath(minimalTestState.documentSelfLink); assertEquals(0, this.minimalTestStates.size()); }
@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 testQueryDocumentWithFactoryPath() throws Throwable { try { this.queryHelper.setFactoryPath(null); fail("IllegalArgumentException expected with null factoryPath."); } catch (IllegalArgumentException e) { // expected } this.minimalTestStates = queryDocumentWithFactoryPath("testLink"); assertEquals(0, this.minimalTestStates.size()); MinimalTestServiceState minimalTestState = new MinimalTestServiceState(); minimalTestState.id = this.idValue1; minimalTestState = doPost(minimalTestState); this.minimalTestStates = queryDocumentWithFactoryPath(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 = queryDocumentWithFactoryPath(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()); }
@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); } }
/** * Here we can test that headers sent by the client are sent correctly. The MinimalTestService * can return the headers it receives. Currently we are only testing the Accept header. */ @Test public void validateHeaders() throws Throwable { MinimalTestService service = new MinimalTestService(); MinimalTestServiceState initialState = new MinimalTestServiceState(); initialState.id = ""; initialState.stringValue = ""; this.host.setSystemAuthorizationContext(); this.host.startServiceAndWait(service, UUID.randomUUID().toString(), initialState); this.host.resetAuthorizationContext(); Map<String, String> headers; headers = getHeaders(service.getUri(), false); assertTrue(headers != null); assertTrue(headers.containsKey(HttpHeaderNames.ACCEPT.toString())); assertTrue(headers.get(HttpHeaderNames.ACCEPT.toString()).equals("*/*")); headers = getHeaders(service.getUri(), true); assertTrue(headers != null); assertTrue(headers.containsKey(HttpHeaderNames.ACCEPT.toString())); assertTrue(headers.get(HttpHeaderNames.ACCEPT.toString()) .equals(Operation.MEDIA_TYPE_APPLICATION_JSON)); this.host.log("Headers validated"); }