public MapReturnHandler(HasOutputModel hasOutputModel) { MetadataType type = hasOutputModel.getOutput().getType(); Preconditions.checkArgument(isMap(type), "The given output type is not a Map"); mapDataType = (MapDataType) toDataType(type); }
@Test public void operationWithPlainInputStreamOutput() throws Exception { when(outputModel.getType()).thenReturn(typeLoader.load(InputStream.class)); when(componentModel.supportsStreaming()).thenReturn(true); assertStreamIsWrapped(new ByteArrayInputStream(HELLO_WORLD_MSG.getBytes(UTF_8))); }
private OutputModel mockOutputModel(Type type) { OutputModel om = mock(OutputModel.class, withSettings().lenient()); when(om.getType()).thenReturn(loader.load(type)); return om; }
private void assertOperation(String operationName, ExtensionModel extensionModel, DeclarationOperation declarer, String outputDocumentation, String outputAttributeDocumentation) { final Optional<OperationModel> operationModelOptional = extensionModel.getOperationModel(operationName); assertThat(operationModelOptional.isPresent(), is(true)); final OperationModel operationModel = operationModelOptional.get(); assertThat(operationModel.getOutput().getType(), is(declarer.getOutput())); assertThat(operationModel.getOutput().getDescription(), is(outputDocumentation)); assertThat(operationModel.getOutputAttributes().getType(), is(declarer.getOutputAttributes())); assertThat(operationModel.getOutputAttributes().getDescription(), is(outputAttributeDocumentation)); }
@Test public void operationOutputIsNotValidated() { when(operationModel.getAllParameterModels()).thenReturn(emptyList()); when(operationModel.getOutput()).thenReturn(outputModel); when(outputModel.getType()).thenReturn(toMetadataType(PojoWithParameterWithoutGetter.class)); validate(extensionModel, validator); }
@Test public void mediaTypeAnnotationWithDefaultValueAndStaticResolverOnSource() throws Exception { mockExtensionTypeDescriptorModelProperty(TestStringSource.class); mockMediaTypeAnnotation(sourceModel, EMPTY, false); mockMediaTypeAnnotation(operationModel, "*/*", false); when(sourceOutputModel.getType()).thenReturn(sourceOutputType); when(sourceOutputType.getAnnotation(CustomDefinedStaticTypeAnnotation.class)) .thenReturn(empty()); when(sourceOutputType.getMetadataFormat()).thenReturn(new MetadataFormat("Xml", "xml", "application/xml")); validate(extensionModel, validator); }
@Test public void customTypeOutputWithStaticAttributes() throws Exception { OperationModel o = getOperation("customTypeOutputWithStaticAttributes"); assertJsonPerson(o.getOutput()); assertThat(MetadataTypeUtils.getTypeId(o.getOutputAttributes().getType()).get(), is(Banana.class.getName())); }
@Test public void mediaTypeAnnotationAndStaticResolverOnSourceInRuntime() throws Exception { setCompileTime(false); mockExtensionTypeDescriptorModelProperty(TestStringSource.class); mockMediaTypeAnnotation(sourceModel, "*/*", false); when(sourceOutputModel.getType()).thenReturn(sourceOutputType); when(sourceOutputType.getAnnotation(CustomDefinedStaticTypeAnnotation.class)) .thenReturn(of(new CustomDefinedStaticTypeAnnotation())); validate(extensionModel, validator); }
@Test public void mediaTypeAnnotationValueMissingOnSource() throws Exception { expectedException.expect(IllegalModelDefinitionException.class); expectedException.expectMessage("Source 'source' has a String type output but doesn't specify a default mime type."); mockExtensionTypeDescriptorModelProperty(TestStringSource.class); mockMediaTypeAnnotation(sourceModel, EMPTY, false); when(sourceOutputModel.getType()).thenReturn(sourceOutputType); when(sourceOutputType.getAnnotation(CustomDefinedStaticTypeAnnotation.class)) .thenReturn(empty()); when(sourceOutputType.getMetadataFormat()).thenReturn(new MetadataFormat("java", "java", "application/java")); validate(extensionModel, validator); }
@Test public void mediaTypeAnnotationAndStaticResolverOnSource() throws Exception { expectedException.expect(IllegalModelDefinitionException.class); expectedException .expectMessage("Source 'source' is declaring both a custom output Type using a Static MetadataResolver, and a custom media type through the @MediaType annotation."); mockExtensionTypeDescriptorModelProperty(TestStringSource.class); mockMediaTypeAnnotation(sourceModel, "*/*", true); when(sourceOutputModel.getType()).thenReturn(sourceOutputType); when(sourceOutputType.getAnnotation(CustomDefinedStaticTypeAnnotation.class)) .thenReturn(of(new CustomDefinedStaticTypeAnnotation())); validate(extensionModel, validator); }
@Test public void attributesDynamicPersonTypeMetadata() throws Exception { location = Location.builder().globalName(OUTPUT_ATTRIBUTES_WITH_DYNAMIC_METADATA).addProcessorsPart().addIndexPart(0).build(); final ComponentMetadataDescriptor<OperationModel> metadataDescriptor = getSuccessComponentDynamicMetadataWithKey(PERSON_METADATA_KEY); final OperationModel typedModel = metadataDescriptor.getModel(); MetadataType type = typedModel.getOutputAttributes().getType(); assertThat(type, is(instanceOf(ObjectType.class))); ObjectType dictionary = (ObjectType) type; assertThat(dictionary.getOpenRestriction().get(), is(instanceOf(StringType.class))); }
/** * The operation returns its input as the output. * * @param model the model to assert on */ private void assertOutputSameAsInput(final OperationModel model) { assertThat(model.getOutput().getType(), instanceOf(VoidType.class)); assertThat(model.getOutput().hasDynamicType(), is(false)); assertThat(model.getOutputAttributes().getType(), instanceOf(VoidType.class)); assertThat(model.getOutputAttributes().hasDynamicType(), is(false)); }
@Test public void mediaTypeAnnotationAndStaticResolverOnOperation() throws Exception { expectedException.expect(IllegalModelDefinitionException.class); expectedException .expectMessage("Operation 'operation' is declaring both a custom output Type using a Static MetadataResolver, and a custom media type through the @MediaType annotation."); mockExtensionOperationDescriptorModelProperty("returnsString"); mockMediaTypeAnnotation(operationModel, "*/*", true); when(operationOutputModel.getType()).thenReturn(operationOutputType); when(operationOutputType.getAnnotation(CustomDefinedStaticTypeAnnotation.class)) .thenReturn(of(new CustomDefinedStaticTypeAnnotation())); validate(extensionModel, validator); }
@Test public void nonBlockingExecutionType() { ExtensionModel extensionModel = loadExtension(MarvelExtension.class); OperationModel operation = extensionModel.getConfigurationModel(CONFIG_NAME).get().getOperationModel("fireMissile").get(); assertThat(operation.isBlocking(), is(false)); assertThat(operation.getExecutionType(), is(CPU_LITE)); assertThat(operation.getOutput().getType(), instanceOf(StringType.class)); assertThat(operation.getOutputAttributes().getType(), equalTo(typeLoader.load(void.class))); }
@Test public void mediaTypeAnnotationMissingOnOperationWithStreamOutput() throws Exception { expectedException.expect(IllegalModelDefinitionException.class); expectedException .expectMessage("Operation 'operation' has a InputStream type output but doesn't specify a default mime type."); mockExtensionOperationDescriptorModelProperty("returnsStream"); when(operationOutputModel.getType()).thenReturn(toMetadataType(InputStream.class)); validate(extensionModel, validator); }
@Test public void mediaTypeAnnotationMissingOnSourceWithStringOutput() throws Exception { expectedException.expect(IllegalModelDefinitionException.class); expectedException.expectMessage("'source' has a String type output but doesn't specify a default mime type."); mockExtensionTypeDescriptorModelProperty(TestStringSource.class); when(sourceOutputModel.getType()).thenReturn(toMetadataType(String.class)); validate(extensionModel, validator); }
@Test public void mediaTypeAnnotationMissingOnSourceWithStreamOutput() throws Exception { expectedException.expect(IllegalModelDefinitionException.class); expectedException.expectMessage("Source 'source' has a InputStream type output but doesn't specify a default mime type."); mockExtensionTypeDescriptorModelProperty(TestStreamSource.class); when(sourceOutputModel.getType()).thenReturn(toMetadataType(InputStream.class)); validate(extensionModel, validator); }
@Test public void dynamicOutputAndContentWithCache() throws Exception { location = Location.builder().globalName(CONTENT_AND_OUTPUT_CACHE_RESOLVER).addProcessorsPart().addIndexPart(0).build(); final ComponentMetadataDescriptor<OperationModel> metadataDescriptor = getSuccessComponentDynamicMetadataWithKey(PERSON_METADATA_KEY); final OperationModel typedModel = metadataDescriptor.getModel(); MetadataType outputType = typedModel.getOutput().getType(); MetadataType contentType = getParameter(typedModel, "content").getType(); assertThat(contentType, is(equalTo(outputType))); }
@Test public void operationWhichReturnsListOfMessages() throws Exception { location = Location.builder().globalName("listOfMessages").addProcessorsPart().addIndexPart(0).build(); final MetadataResult<ComponentMetadataDescriptor<OperationModel>> result = metadataService.getOperationMetadata(location); assertSuccessResult(result); ComponentMetadataDescriptor<OperationModel> descriptor = result.get(); MetadataType param = descriptor.getModel().getOutput().getType(); assertThat(param, is(instanceOf(ArrayType.class))); assertMessageType(((ArrayType) param).getType(), TYPE_LOADER.load(String.class), TYPE_LOADER.load(StringAttributes.class)); }
@Test public void getNativeQueryOutputArrayMetadata() throws Exception { location = builder().globalName(NATIVE_QUERY_LIST_FLOW).addProcessorsPart().addIndexPart(0).build(); MetadataKey nativeKey = newKey(NATIVE_QUERY).build(); MetadataResult<ComponentMetadataDescriptor<OperationModel>> entityMetadata = getComponentDynamicMetadata(nativeKey); assertThat(entityMetadata.isSuccess(), is(true)); MetadataType generatedType = entityMetadata.get().getModel().getOutput().getType(); assertThat(generatedType, is(instanceOf(ArrayType.class))); assertCircleType((ObjectType) ((ArrayType) generatedType).getType()); }