private void writeEntitySets(final JsonGenerator gen, final EdmEntityContainer container)
     throws IOException {
   for (EdmEntitySet edmEntitySet : container.getEntitySets()) {
     if (edmEntitySet.isIncludeInServiceDocument()) {
       writeElement(gen, null, edmEntitySet.getName(), edmEntitySet.getName());
     }
   }
 }
  private void appendEntityContainer(
      final XMLStreamWriter writer, final EdmEntityContainer container) throws XMLStreamException {
    if (container != null) {
      writer.writeStartElement(XML_ENTITY_CONTAINER);

      writer.writeAttribute(XML_NAME, container.getName());
      FullQualifiedName parentContainerName = container.getParentContainerName();
      if (parentContainerName != null) {
        String parentContainerNameString;
        if (namespaceToAlias.get(parentContainerName.getNamespace()) != null) {
          parentContainerNameString =
              namespaceToAlias.get(parentContainerName.getNamespace())
                  + "."
                  + parentContainerName.getName();
        } else {
          parentContainerNameString = parentContainerName.getFullQualifiedNameAsString();
        }
        writer.writeAttribute(XML_EXTENDS, parentContainerNameString);
      }

      // EntitySets
      appendEntitySets(writer, container.getEntitySets());

      // ActionImports
      appendActionImports(writer, container.getActionImports());

      // FunctionImports
      String containerNamespace;
      if (namespaceToAlias.get(container.getNamespace()) != null) {
        containerNamespace = namespaceToAlias.get(container.getNamespace());
      } else {
        containerNamespace = container.getNamespace();
      }
      appendFunctionImports(writer, container.getFunctionImports(), containerNamespace);

      // Singletons
      appendSingletons(writer, container.getSingletons());

      writer.writeEndElement();
    }
  }
  @Test
  public void writeServiceWithEmptyMockedEdm() throws Exception {
    final Edm edm = mock(Edm.class);
    EdmEntityContainer container = mock(EdmEntityContainer.class);
    when(container.getFullQualifiedName()).thenReturn(new FullQualifiedName("service", "test"));
    when(container.getEntitySets()).thenReturn(Collections.<EdmEntitySet>emptyList());
    when(container.getFunctionImports()).thenReturn(Collections.<EdmFunctionImport>emptyList());
    when(container.getSingletons()).thenReturn(Collections.<EdmSingleton>emptyList());
    when(edm.getEntityContainer()).thenReturn(container);
    ServiceMetadata metadata = mock(ServiceMetadata.class);
    when(metadata.getEdm()).thenReturn(edm);

    assertEquals(
        "<?xml version='1.0' encoding='UTF-8'?>"
            + "<app:service xmlns:atom=\"http://www.w3.org/2005/Atom\" "
            + "xmlns:app=\"http://www.w3.org/2007/app\" "
            + "xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\" "
            + "metadata:context=\"http://host/svc/$metadata\">"
            + "<app:workspace><atom:title>service.test</atom:title></app:workspace>"
            + "</app:service>",
        IOUtils.toString(serializer.serviceDocument(metadata, "http://host/svc").getContent()));
  }