chapter_011_Spring_Boot/src/test/java/my/jpa/controllers/ServletIntegrationTest.java
@Test
    @WithMockUser(username = "user@gmail.com", roles = {"User"})
    public void testUserController() throws Exception {

        // Test Create
        this.mvc.perform(
                post(UserServlet.USER_URI)
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .content("{\"user\":{\"id\":\"1\",\"login\":\"TestUser\",\"email\":\"test@gmail.com\",\"password\":\"vfitymrf\",\"createDate\":\"null\"},\"command\":\"CREATE\"}"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(
                        content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                )
                .andExpect(jsonPath("$.id").value(1))
                .andExpect(jsonPath("$.login").value("TestUser"))
                .andReturn();
        // Get all
        this.mvc.perform(get(UserServlet.USER_URI).accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(status().isOk())
                .andExpect(
                        content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                )
                .andExpect(jsonPath("$[0].id").value(1))
                .andExpect(jsonPath("$[0].login").value("TestUser"))
                .andDo(print())
                .andReturn();
        //Test UPDATE
        this.mvc.perform(
                post(UserServlet.USER_URI)
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .content("{\"user\":{\"id\":1,\"login\":\"UpdateUser\"},\"command\":\"UPDATE\"}")
        )
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(
                        content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)

                )
                .andExpect(jsonPath("$.login").value("UpdateUser"))
                .andReturn();

        //Test DELETE
        this.mvc.perform(
                post(UserServlet.USER_URI)
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .content("{\"user\":{\"id\":1},\"command\":\"DELETE\"}")
        )
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn();
        this.mvc.perform(get(UserServlet.USER_URI).accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(status().isOk())
                .andExpect(
                        content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                )
                .andExpect(jsonPath("$.id").doesNotExist())
                .andDo(print())
                .andReturn();
    }
/**
     * Testing CRUD operation by User controller.
     *
     * @throws Exception
     */
Testing CRUD operation by User controller.
@throws Exception
Testing CRUD operation by User controller . @throws Exception
@Test
    @WithMockUser(username = "user@gmail.com", roles = {"User"})
    public void testUserController() throws Exception {
        this.mvc.perform(
                post(UserServlet.USER_URI)
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .content("{\"user\":{\"id\":\"1\",\"login\":\"TestUser\",\"email\":\"test@gmail.com\",\"password\":\"vfitymrf\",\"createDate\":\"null\"},\"command\":\"CREATE\"}"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(
                        content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                )
                .andExpect(jsonPath("$.id").value(1))
                .andExpect(jsonPath("$.login").value("TestUser"))
                .andReturn();
        this.mvc.perform(get(UserServlet.USER_URI).accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(status().isOk())
                .andExpect(
                        content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                )
                .andExpect(jsonPath("$[0].id").value(1))
                .andExpect(jsonPath("$[0].login").value("TestUser"))
                .andDo(print())
                .andReturn();
        this.mvc.perform(
                post(UserServlet.USER_URI)
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .content("{\"user\":{\"id\":1,\"login\":\"UpdateUser\"},\"command\":\"UPDATE\"}")
        )
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(
                        content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                )
                .andExpect(jsonPath("$.login").value("UpdateUser"))
                .andReturn();
        this.mvc.perform(
                post(UserServlet.USER_URI)
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .content("{\"user\":{\"id\":1},\"command\":\"DELETE\"}")
        )
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn();
        this.mvc.perform(get(UserServlet.USER_URI).accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
                .andExpect(status().isOk())
                .andExpect(
                        content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                )
                .andExpect(jsonPath("$.id").doesNotExist())
                .andDo(print())
                .andReturn();
    }
@ Test @ WithMockUser ( username = "user@gmail.com" , roles = { "User" } ) public void testUserController ( ) throws Exception { this . mvc . perform ( post ( UserServlet . USER_URI ) . contentType ( MediaType . APPLICATION_JSON_UTF8 ) . content ( "{\"user\":{\"id\":\"1\",\"login\":\"TestUser\",\"email\":\"test@gmail.com\",\"password\":\"vfitymrf\",\"createDate\":\"null\"},\"command\":\"CREATE\"}" ) ) . andDo ( print ( ) ) . andExpect ( status ( ) . isOk ( ) ) . andExpect ( content ( ) . contentType ( MediaType . APPLICATION_JSON_UTF8_VALUE ) ) . andExpect ( jsonPath ( "$.id" ) . value ( 1 ) ) . andExpect ( jsonPath ( "$.login" ) . value ( "TestUser" ) ) . andReturn ( ) ; this . mvc . perform ( get ( UserServlet . USER_URI ) . accept ( MediaType . APPLICATION_JSON_UTF8_VALUE ) ) . andExpect ( status ( ) . isOk ( ) ) . andExpect ( content ( ) . contentType ( MediaType . APPLICATION_JSON_UTF8_VALUE ) ) . andExpect ( jsonPath ( "$[0].id" ) . value ( 1 ) ) . andExpect ( jsonPath ( "$[0].login" ) . value ( "TestUser" ) ) . andDo ( print ( ) ) . andReturn ( ) ; this . mvc . perform ( post ( UserServlet . USER_URI ) . contentType ( MediaType . APPLICATION_JSON_UTF8 ) . content ( "{\"user\":{\"id\":1,\"login\":\"UpdateUser\"},\"command\":\"UPDATE\"}" ) ) . andDo ( print ( ) ) . andExpect ( status ( ) . isOk ( ) ) . andExpect ( content ( ) . contentType ( MediaType . APPLICATION_JSON_UTF8_VALUE ) ) . andExpect ( jsonPath ( "$.login" ) . value ( "UpdateUser" ) ) . andReturn ( ) ; this . mvc . perform ( post ( UserServlet . USER_URI ) . contentType ( MediaType . APPLICATION_JSON_UTF8 ) . content ( "{\"user\":{\"id\":1},\"command\":\"DELETE\"}" ) ) . andDo ( print ( ) ) . andExpect ( status ( ) . isOk ( ) ) . andReturn ( ) ; this . mvc . perform ( get ( UserServlet . USER_URI ) . accept ( MediaType . APPLICATION_JSON_UTF8_VALUE ) ) . andExpect ( status ( ) . isOk ( ) ) . andExpect ( content ( ) . contentType ( MediaType . APPLICATION_JSON_UTF8_VALUE ) ) . andExpect ( jsonPath ( "$.id" ) . doesNotExist ( ) ) . andDo ( print ( ) ) . andReturn ( ) ; }
Testing CRUD operation by User controller.
Testing CRUD operation by User controller .
// Test Create // Get all //Test UPDATE //Test DELETE