Exemplo n.º 1
0
        public void TestCoreDescriptionAnnotation()
        {
            EdmModel model = new EdmModel();

            const string personTypeDescription = "this is person type.";
            const string personTypeLongDescription = "this is person type, but with a longer description.";
            const string overwritePersonDescription = "this is overwritten person type.";
            const string propertyIdDescription = "this is property Id.";
            const string structuralPropertyDescription = "this is structural property.";
            const string stringTermDescription = "this is string term.";
            const string entitySetDescription = "this is entitySet.";
            const string singletonDescription = "this is singleton.";

            EdmEntityContainer container = new EdmEntityContainer("Ns", "Container");
            var personType = new EdmEntityType("Ns", "Person");
            var propertyId = personType.AddStructuralProperty("Id", EdmCoreModel.Instance.GetInt32(false));
            personType.AddKeys(propertyId);
            var structuralProperty = personType.AddStructuralProperty("Concurrency", EdmCoreModel.Instance.GetInt32(true));
            model.AddElement(personType);
            var entitySet = container.AddEntitySet("People", personType);
            var driverSet = container.AddEntitySet("Drivers", personType);

            var stringTerm = new EdmTerm("Ns", "HomeAddress", EdmCoreModel.Instance.GetString(true));
            model.AddElement(stringTerm);

            var singleton = container.AddSingleton("Boss", personType);
            model.AddElement(container);

            model.SetDescriptionAnnotation(personType, personTypeDescription);
            model.SetDescriptionAnnotation(personType, overwritePersonDescription);
            model.SetLongDescriptionAnnotation(personType, personTypeLongDescription);
            model.SetDescriptionAnnotation(structuralProperty, structuralPropertyDescription);
            model.SetDescriptionAnnotation(propertyId, propertyIdDescription);
            model.SetDescriptionAnnotation(stringTerm, stringTermDescription);
            model.SetDescriptionAnnotation(entitySet, entitySetDescription);
            model.SetDescriptionAnnotation(singleton, singletonDescription);

            Assert.AreEqual(overwritePersonDescription, model.GetDescriptionAnnotation(personType));
            Assert.AreEqual(personTypeLongDescription, model.GetLongDescriptionAnnotation(personType));
            Assert.AreEqual(structuralPropertyDescription, model.GetDescriptionAnnotation(structuralProperty));
            Assert.AreEqual(propertyIdDescription, model.GetDescriptionAnnotation(propertyId));
            Assert.AreEqual(stringTermDescription, model.GetDescriptionAnnotation(stringTerm));
            Assert.AreEqual(entitySetDescription, model.GetDescriptionAnnotation(entitySet));
            Assert.AreEqual(singletonDescription, model.GetDescriptionAnnotation(singleton));
            Assert.IsNull(model.GetDescriptionAnnotation(driverSet));

            const string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""Ns"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""Person"">
    <Key>
      <PropertyRef Name=""Id"" />
    </Key>
    <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"">
      <Annotation Term=""Org.OData.Core.V1.Description"" String=""this is property Id."" />
    </Property>
    <Property Name=""Concurrency"" Type=""Edm.Int32"">
      <Annotation Term=""Org.OData.Core.V1.Description"" String=""this is structural property."" />
    </Property>
    <Annotation Term=""Org.OData.Core.V1.Description"" String=""this is overwritten person type."" />
    <Annotation Term=""Org.OData.Core.V1.LongDescription"" String=""this is person type, but with a longer description."" />
  </EntityType>
  <Term Name=""HomeAddress"" Type=""Edm.String"">
    <Annotation Term=""Org.OData.Core.V1.Description"" String=""this is string term."" />
  </Term>
  <EntityContainer Name=""Container"">
    <EntitySet Name=""People"" EntityType=""Ns.Person"">
      <Annotation Term=""Org.OData.Core.V1.Description"" String=""this is entitySet."" />
    </EntitySet>
    <EntitySet Name=""Drivers"" EntityType=""Ns.Person"" />
    <Singleton Name=""Boss"" Type=""Ns.Person"">
      <Annotation Term=""Org.OData.Core.V1.Description"" String=""this is singleton."" />
    </Singleton>
  </EntityContainer>
</Schema>";

            IEnumerable<EdmError> errors;
            StringWriter sw = new StringWriter();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.Encoding = System.Text.Encoding.UTF8;
            XmlWriter xw = XmlWriter.Create(sw, settings);
            model.TryWriteCsdl(xw, out errors);
            xw.Flush();
            xw.Close();
            var actual = sw.ToString();

            Assert.AreEqual(expected, actual);

            IEdmModel parsedModel;
            IEnumerable<EdmError> errs;
            bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(expected)) }, out parsedModel, out errs);
            Assert.IsTrue(parsed, "parsed");
            Assert.IsTrue(!errors.Any(), "No errors");

            Assert.AreEqual(null, parsedModel.GetDescriptionAnnotation(personType));
            Assert.AreEqual(null, parsedModel.GetDescriptionAnnotation(structuralProperty));
            Assert.AreEqual(null, parsedModel.GetDescriptionAnnotation(propertyId));
            Assert.AreEqual(null, parsedModel.GetDescriptionAnnotation(stringTerm));
            Assert.AreEqual(null, parsedModel.GetDescriptionAnnotation(entitySet));
            Assert.AreEqual(null, parsedModel.GetDescriptionAnnotation(singleton));

            var parsedPersonType = parsedModel.FindType("Ns.Person") as IEdmEntityType;
            Assert.IsNotNull(parsedPersonType);
            Assert.AreEqual(overwritePersonDescription, parsedModel.GetDescriptionAnnotation(parsedPersonType));
            Assert.AreEqual(personTypeLongDescription, parsedModel.GetLongDescriptionAnnotation(parsedPersonType));
            Assert.AreEqual(structuralPropertyDescription, parsedModel.GetDescriptionAnnotation((parsedPersonType.FindProperty("Concurrency"))));
            Assert.AreEqual(propertyIdDescription, parsedModel.GetDescriptionAnnotation(parsedPersonType.FindProperty("Id")));
            Assert.AreEqual(stringTermDescription, parsedModel.GetDescriptionAnnotation(parsedModel.FindValueTerm("Ns.HomeAddress")));
            Assert.AreEqual(entitySetDescription, parsedModel.GetDescriptionAnnotation(parsedModel.FindDeclaredEntitySet("People")));
            Assert.AreEqual(singletonDescription, parsedModel.GetDescriptionAnnotation(parsedModel.FindDeclaredSingleton("Boss")));
            Assert.IsNull(parsedModel.GetDescriptionAnnotation(parsedModel.FindDeclaredEntitySet("Drivers")));
        }