@Test
public void writeMetadataWithTypeDefinitions() throws Exception {

  EdmSchema schema = mock(EdmSchema.class);
  when(schema.getNamespace()).thenReturn("MyNamespace");
  Edm edm = mock(Edm.class);
  when(edm.getSchemas()).thenReturn(Arrays.asList(schema));
  EdmTypeDefinition typeDefinition = mock(EdmTypeDefinition.class);
  when (schema.getTypeDefinitions()).thenReturn(Arrays.asList(typeDefinition));
  when(typeDefinition.getMaxLength()).thenReturn(10);
  when(typeDefinition.getScale()).thenReturn(2);
  when(typeDefinition.getPrecision()).thenReturn(10);
  when(typeDefinition.getSrid()).thenReturn(SRID.valueOf("123"));
  when(typeDefinition.getName()).thenReturn("MyTypeDefinition");
  when(typeDefinition.getKind()).thenReturn(EdmTypeKind.DEFINITION);
  EdmPrimitiveType int32Type = OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Int32);
  when(typeDefinition.getUnderlyingType()).thenReturn(int32Type);
  
  EdmAnnotation annotation = mock(EdmAnnotation.class);
  when(typeDefinition.getAnnotations()).thenReturn(Collections.singletonList(annotation));
  EdmTerm term = mock(EdmTerm.class);
  when(term.getName()).thenReturn("Unit");
  when(term.getFullQualifiedName()).thenReturn(new FullQualifiedName("Measures", "Unit"));
  when(annotation.getTerm()).thenReturn(term);
  EdmConstantExpression expression = mock(EdmConstantExpression.class);
  when(expression.isConstant()).thenReturn(true);
  when(expression.asConstant()).thenReturn(expression);
  when(expression.getExpressionType()).thenReturn(EdmExpressionType.String);
  when(expression.getExpressionName()).thenReturn("String");
  when(expression.getValueAsString()).thenReturn("Centimeters");
  when(annotation.getExpression()).thenReturn(expression);
  
  ServiceMetadata serviceMetadata = mock(ServiceMetadata.class);
  when(serviceMetadata.getEdm()).thenReturn(edm);
  
  InputStream metadata = serializer.metadataDocument(serviceMetadata).getContent();
  assertNotNull(metadata);
  String metadataStr = IOUtils.toString(metadata);
  assertEquals("{\"$Version\":\"4.01\","
      + "\"MyNamespace\":"
      + "{\"MyTypeDefinition\":{"
      + "\"$Kind\":\"DEFINITION\","
      + "\"$UnderlyingType\":\"Edm.Int32\","
      + "\"$MaxLength\":\"10\",\"$Precision\":\"10\","
      + "\"$Scale\":\"2\",\"$SRID\":\"123\","
      + "\"@Measures.Unit\":\"Centimeters\"}}}",
      metadataStr);
}