Exemplo n.º 1
0
 public void WhenTheClientGetsTheProjectByHeaderLocation()
 {
     var client = StepHelpers.SetupHttpClient(_projectTestModel.UserName, _projectTestModel.Password);
     _responseContent = client.GetAsync(_responseContent.Headers.Location).Result;
     _projectSaved = JsonConvert.DeserializeObject<Project>(_responseContent.Content.ReadAsStringAsync().Result);
     client.Dispose();
 }
Exemplo n.º 2
0
 public void WhenTheClientGetsTheProjectByID()
 {
     var client = StepHelpers.SetupHttpClient(_projectTestModel.UserName, _projectTestModel.Password);
     _responseContent = client.GetAsync("http://localhost:55301/api/v1/projects/" + _projectSaved.ProjectId).Result;
     _projectSaved = JsonConvert.DeserializeObject<Project>(_responseContent.Content.ReadAsStringAsync().Result);
     client.Dispose();
 }
        // POST api/projects (Insert)
        public override HttpResponseMessage Post(Project project)
        {
            // Check for presence of V2 "IsUrgent" flag.
            if (project.IsUrgent == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The IsUrgent indicator is required.");
            }

            try
            {
                project.UserName = User.Identity.Name;
                project.DateCreated = DateTime.Now;
                project.DateUpdated = DateTime.Now;
                _projectRepository.Insert(project);
                _projectRepository.Commit();
            }
            catch (Exception ex)
            {
                var error = new HttpError("Error inserting project: " + ex.Message) { { "Trace", ex.StackTrace } };
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
            }
            var response = Request.CreateResponse(HttpStatusCode.Created, project);
            string uri = Url.Link("DefaultApi", new { id = project.ProjectId });
            response.Headers.Location = new Uri(uri);
            return response;
        }
        // GET /api/projects/4/researchitems/5
        public virtual HttpResponseMessage Get(int projectId, int researchItemId)
        {
            var researchItems = new List<ResearchItem>();
            var project = new Project();
            try
            {
                // Verify that the user is the owner of project and research item.
                project = _projectRepository.GetByUser(projectId, User.Identity.Name);
                if (project == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Project not found for user " + User.Identity.Name + ".");
                }
                var researchItem = _researchItemRepository.GetByUser(researchItemId, User.Identity.Name);
                if (researchItem == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Research item not found for user " + User.Identity.Name + ".");
                }
                researchItems.Add(researchItem);
            }
            catch (Exception ex)
            {
                var error = new HttpError("Error getting research item: " + ex.Message) { { "Trace", ex.StackTrace } };
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
            }

            var researchItemDto = new ResearchItemDto()
            {
                Meta = new ResearchItemMeta() { NumberResearchItems = researchItems.Count(), ProjectName = project.Name },
                ResearchItems = researchItems
            };

            return Request.CreateResponse<ResearchItemDto>(HttpStatusCode.OK, researchItemDto);
        }
        // PUT api/projects/5  (Update)
        public override HttpResponseMessage Put(int id, Project project)
        {
            // Check for presence of V2 "IsUrgent" flag.
            if (project.IsUrgent == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The IsUrgent indicator is required.");
            }

            var currentProject = new Project();
            try
            {
                // Verify that the user is the owner
                currentProject = _projectRepository.GetByUser(id, User.Identity.Name);
                if (currentProject == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Project not found for user " + User.Identity.Name + ".");
                }
                currentProject.DateUpdated = DateTime.Now;
                currentProject.Name = project.Name;
                currentProject.Description = project.Description;
                currentProject.IsUrgent = project.IsUrgent;
                _projectRepository.Commit();
            }
            catch (Exception ex)
            {
                var error = new HttpError("Error updating project: " + ex.Message) { { "Trace", ex.StackTrace } };
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK, currentProject);
            string uri = Url.Link("DefaultApi", new { id = id });
            response.Headers.Location = new Uri(uri);
            return response;
        }
 // GET api/projects/5 (Detail)
 public virtual HttpResponseMessage Get(int id)
 {
     var project = new Project();
     try
     {
         // Verify that the user is the owner
         project = _projectRepository.GetByUser(id, User.Identity.Name);
         if (project == null)
         {
             return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Project not found for user " + User.Identity.Name + ".");
         }
     }
     catch (Exception ex)
     {
         var error = new HttpError("Error getting project: " + ex.Message) { { "Trace", ex.StackTrace } };
         return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
     }
     return Request.CreateResponse<Project>(HttpStatusCode.OK, project);
 }
 // POST api/projects (Insert)
 public virtual HttpResponseMessage Post(Project project)
 {
     try
     {
         project.UserName = User.Identity.Name;
         project.DateCreated = DateTime.Now;
         project.DateUpdated = DateTime.Now;
         _projectRepository.Insert(project);
         _projectRepository.Commit();
     }
     catch (Exception ex)
     {
         var error = new HttpError("Error inserting project: " + ex.Message) { { "Trace", ex.StackTrace } };
         return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
     }
     var response = Request.CreateResponse(HttpStatusCode.Created, project);
     string uri = Url.Link("DefaultApi", new { id = project.ProjectId });
     response.Headers.Location = new Uri(uri);
     return response;
 }
        // PUT /api/projects/4/researchitems/5
        public virtual HttpResponseMessage Put(int projectId, int researchItemId, ResearchItem researchItem)
        {
            var project = new Project();
            ResearchItem currentResearchItem = null;
            try
            {
                project = _projectRepository.GetByUser(projectId, User.Identity.Name);
                if (project == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Project not found for user " + User.Identity.Name + ".");
                }
                currentResearchItem = _researchItemRepository.GetByUser(researchItemId, User.Identity.Name);
                if (currentResearchItem == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Research item not found for user " + User.Identity.Name + ".");
                }
                currentResearchItem.DateUpdated = DateTime.Now;
                currentResearchItem.Subject = researchItem.Subject;
                currentResearchItem.Description = researchItem.Description;
                _researchItemRepository.Commit();
            }
            catch (Exception ex)
            {
                var error = new HttpError("Error updating research item: " + ex.Message) { { "Trace", ex.StackTrace } };
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
            }

            var response = Request.CreateResponse(HttpStatusCode.OK, currentResearchItem);
            string uri = Url.Link("ProjectResearchItemsApi", new { projectId = projectId, researchItemId = researchItem.ResearchItemId });
            response.Headers.Location = new Uri(uri);
            return response;
        }
        public void Put_Project_Database_Exception_Returns_Error(string version)
        {
            //Setup
            _projectRepository = _mockRepositories.GetProjectsRepository(ReturnType.Exception);
            var projectsController = SetupController(_projectRepository.Object, "james", HttpMethod.Put, version);
            var inputProject = new Project() { ProjectId = 1, Name = "Update Test 1", UserName = "******", Description = "Insert Test Description", IsUrgent = true };

            //Act
            var response = projectsController.Put(1, inputProject);
            dynamic errorMessage = JObject.Parse(response.Content.ReadAsStringAsync().Result);

            //Assert
            Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode, "Expecting an Internal Server Error Status Code");
            Assert.AreEqual("Error updating project: Database exception!", (string)errorMessage.Message);
        } 
        public void Put_Project_When_IsUrgent_Missing_Returns_Bad_Request(string version)
        {
            //Setup
            _projectRepository = _mockRepositories.GetProjectsRepository(ReturnType.Exception);
            var projectsController = SetupController(_projectRepository.Object, "james", HttpMethod.Put, version);
            var inputProject = new Project() { ProjectId = 1, Name = "Update Test 1", UserName = "******", Description = "Insert Test Description" };

            //Act
            var response = projectsController.Put(1, inputProject);
            dynamic errorMessage = JObject.Parse(response.Content.ReadAsStringAsync().Result);

            //Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode, "Expecting a Bad Request Status Code");
        } 
        public void Put_James_Project_Returns_Null_Project_For_John(string version)
        {
            //Setup
            _projectRepository = _mockRepositories.GetProjectsRepository(ReturnType.Normal);
            var projectsController = SetupController(_projectRepository.Object, "john", HttpMethod.Put, version);
            var inputProject = new Project() { ProjectId = 1, Name = "Update Test 1", UserName = "******", Description = "Insert Test Description", IsUrgent = true };

            //Act
            var response = projectsController.Put(1, inputProject);
            dynamic errorMessage = JObject.Parse(response.Content.ReadAsStringAsync().Result);

            //Assert
            Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode, "Expecting a Not Found Status Code");
            Assert.AreEqual("Project not found for user john.", (string)errorMessage.Message);
        }
        public void Put_Project_Returns_Expected_Header_For_James(string version)
        {
            //Setup
            _projectRepository = _mockRepositories.GetProjectsRepository(ReturnType.Normal);
            var projectsController = SetupController(_projectRepository.Object, "james", HttpMethod.Put, version);
            var inputProject = new Project() { ProjectId = 1, Name = "Update Test 1", UserName = "******", Description = "Insert Test Description", IsUrgent = true };

            //Act
            var response = projectsController.Put(1, inputProject);

            //Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Expecting an OK Status Code");
            Assert.AreEqual("http://localhost/api/projects/1", response.Headers.Location.ToString());
        }
        public void Post_Project_When_IsUrgent_Missing_Returns_Bad_Request(string version)
        {
            //Setup
            _projectRepository = _mockRepositories.GetProjectsRepository(ReturnType.Normal);
            var projectsController = SetupController(_projectRepository.Object, "james", HttpMethod.Post, version);
            var inputProject = new Project() { ProjectId = 1, Name = "Insert Test", UserName = "******", Description = "Insert Test Description" };

            //Act
            var response = projectsController.Post(inputProject);

            //Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode, "Expecting a Bad Request Status Code");
        }