public void DataTable_From_MultipleRecords_Json() { // Arrange String json = GetResourceString(TestFile_MultipleRecords); // Act DataTable dataTable = ManagedApiHelper.ToDataTable( "application/json", json, fixture.ApiDefinition, fixture.Definition); // Assert Assert.Equal(fixture.ExpectedRowCount, dataTable.Rows.Count); }
public ActionResult <Boolean> Get([FromRoute] Guid packageId, [FromRoute] String objectType) { // Do the authentication process with the given request and helpers // to determine the result ApiAuthenticationResult authResult = WebAuthHelper.AuthenticateApiRequest( packageId, objectType, SessionHandler.PackageRepository, Request ); // Everything work ok? Then continue to the important bits if (authResult.StatusCode == HttpStatusCode.OK) { // Use the api definition to get the data connection and // definition from the package and then try to connect IDataProvider provider = providerFactory.Get( authResult.Package, authResult.Package.DataConnection(authResult.ApiDefinition.DataConnection), authResult.Package.DataDefinition(authResult.ApiDefinition.DataDefinition), true); // Are we connected? if (provider.Connected) { // Return the data with the appropriate filter DataTable results = provider.Read(authResult.Permissions.Filter); // Manage any aliases for the results table ManagedApiHelper.HandleAliases(results, authResult.ApiDefinition.Aliases); // Format the data table as Json return(ManagedApiHelper.ToJson(results)); } else { return(StatusCode((Int32)HttpStatusCode.InternalServerError, "Could not connect to the data source")); } } else { return(StatusCode((Int32)authResult.StatusCode, authResult.StatusDescription)); } }
public void Aliases_Injected_To_DataTable() { // Arrange fixture.Initialise(); String aliasName = "Alias Name"; List <KeyValuePair <String, String> > aliases = new List <KeyValuePair <String, String> >() { new KeyValuePair <String, String>("StringData", aliasName) }; // Act ManagedApiHelper.HandleAliases(fixture.Data, aliases); DataColumn dataColumn = fixture.Data.Columns["StringData"]; // Assert Assert.True(dataColumn.ExtendedProperties.Contains("Alias")); Assert.Equal(aliasName, dataColumn.ExtendedProperties["Alias"]); }
public void Json_From_DataTable() { // Arrange fixture.Initialise(); // Act JsonResult jsonResult = ManagedApiHelper.ToJson(fixture.Data); var result = JsonConvert.SerializeObject(jsonResult.Value, jsonResult.SerializerSettings); // Assert Assert.DoesNotContain("\"RowError\":", result); // Standard DataTable Tags Should Be Stripped Out Assert.DoesNotContain("\"RowState\":", result); // Standard DataTable Tags Should Be Stripped Out Assert.DoesNotContain("\"Table\":", result); // Standard DataTable Tags Should Be Stripped Out Assert.DoesNotContain("\"HasErrors\":", result); // Standard DataTable Tags Should Be Stripped Out Assert.Contains($"\"StringData\": \"{fixture.StringToTest}\"", result); // Element Exists Assert.Contains($"\"BooleanData\": true", result); // Element Exists Assert.Contains($"\"DateData\": \"{fixture.DateToTest.ToString("yyyy")}", result); // Element Exists (We only care about the first part of the date here) Assert.Contains($"\"NumericData\": {fixture.NumberToTest.ToString()}", result); // Element Exists }
public ActionResult <Boolean> Post([FromRoute] Guid packageId, [FromRoute] String objectType) { // Do the authentication process with the given request and helpers // to determine the result ApiAuthenticationResult authResult = WebAuthHelper.AuthenticateApiRequest( packageId, objectType, SessionHandler.PackageRepository, Request ); // Everything work ok? Then continue to the important bits if (authResult.StatusCode != HttpStatusCode.OK) { return(StatusCode((Int32)authResult.StatusCode, authResult.StatusDescription)); } // Get the body of the request from the stream String body = HttpRequestHelper.GetBody(Request); if (body == String.Empty) { return(StatusCode((Int32)HttpStatusCode.BadRequest, "Request body contained no data")); } // Translate the body try { DataTable data = null; // Parse the body to a queryable Json Object (bad formatting will fail it) data = ManagedApiHelper.ToDataTable( Request.ContentType.Trim().ToLower(), body, authResult.ApiDefinition, authResult.DataDefinition); // Did we get some data from the conversion (depending on the type format) if (data != null) { // Use the api definition to get the data connection and // definition from the package and then try to connect IDataProvider provider = providerFactory.Get( authResult.Package, authResult.Package.DataConnection(authResult.ApiDefinition.DataConnection), authResult.Package.DataDefinition(authResult.ApiDefinition.DataDefinition), true); // Are we connected? if (provider.Connected) { // Return the data with the appropriate filter // DataTable results = provider.Read(authResult.Permissions.Filter); if (provider.Write(data, "")) { } else { return(StatusCode((Int32)HttpStatusCode.InternalServerError, "Could not write the data")); } } else { return(StatusCode((Int32)HttpStatusCode.InternalServerError, "Could not connect to the data source")); } } else { return(StatusCode((Int32)HttpStatusCode.BadRequest, "Request body contained no valid data or the format was incorrect")); } } catch (Exception ex) { return(StatusCode((Int32)HttpStatusCode.BadRequest, $"Malformed body content in request ({ex.Message})")); } // Got to the end so must be ok return(StatusCode((Int32)HttpStatusCode.OK, "")); }