예제 #1
0
        /// <summary>
        /// Validates connection
        /// </summary>
        /// <returns>A Validation object which specifies if connection is valid</returns>
        internal static ResponseValidation ValidateConnection()
        {
            var result  = new ResponseValidation();
            var isError = false;

            if (IsAutoCredMode)
            {
                isError = String.IsNullOrEmpty(UUID);
                if (isError)
                {
                    result.Message = Messages.UUIDRequired;
                }
            }
            else
            {
                isError = String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password);
                if (!isError)
                {
                    AtomManagerInstance.Credentials = new Credentials(Username, Password);
                }
                else
                {
                    result.Message = Messages.CredentialsRequired;
                }
            }
            result.IsValid = !isError;
            return(result);
        }
예제 #2
0
        public async Task <Table> AddRows <TTableRows>(string groupId, string datasetId, IEnumerable <TTableRows> rows)
        {
            var length     = rows.Count();
            var numOfSkips = (int)Math.Ceiling((double)length / Buffer);

            IRestResponse <Table> response = null;
            var count = 0;

            while (count <= numOfSkips)
            {
                var bufferTableEntries = new List <TTableRows>();
                var numremaining       = length - count * Buffer;
                bufferTableEntries.AddRange(rows.Skip(count * Buffer).Take(numremaining > Buffer ? Buffer : numremaining));
                var request = new RestRequest(_addresses.AddOrRemoveRows(groupId), Method.POST)
                {
                    JsonSerializer = new Serialization.JsonSerializer()
                };
                request.RequestFormat = DataFormat.Json;
                request.AddBody(bufferTableEntries);
                request.AddUrlSegment("datasetId", datasetId);
                request.AddUrlSegment("tableName", typeof(TTableRows).Name);

                response = await _client.ExecuteTaskAsync <Table>(request);

                ResponseValidation.HandleResponseErrors(response);
                count++;
            }

            if (response == null)
            {
                throw new BISharpRequestException("Error recieving response");
            }

            return(response.Data);
        }
        public async Task AssertThatResponseIsValidThrowsNoErrorWhenTheResponseConformsToTheSpecification()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            await ResponseValidation.AssertThatResponseIsValid(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK, null, new object[] {});
        }
예제 #4
0
        public async Task <Dashboards> List(string groupId)
        {
            var request = new RestRequest(_addresses.GetDashboards(groupId), Method.GET);

            var response = await _client.ExecuteTaskAsync <Dashboards>(request, _cancellationToken.Token);

            ResponseValidation.HandleResponseErrors(response);
            return(response.Data);
        }
예제 #5
0
        public async Task <Groups> Get()
        {
            var request = new RestRequest($"v1.0/myorg/groups", Method.GET);

            var response = await _client.ExecuteTaskAsync <Groups>(request, _cancellationToken.Token);

            ResponseValidation.HandleResponseErrors(response);
            return(response.Data);
        }
예제 #6
0
        public async Task <Tiles> Tiles(string groupId, string dashboardId)
        {
            var request = new RestRequest(_addresses.GetDashboardTiles(groupId), Method.GET);

            request.AddUrlSegment("dashboardId", dashboardId);

            var response = await _client.ExecuteTaskAsync <Tiles>(request, _cancellationToken.Token);

            ResponseValidation.HandleResponseErrors(response);
            return(response.Data);
        }
예제 #7
0
 public JsonResult DeleteAnEmployee(int employeeId)
 {
     try
     {
         return(new JsonResult(_employeeService.DeleteAnEmployee(employeeId)));
     }
     catch (Exception ex)
     {
         return(new JsonResult(ResponseValidation.GetHTTPStatusCode(ex)));
     }
 }
예제 #8
0
 public JsonResult GetAllEmployee()
 {
     try
     {
         return(new JsonResult(_employeeService.GetAllEmployees()));
     }
     catch (Exception ex)
     {
         return(new JsonResult(ResponseValidation.GetHTTPStatusCode(ex)));
     }
 }
예제 #9
0
        public async Task <Table> ClearRows(string groupId, string datasetId, string tableName)
        {
            var request = new RestRequest(_addresses.AddOrRemoveRows(groupId), Method.DELETE);

            request.AddUrlSegment("datasetId", datasetId);
            request.AddUrlSegment("tableName", tableName);

            var response = await _client.ExecuteTaskAsync <Table>(request);

            ResponseValidation.HandleResponseErrors(response);
            return(response.Data);
        }
        public async Task ValidateResponseValidShouldBeTrueWhenResponseConformsToOpenApiSpecWithEmptyArray()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>(), new List <object>());

            Assert.That(validationResult.Valid, Is.True);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(0));
        }
        public async Task ValidateResponseValidShouldBeFalseWhenResponseDoesNotConformToOpenApiSpec_3_NoJson()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>());

            Assert.That(validationResult.Valid, Is.False);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(1));
            Assert.That(validationResult.Errors[0], Is.EqualTo("{\"path\":\".response\",\"message\":\"response body required.\"}"));
        }
        public async Task ValidateResponseThrowsAnExceptionWhenNotInitialised()
        {
            try
            {
                await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                          new Dictionary <string, string>(), new List <object>());

                Assert.Fail("Fail");
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("You have not initialised the ResponseValidation object"));
            }
        }
        public async Task AssertThatResponseIsValidThrowsNoErrorWhenTheResponseConformsToTheSpecificationUsingRequestAndResponseOverloads()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var request  = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost/v1/pets"));
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("[]")
            };

            await ResponseValidation.AssertThatResponseIsValid(request, response);
        }
        public async Task ValidateResponseValidShouldBeFalseWhenResponseDoesNotConformToOpenApiSpec_2()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var json = new { dayOfWeek = "Monday" };

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>(), json);

            Assert.That(validationResult.Valid, Is.False);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(1));
            Assert.That(validationResult.Errors[0], Is.EqualTo("{\"path\":\".response\",\"message\":\"should be array\",\"errorCode\":\"type.openapi.validation\"}"));
        }
        public async Task ValidateResponseValidShouldBeTrueWhenResponseConformsToOpenApiSpecWithPopulatedArrayFromJsonString()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var jsonString = "[{\"id\":1,\"name\":\"bob\",\"type\":\"dog\"}]";
            var json       = JsonConvert.DeserializeObject(jsonString);

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>(), json);

            Assert.That(validationResult.Valid, Is.True);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(0));
        }
        public async Task InitialiseShouldThrowAnErrorIfTheResponseValidationServiceIsNotAvailable()
        {
            await Setup("./docker-compose-invalid-api.yml", false);

            try
            {
                await ResponseValidation.Initialise("http://localhost:3010/");

                Assert.Fail("Fail");
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("An error occurred while trying to initialise. An error occurred while sending the request."));
            }
        }
        public async Task InitialiseFailsIfTheResponseValidationUriIsInvalid(string responseValidationUri)
        {
            await Setup();

            try
            {
                await ResponseValidation.Initialise(responseValidationUri);

                Assert.Fail("Fail");
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("You must define the responseValidationUri"));
            }
        }
        public async Task AssertThatResponseIsValidThrowsAnErrorWhenTheResponseDoesNotConformToTheSpecification()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            try
            {
                await ResponseValidation.AssertThatResponseIsValid(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK, null, "");

                throw new Exception("Fail");
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("Response validation failed with the following errors: {\"path\":\".response\",\"message\":\"should be array\",\"errorCode\":\"type.openapi.validation\"}."));
            }
        }
        public async Task ValidateResponseValidShouldBeTrueWhenResponseConformsToOpenApiSpecWhenUsingRequestAndResponseOverload()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var request  = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost/v1/pets"));
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("[]")
            };

            var validationResult = await ResponseValidation.ValidateResponse(request, response);

            Assert.That(validationResult.Valid, Is.True);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(0));
        }
        public async Task ValidateResponseThrowsAnExceptionWhenPathIsNotDefined()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            try
            {
                await ResponseValidation.ValidateResponse(HttpMethod.Get, "", HttpStatusCode.OK,
                                                          new Dictionary <string, string>(), new List <object>());

                Assert.Fail("Fail");
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("You must define a path"));
            }
        }
예제 #21
0
        public JsonResult CreateNewtEmployee([FromBody] EmployeeToAdd employeeToAdd)
        {
            try
            {
                var newEmployee = new Employee()
                {
                    EmployeeAddress = employeeToAdd.Address,
                    EmployeeAge     = employeeToAdd.Age,
                    EmployeeName    = employeeToAdd.Name,
                    EmployeeSalary  = employeeToAdd.Salary
                };

                return(new JsonResult(_employeeService.CreateNewEmployee(newEmployee)));
            }
            catch (Exception ex)
            {
                return(new JsonResult(ResponseValidation.GetHTTPStatusCode(ex)));
            }
        }
예제 #22
0
        public JsonResult UpdateAnExistingEmployee(int employeeId, [FromBody] EmployeeToUpdate employeeToUpdate)
        {
            try
            {
                var modifiedEmployee = new Employee
                {
                    EmployeeAddress = employeeToUpdate.Address,
                    EmployeeAge     = employeeToUpdate.Age,
                    EmployeeName    = employeeToUpdate.Name,
                    EmployeeSalary  = employeeToUpdate.Salary,
                    EmployeeStatus  = employeeToUpdate.Status
                };

                return(new JsonResult(_employeeService.UpdateAnExistingEmployee(employeeId, modifiedEmployee)));
            }
            catch (Exception ex)
            {
                return(new JsonResult(ResponseValidation.GetHTTPStatusCode(ex)));
            }
        }
예제 #23
0
        public async Task <Table> UpdateTableSchema(string groupId, string datasetId, string tableName, Type newTableStructure)
        {
            var table = Table.FromType(newTableStructure);

            table.name = tableName;
            var request = new RestRequest(_addresses.UpdateTableSchema(groupId), Method.PUT)
            {
                JsonSerializer = new Serialization.JsonSerializer()
            };

            request.RequestFormat = DataFormat.Json;
            request.AddBody(table);
            request.AddUrlSegment("datasetId", datasetId);
            request.AddUrlSegment("tableName", tableName);

            var response = await _client.ExecuteTaskAsync <Table>(request);

            ResponseValidation.HandleResponseErrors(response);
            return(response.Data);
        }
        public async Task ValidateResponseValidShouldBeFalseWhenResponseDoesNotConformToOpenApiSpec_1()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            var json = new List <object>
            {
                new { dayOfWeek = "Monday" }
            };

            var validationResult = await ResponseValidation.ValidateResponse(HttpMethod.Get, "/v1/pets", HttpStatusCode.OK,
                                                                             new Dictionary <string, string>(), json);

            Assert.That(validationResult.Valid, Is.False);
            Assert.That(validationResult.Errors.Count, Is.EqualTo(4));
            Assert.That(validationResult.Errors[0], Is.EqualTo("{\"path\":\".response[0].dayOfWeek\",\"message\":\"should NOT have additional properties\",\"errorCode\":\"additionalProperties.openapi.validation\"}"));
            Assert.That(validationResult.Errors[1], Is.EqualTo("{\"path\":\".response[0].id\",\"message\":\"should have required property 'id'\",\"errorCode\":\"required.openapi.validation\"}"));
            Assert.That(validationResult.Errors[2], Is.EqualTo("{\"path\":\".response[0].name\",\"message\":\"should have required property 'name'\",\"errorCode\":\"required.openapi.validation\"}"));
            Assert.That(validationResult.Errors[3], Is.EqualTo("{\"path\":\".response[0].type\",\"message\":\"should have required property 'type'\",\"errorCode\":\"required.openapi.validation\"}"));
        }
예제 #25
0
        public async Task <Dataset> Create(string groupId, string datasetName, bool useBasicFifoRetentionPolicy, params Type[] tableStructures)
        {
            var defaultRetentionPolicy = useBasicFifoRetentionPolicy ? "basicFIFO" : "None";
            var tables  = tableStructures.Select(t => Table.FromType(t)).ToList();
            var dataset = new Dataset {
                name = datasetName, tables = tables
            };

            var request = new RestRequest(_addresses.CreateDataset(groupId), Method.POST)
            {
                JsonSerializer = new Serialization.JsonSerializer()
            };

            request.RequestFormat = DataFormat.Json;
            request.AddUrlSegment("defaultRetentionPolicy", defaultRetentionPolicy);
            request.AddBody(dataset);

            var response = await _client.ExecuteTaskAsync <Dataset>(request);

            ResponseValidation.HandleResponseErrors(response);
            return(response.Data);
        }
        public async Task AssertThatResponseIsValidThrowsAnErrorWhenTheResponseDoesNotConformToTheSpecificationUsingRequestAndResponseOverloads()
        {
            await Setup();

            await ResponseValidation.Initialise("http://localhost:3010/");

            try
            {
                var request  = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost/v1/pets"));
                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("")
                };

                await ResponseValidation.AssertThatResponseIsValid(request, response);

                throw new Exception("Fail");
            }
            catch (Exception e)
            {
                Assert.That(e.Message, Is.EqualTo("Response validation failed with the following errors: {\"path\":\".response\",\"message\":\"response body required.\"}."));
            }
        }
 public void TearDown()
 {
     ResponseValidation.Dispose();
     Bash("docker-compose down");
 }