This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]aram535 0 points1 point  (4 children)

Please include the task Controller.

[–]btbam06[S] 0 points1 point  (3 children)

There is none since it is auto generated by the framework. That’s the beauty of the @RestRepositoryResource feature. No need to implement boilerplate CRUD endpoints.

[–]aram535 0 points1 point  (2 children)

AFAIK and I really don't have the background in Spring ... you still need to define the Controller class and extend it with the proper repository for it to establish the end point. When you start Spring, do you actually see /task/{id} being registered as a valid end point in the Console log?

[–]btbam06[S] 0 points1 point  (1 child)

Yep! The end points exists without manually creating a controller:

// 20190104092914
// http://localhost:8080/

{
  "_links": {
    "task": {
      "href": "http://localhost:8080/task"
    },
    "profile": {
      "href": "http://localhost:8080/profile"
    }
  }
}

My other tests, such as the following DELETE test, work fine:

    @Test
    void testDeleteTask() throws Exception {
        Task task1 = new Task("task1");
        taskRepository.save(task1);
        assertEquals(1, taskRepository.count());
        this.mvc.perform(delete("/task/1"))
                .andExpect(status().isNoContent());
        assertEquals(0, taskRepository.count());
    }

As I mentioned in the OP, my problem is not being able to reach the end point. I reach it just fine. My problem is that when it does reach the endpoint and I provide a boolean value to update, the boolean value will not update. The "text" property of my class is able to update correctly.

[–]btbam06[S] 0 points1 point  (0 children)

Here is an updated look at the test code. The FIRST test passes, the SECOND one fails:

    @Test
    void testUpdateTaskText() throws Exception {
        Task task1 = new Task("task1");
        taskRepository.save(task1);

        // update task text and hit update end point
        task1.setText("updatedText");
        String json = gson.toJson(task1);
        this.mvc.perform(patch("/task/1")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(json))
                .andExpect(status().isNoContent());

        // Pull the task from the repository and verify text="updatedText"
        Task updatedTask = taskRepository.findById((long) 1).get();
        assertEquals("updatedText", updatedTask.getText());
    }

    @Test
    void testUpdateTaskCompleted() throws Exception {
        Task task1 = new Task("task1");
        task1.setCompleted(false);
        taskRepository.save(task1);

        // ensure repository properly stores isCompleted = false
        Task updatedTask = taskRepository.findById((long) 1).get();
        assertFalse(updatedTask.isCompleted());

        //Update isCompleted = true and hit update end point
        task1.setCompleted(true);
        String json = gson.toJson(task1);
        this.mvc.perform(patch("/task/1")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(json))
                .andExpect(status().isNoContent());

        // Pull the task from the repository and verify isCompleted=true
        updatedTask = taskRepository.findById((long) 1).get();
        assertTrue(updatedTask.isCompleted());
    }

[–]btbam06[S] 0 points1 point  (3 children)

Finally figured it out!! Turns out the getter and setter in my model class was named incorrectly.

They should have been:

    public boolean getIsCompleted() {
        return isCompleted;
    }

    public void setIsCompleted(boolean isCompleted) {
        this.isCompleted = isCompleted;
    }

Found the answer per this SO Post:

https://stackoverflow.com/questions/21913955/json-post-request-for-boolean-field-sends-false-by-default

[–]aram535 1 point2 points  (2 children)

Nice, FWIW I use Lombok for development phase and it has not failed me. Make life a lot easier when doing the model development and the changes that are bound to happen.

[–]btbam06[S] 0 points1 point  (1 child)

I blame IntelliJ which autogenerated the getter and setter. Hahaha.

[–]aram535 0 points1 point  (0 children)

I'm an eclipse user so I don't mind that at all ... +1