public void GenerateDynamicPropertyContainerWithNonConflictingName() { // Edmx with declared property named DynamicProperties var edmx = @"<?xml version=""1.0"" standalone=""yes"" ?> <edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx""> <edmx:DataServices> <Schema Namespace=""NS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <EntityType Name=""Vehicle""> <Key> <PropertyRef Name=""Id"" /> </Key> <Property Name=""Id"" Type=""Edm.Int32"" Nullable=""false"" /> <Property Name=""DynamicProperties"" Type=""Edm.String"" /> </EntityType> <EntityType Name=""Car"" BaseType=""NS.Vehicle"" OpenType=""true""> <Property Name=""Model"" Type=""Edm.String"" /> <Property Name=""DynamicProperties2"" Type=""Edm.String"" /> </EntityType> </Schema> <Schema xmlns=""http://docs.oasis-open.org/odata/ns/edm"" Namespace=""NS""> <EntityContainer Name=""Container""/> </Schema> </edmx:DataServices> </edmx:Edmx> "; var languageOptionTargets = new Dictionary <ODataT4CodeGenerator.LanguageOption, string> { { ODataT4CodeGenerator.LanguageOption.CSharp, "[global::Microsoft.OData.Client.ContainerProperty]publicvirtualglobal::System.Collections.Generic.IDictionary<string,object>DynamicProperties3" }, { ODataT4CodeGenerator.LanguageOption.VB, "<Global.Microsoft.OData.Client.ContainerProperty>_PublicOverridablePropertyDynamicProperties3()AsGlobal.System.Collections.Generic.IDictionary(OfString,Object)" } }; foreach (var languageOption in new[] { ODataT4CodeGenerator.LanguageOption.CSharp, ODataT4CodeGenerator.LanguageOption.VB }) { var containerPropertyAttributeSnippet = languageOptionTargets[languageOption]; var t4CodeGenerator = new ODataT4CodeGenerator { Edmx = edmx, GetReferencedModelReaderFunc = null, NamespacePrefix = null, TargetLanguage = languageOption, EnableNamingAlias = false, IgnoreUnexpectedElementsAndAttributes = false, GenerateMultipleFiles = false, ExcludedSchemaTypes = null, EmitContainerPropertyAttribute = true }; var generatedCode = t4CodeGenerator.TransformText(); var normalizedGeneratedCode = GeneratedCodeHelpers.NormalizeGeneratedCode(generatedCode); Assert.IsTrue(normalizedGeneratedCode.IndexOf(containerPropertyAttributeSnippet, StringComparison.Ordinal) > 0); } }
public void TestV3AddGeneratedClientCode_GeneratesCodeForv3_ForVB() { var serviceName = "MyService"; var referenceFolderPath = Path.Combine(TestProjectRootPath, ServicesRootFolder, serviceName); Directory.CreateDirectory(referenceFolderPath); Project project = CreateTestProject(TestProjectRootPath, ODataT4CodeGenerator.LanguageOption.VB); var serviceConfig = new ServiceConfiguration() { Endpoint = Path.Combine(Directory.GetCurrentDirectory(), "CodeGeneration", "SampleServiceV3.xml"), GeneratedFileNamePrefix = "Reference", EdmxVersion = Common.Constants.EdmxVersion3 }; var serviceInstance = new ODataConnectedServiceInstance() { ServiceConfig = serviceConfig, Name = serviceName }; var handlerHelper = new TestConnectedServiceHandlerHelper(); handlerHelper.ServicesRootFolder = ServicesRootFolder; ConnectedServiceHandlerContext context = new TestConnectedServiceHandlerContext(serviceInstance, handlerHelper); var descriptor = new TestV3CodeGenDescriptor(serviceConfig.Endpoint, context, project); descriptor.AddGeneratedClientCodeAsync().Wait(); var addedFile = handlerHelper.AddedFiles.FirstOrDefault(); var generatedCode = File.ReadAllText(addedFile.SourceFile); var expectedCode = GeneratedCodeHelpers.LoadReferenceContent("SampleServiceV3.vb"); Assert.IsNotNull(addedFile); Assert.AreEqual(Path.Combine(TestProjectRootPath, ServicesRootFolder, serviceName, "Reference.vb"), addedFile.CreatedFile); GeneratedCodeHelpers.VerifyGeneratedCode(expectedCode, generatedCode); }
public void GenerateDynamicPropertyContainer() { var edmx = @"<?xml version=""1.0"" standalone=""yes"" ?> <edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx""> <edmx:DataServices> <Schema Namespace=""NS"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <ComplexType Name=""Address"" OpenType=""True""> <Property Name=""Street"" Type=""Edm.String"" Nullable=""false"" /> <Property Name=""City"" Type=""Edm.String"" Nullable=""false"" /> </ComplexType> </Schema> <Schema xmlns=""http://docs.oasis-open.org/odata/ns/edm"" Namespace=""NS""> <EntityContainer Name=""Container""/> </Schema> </edmx:DataServices> </edmx:Edmx> "; var languageOptionTargets = new Dictionary <ODataT4CodeGenerator.LanguageOption, Tuple <string, string> > { { ODataT4CodeGenerator.LanguageOption.CSharp, new Tuple <string, string>("DynamicPropertyContainerTest.cs", "[global::Microsoft.OData.Client.ContainerProperty]publicvirtualglobal::System.Collections.Generic.IDictionary<string,object>DynamicProperties") }, { ODataT4CodeGenerator.LanguageOption.VB, new Tuple <string, string>("DynamicPropertyContainerTest.vb", "<Global.Microsoft.OData.Client.ContainerProperty>_PublicOverridablePropertyDynamicProperties()AsGlobal.System.Collections.Generic.IDictionary(OfString,Object)") } }; foreach (var languageOption in new[] { ODataT4CodeGenerator.LanguageOption.CSharp, ODataT4CodeGenerator.LanguageOption.VB }) { var expectedCodeFileName = languageOptionTargets[languageOption].Item1; var containerPropertyAttributeSnippet = languageOptionTargets[languageOption].Item2; var t4CodeGenerator = new ODataT4CodeGenerator { Edmx = edmx, GetReferencedModelReaderFunc = null, NamespacePrefix = null, TargetLanguage = languageOption, EnableNamingAlias = false, IgnoreUnexpectedElementsAndAttributes = false, GenerateMultipleFiles = false, ExcludedSchemaTypes = null, EmitContainerPropertyAttribute = true }; var generatedCode = t4CodeGenerator.TransformText(); var expectedCode = ODataT4CodeGeneratorTestDescriptors.GetFileContent(expectedCodeFileName); var normalizedGeneratedCode = GeneratedCodeHelpers.NormalizeGeneratedCode(generatedCode); var normalizedExpectedCode = GeneratedCodeHelpers.NormalizeGeneratedCode(expectedCode); Assert.AreEqual(normalizedGeneratedCode, normalizedExpectedCode); Assert.IsTrue(normalizedGeneratedCode.IndexOf(containerPropertyAttributeSnippet, StringComparison.Ordinal) > 0); } }
public void TestTypeDefinitionsParamsConvertedToUnderlyingType() { string edmx = GeneratedCodeHelpers.LoadReferenceContent("TypeDefinitions.xml"); string expected = GeneratedCodeHelpers.LoadReferenceContent("TypeDefinitionsParamsConvertedToUnderlyingType.cs"); var generator = new ODataT4CodeGenerator() { Edmx = edmx, TargetLanguage = ODataT4CodeGenerator.LanguageOption.CSharp }; var output = generator.TransformText(); GeneratedCodeHelpers.VerifyGeneratedCode(expected, output); }
public void TestEntitiesComplexTypesEnumsFunctions() { string edmx = GeneratedCodeHelpers.LoadReferenceContent("EntitiesEnumsFunctions.xml"); string expected = GeneratedCodeHelpers.LoadReferenceContent("EntitiesEnumsFunctions.cs"); var generator = new ODataT4CodeGenerator() { Edmx = edmx, TargetLanguage = ODataT4CodeGenerator.LanguageOption.CSharp }; var output = generator.TransformText(); GeneratedCodeHelpers.VerifyGeneratedCode(expected, output); }
public void OnPageLeavingConfigODataEndpointPageTest() { string edmx = GeneratedCodeHelpers.LoadReferenceContent("Simple.xml"); string expectedTempfileContent = GeneratedCodeHelpers.LoadReferenceContent("TempSimple.xml"); Task <PageNavigationResult> pageNavigationResultTask; PageNavigationResult pageNavigationResult; File.WriteAllText("EdmxFile.xml", edmx); //Check if an error is thrown if the on leaving the page without providing the endpoint pageNavigationResultTask = configOdataEndPointViewModel.OnPageLeavingAsync(null); pageNavigationResult = pageNavigationResultTask?.Result; Assert.IsNotNull(pageNavigationResult.ErrorMessage); Assert.IsTrue(pageNavigationResult.ErrorMessage.Contains(Constants.InputServiceEndpointMsg), "User is not prompted to enter endpoint"); Assert.IsFalse(pageNavigationResult.IsSuccess); Assert.IsTrue(pageNavigationResult.ShowMessageBoxOnFailure); //Provide a url without $metadata configOdataEndPointViewModel.UserSettings.Endpoint = "http://mysite/ODataService"; pageNavigationResultTask = configOdataEndPointViewModel.OnPageLeavingAsync(null); //Check if $metadata is apended if the url does not have it added at the end Assert.AreEqual(configOdataEndPointViewModel.UserSettings.Endpoint, "http://mysite/ODataService/$metadata"); //Check if an exception is thrown for an invalid url and the user is notified pageNavigationResult = pageNavigationResultTask?.Result; Assert.IsNotNull(pageNavigationResult.ErrorMessage); Assert.IsTrue(pageNavigationResult.ErrorMessage.Contains("The remote name could not be resolved") || pageNavigationResult.ErrorMessage.Contains("The remote server returned an error: (407) Proxy Authentication Required")); Assert.IsFalse(pageNavigationResult.IsSuccess); Assert.IsTrue(pageNavigationResult.ShowMessageBoxOnFailure); configOdataEndPointViewModel.UserSettings.Endpoint = Path.Combine(Directory.GetCurrentDirectory(), "EdmxFile.xml"); pageNavigationResultTask = configOdataEndPointViewModel.OnPageLeavingAsync(null); //Check if any errors were reported pageNavigationResult = pageNavigationResultTask?.Result; Assert.IsNull(pageNavigationResult.ErrorMessage); Assert.IsTrue(pageNavigationResult.IsSuccess); Assert.IsFalse(pageNavigationResult.ShowMessageBoxOnFailure); //Check if the content writtent to the temp file is correct string actualTempFileContent = File.ReadAllText(configOdataEndPointViewModel.MetadataTempPath); Assert.AreEqual(expectedTempfileContent.Trim(), actualTempFileContent.Trim(), "temp metadata file not properly written"); //Check if Edmx verison of has correctly been detected Assert.AreEqual(configOdataEndPointViewModel.EdmxVersion.ToString(), "4.0.0.0", "Version not properly detected"); }
public void TestExcludedOperationImportsNotIncludeInGeneratedCode() { string edmx = GeneratedCodeHelpers.LoadReferenceContent("EntitiesEnumsFunctions.xml"); string expected = GeneratedCodeHelpers.LoadReferenceContent("EntitiesEnumsFunctionsDSCExcludeOperationImports.cs"); var generator = new ODataT4CodeGenerator() { Edmx = edmx, UseDataServiceCollection = true, ExcludedOperationImports = new string[] { "GetPersonWithMostFriends", "ResetDataSource" }, TargetLanguage = ODataT4CodeGenerator.LanguageOption.CSharp }; var output = generator.TransformText(); GeneratedCodeHelpers.VerifyGeneratedCode(expected, output); }
public void TestEntitiesComplexTypesEnumFunctionsDSCWithInternalTypes() { string edmx = GeneratedCodeHelpers.LoadReferenceContent("EntitiesEnumsFunctions.xml"); string expected = GeneratedCodeHelpers.LoadReferenceContent("EntitiesEnumsFunctionsDSCWithInternalTypes.cs"); var generator = new ODataT4CodeGenerator() { Edmx = edmx, TargetLanguage = ODataT4CodeGenerator.LanguageOption.CSharp, UseDataServiceCollection = true, MakeTypesInternal = true }; var output = generator.TransformText(); GeneratedCodeHelpers.VerifyGeneratedCode(expected, output); }
public void CodeGenSimpleEdmxMultipleFiles() { string code = CodeGenWithT4Template(ODataT4CodeGeneratorTestDescriptors.SimpleMultipleFiles.Metadata, null, true, false, generateMultipleFiles: true); string expectedTestType = GeneratedCodeHelpers.NormalizeGeneratedCode(ODataT4CodeGeneratorTestDescriptors.GetFileContent("SimpleMultipleTestType.cs")); string actualTestType = GeneratedCodeHelpers.NormalizeGeneratedCode(File.ReadAllText(Path.Combine(Path.GetTempPath(), "TestType.cs"))); string expectedPersonGender = GeneratedCodeHelpers.NormalizeGeneratedCode(ODataT4CodeGeneratorTestDescriptors.GetFileContent("SimpleMultipleFilesPersonGender.cs")); string actualPersonGender = GeneratedCodeHelpers.NormalizeGeneratedCode(File.ReadAllText(Path.Combine(Path.GetTempPath(), "PersonGender.cs"))); string expectedCity = GeneratedCodeHelpers.NormalizeGeneratedCode(ODataT4CodeGeneratorTestDescriptors.GetFileContent("SimpleMultipleFilesCity.cs")); string actualCity = GeneratedCodeHelpers.NormalizeGeneratedCode(File.ReadAllText(Path.Combine(Path.GetTempPath(), "City.cs"))); string expectedExtensionMethods = GeneratedCodeHelpers.NormalizeGeneratedCode(ODataT4CodeGeneratorTestDescriptors.GetFileContent("SimpleMultipleFilesMain.cs")); string actualExtenisonMethods = GeneratedCodeHelpers.NormalizeGeneratedCode(File.ReadAllText(Path.Combine(Path.GetTempPath(), "ExtensionMethods.cs"))); Assert.AreEqual(expectedTestType, actualTestType); Assert.AreEqual(expectedPersonGender, actualPersonGender); Assert.AreEqual(expectedCity, actualCity); Assert.AreEqual(expectedExtensionMethods, actualExtenisonMethods); }