@Test(dataProvider = "ValidStartStages") public void testValidStartState(TaskState.TaskStage startStage) throws Throwable { CreateContainerSpecTaskService.State startState = buildValidStartState(startStage); Operation startOperation = testHost.startServiceSynchronously(createContainerSpecTaskService, startState); assertThat(startOperation.getStatusCode(), is(200)); }
@Test public void testMinimalStartState() throws Throwable { CreateContainerSpecTaskService.State startState = buildValidStartState(); Operation startOperation = testHost.startServiceSynchronously(createContainerSpecTaskService, startState); assertThat(startOperation.getStatusCode(), is(200)); CreateContainerSpecTaskService.State savedState = testHost.getServiceState(CreateContainerSpecTaskService.State.class); assertThat(savedState.taskState.stage, is(TaskState.TaskStage.STARTED)); }
@Test(dataProvider = "FinalStartStages") public void testFinalStartState(TaskState.TaskStage startStage) throws Throwable { CreateContainerSpecTaskService.State startState = buildValidStartState(startStage); startState.controlFlags = 0; Operation startOperation = testHost.startServiceSynchronously(createContainerSpecTaskService, startState); assertThat(startOperation.getStatusCode(), is(200)); CreateContainerSpecTaskService.State savedState = testHost.getServiceState(CreateContainerSpecTaskService.State.class); assertThat(savedState.taskState.stage, is(startStage)); }
@Test(dataProvider = "StartStagesWhichTransitionToStarted") public void testStartStateTransitionsToStarted(TaskState.TaskStage startStage) throws Throwable { CreateContainerSpecTaskService.State startState = buildValidStartState(startStage); Operation startOperation = testHost.startServiceSynchronously(createContainerSpecTaskService, startState); assertThat(startOperation.getStatusCode(), is(200)); CreateContainerSpecTaskService.State savedState = testHost.getServiceState(CreateContainerSpecTaskService.State.class); assertThat(savedState.taskState.stage, is(TaskState.TaskStage.STARTED)); }
@Test public void testGetServiceStateWithDefaultPath() throws Throwable { ExampleService exampleService = new ExampleService(); ExampleService.ExampleServiceState exampleServiceState = new ExampleService.ExampleServiceState(); exampleServiceState.name = UUID.randomUUID().toString(); Operation post = host.startServiceSynchronously(exampleService, exampleServiceState); assertThat(post.getStatusCode(), is(200)); ExampleService.ExampleServiceState result = host.getServiceState(ExampleService.ExampleServiceState.class); assertThat(result.name, is(exampleServiceState.name)); }
@Test(dataProvider = "ValidStageTransitions") public void testValidStageTransition( TaskState.TaskStage startStage, TaskState.TaskStage patchStage) throws Throwable { startService(startStage); CreateContainerSpecTaskService.State patchState = createContainerSpecTaskService.buildPatch(patchStage, null); Operation patchOperation = Operation.createPatch(UriUtils.buildUri(testHost, TestHost.SERVICE_URI, null)) .setBody(patchState); Operation result = testHost.sendRequestAndWait(patchOperation); assertThat(result.getStatusCode(), is(200)); CreateContainerSpecTaskService.State savedState = testHost.getServiceState(CreateContainerSpecTaskService.State.class); assertThat(savedState.taskState.stage, is(patchStage)); }
@Test public void testWaitForState() throws Throwable { ExampleService exampleService = new ExampleService(); final ExampleService.ExampleServiceState exampleServiceState = new ExampleService.ExampleServiceState(); exampleServiceState.name = UUID.randomUUID().toString(); Operation post = host.startServiceSynchronously(exampleService, exampleServiceState); assertThat(post.getStatusCode(), is(200)); try { host.waitForState( ExampleService.ExampleServiceState.class, new Predicate<ExampleService.ExampleServiceState>() { @Override public boolean test(ExampleService.ExampleServiceState state) { return false; } }); Assert.fail("this state transition should not have succeeded"); } catch (TimeoutException e) { assertThat( e.getMessage(), is(equalToIgnoringCase("timeout waiting for state transition."))); } try { host.waitForState( ExampleService.ExampleServiceState.class, new Predicate<ExampleService.ExampleServiceState>() { @Override public boolean test(ExampleService.ExampleServiceState state) { return state.name.equals(exampleServiceState.name); } }); } catch (RuntimeException runtimeException) { Assert.fail("failed to detect state transition"); } }
private void startService(TaskState.TaskStage startStage) throws Throwable { CreateContainerSpecTaskService.State startState = buildValidStartState(startStage); Operation startOperation = testHost.startServiceSynchronously(createContainerSpecTaskService, startState); assertThat(startOperation.getStatusCode(), is(200)); }
@Test public void testWaitForQuery() throws Throwable { ExampleService exampleService = new ExampleService(); final ExampleService.ExampleServiceState exampleServiceState = new ExampleService.ExampleServiceState(); exampleServiceState.name = UUID.randomUUID().toString(); QueryTask.Query kindClause = new QueryTask.Query() .setTermPropertyName(ServiceDocument.FIELD_NAME_KIND) .setTermMatchValue(Utils.buildKind(ExampleService.ExampleServiceState.class)); QueryTask.Query nameClause = new QueryTask.Query() .setTermPropertyName("name") .setTermMatchValue(exampleServiceState.name); QueryTask.QuerySpecification spec = new QueryTask.QuerySpecification(); spec.query.addBooleanClause(kindClause); spec.query.addBooleanClause(nameClause); spec.options = EnumSet.of(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT); QueryTask query = QueryTask.create(spec).setDirect(true); try { host.waitForQuery( query, new Predicate<QueryTask>() { @Override public boolean test(QueryTask queryTask) { return queryTask.results.documentLinks.size() > 0; } }); Assert.fail("waitForQuery should not have succeeded before documents are created"); } catch (RuntimeException runtimeException) { assertThat( runtimeException.getMessage(), is(equalToIgnoringCase("timeout waiting for query result."))); } Operation post = host.startServiceSynchronously(exampleService, exampleServiceState); assertThat(post.getStatusCode(), is(200)); ExampleService.ExampleServiceState result = host.getServiceState(ExampleService.ExampleServiceState.class); assertThat(result.name, is(exampleServiceState.name)); QueryTask response = host.waitForQuery( query, new Predicate<QueryTask>() { @Override public boolean test(QueryTask queryTask) { return queryTask.results.documentLinks.size() > 0; } }); assertThat(response.results.documentLinks.size(), is(1)); // verify fields are passed down correctly for (Map.Entry<String, Object> document : response.results.documents.entrySet()) { ExampleService.ExampleServiceState docState = Utils.fromJson(document.getValue(), ExampleService.ExampleServiceState.class); assertThat(docState.name, is(equalTo(exampleServiceState.name))); } }