Search in sources :

Example 61 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-sync by spring-projects.

the class DiffSyncControllerTest method patchSendsEntityIdChange.

@Test
public void patchSendsEntityIdChange() throws Exception {
    TodoRepository todoRepository = todoRepository();
    MockMvc mvc = mockMvc(todoRepository);
    mvc.perform(patch(RESOURCE_PATH + "/2").content(resource("patch-change-entity-id")).accept(JSON_PATCH).contentType(JSON_PATCH)).andExpect(status().isOk()).andExpect(content().string("[]")).andExpect(content().contentType(JSON_PATCH));
    List<Todo> all = (List<Todo>) repository.findAll();
    assertEquals(4, all.size());
    assertEquals(new Todo(1L, "A", false), all.get(0));
    assertEquals(new Todo(2L, "B", false), all.get(1));
    assertEquals(new Todo(3L, "C", false), all.get(2));
    // This is odd behavior, but correct in the context of the backing database.
    assertEquals(new Todo(4L, "B", false), all.get(3));
}
Also used : Todo(org.springframework.sync.Todo) List(java.util.List) TodoRepository(org.springframework.sync.TodoRepository) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 62 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-sync by spring-projects.

the class DiffSyncControllerTest method noChangesFromEitherSide.

//
// list patching
//
@Test
public void noChangesFromEitherSide() throws Exception {
    TodoRepository todoRepository = todoRepository();
    MockMvc mvc = mockMvc(todoRepository);
    mvc.perform(patch(RESOURCE_PATH).content("[]").accept(JSON_PATCH).contentType(JSON_PATCH)).andExpect(content().string("[]")).andExpect(content().contentType(JSON_PATCH)).andExpect(status().isOk());
    List<Todo> all = (List<Todo>) repository.findAll();
    assertEquals(3, all.size());
    assertEquals(all.get(0), new Todo(1L, "A", false));
    assertEquals(all.get(1), new Todo(2L, "B", false));
    assertEquals(all.get(2), new Todo(3L, "C", false));
}
Also used : Todo(org.springframework.sync.Todo) List(java.util.List) TodoRepository(org.springframework.sync.TodoRepository) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 63 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-sync by spring-projects.

the class DiffSyncControllerTest method noChangesFromClientSide_itemDeletedFromServer.

//
// server-side changes
//
@Test
@Ignore
public void noChangesFromClientSide_itemDeletedFromServer() throws Exception {
    TodoRepository todoRepository = todoRepository();
    MockMvc mvc = mockMvc(todoRepository);
    performNoOpRequestToSetupShadow(mvc);
    repository.delete(2L);
    mvc.perform(patch(RESOURCE_PATH).content("[]").accept(JSON_PATCH).contentType(JSON_PATCH)).andExpect(content().string(resource("patch-remove-item"))).andExpect(content().contentType(JSON_PATCH)).andExpect(status().isOk());
    List<Todo> all = (List<Todo>) repository.findAll();
    assertEquals(2, all.size());
    assertEquals(new Todo(1L, "A", false), all.get(0));
    assertEquals(new Todo(3L, "C", false), all.get(1));
}
Also used : Todo(org.springframework.sync.Todo) List(java.util.List) TodoRepository(org.springframework.sync.TodoRepository) MockMvc(org.springframework.test.web.servlet.MockMvc) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 64 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-sync by spring-projects.

the class DiffSyncControllerTest method mockMvc.

private MockMvc mockMvc(TodoRepository todoRepository) {
    ShadowStore shadowStore = new MapBasedShadowStore("x");
    PersistenceCallbackRegistry callbackRegistry = new PersistenceCallbackRegistry();
    callbackRegistry.addPersistenceCallback(new JpaPersistenceCallback<Todo>(todoRepository, Todo.class));
    DiffSyncController controller = new DiffSyncController(callbackRegistry, shadowStore);
    MockMvc mvc = standaloneSetup(controller).setMessageConverters(new JsonPatchHttpMessageConverter()).build();
    return mvc;
}
Also used : Todo(org.springframework.sync.Todo) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) PersistenceCallbackRegistry(org.springframework.sync.diffsync.PersistenceCallbackRegistry) MapBasedShadowStore(org.springframework.sync.diffsync.shadowstore.MapBasedShadowStore) ShadowStore(org.springframework.sync.diffsync.ShadowStore) MockMvc(org.springframework.test.web.servlet.MockMvc)

Example 65 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-sync by spring-projects.

the class DiffSyncControllerTest method patchSendsAStatusChangeAndADescriptionChangeForSameItem.

@Test
public void patchSendsAStatusChangeAndADescriptionChangeForSameItem() throws Exception {
    TodoRepository todoRepository = todoRepository();
    MockMvc mvc = mockMvc(todoRepository);
    mvc.perform(patch(RESOURCE_PATH).content(resource("patch-change-single-status-and-desc")).accept(JSON_PATCH).contentType(JSON_PATCH)).andExpect(status().isOk()).andExpect(content().string("[]")).andExpect(content().contentType(JSON_PATCH));
    List<Todo> all = (List<Todo>) repository.findAll();
    assertEquals(3, all.size());
    assertEquals(all.get(0), new Todo(1L, "A", false));
    assertEquals(all.get(1), new Todo(2L, "BBB", true));
    assertEquals(all.get(2), new Todo(3L, "C", false));
}
Also used : Todo(org.springframework.sync.Todo) List(java.util.List) TodoRepository(org.springframework.sync.TodoRepository) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Aggregations

MockMvc (org.springframework.test.web.servlet.MockMvc)97 Test (org.junit.Test)91 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)25 Todo (org.springframework.sync.Todo)16 List (java.util.List)15 TodoRepository (org.springframework.sync.TodoRepository)15 Filter (javax.servlet.Filter)13 MockServletContext (org.springframework.mock.web.MockServletContext)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)9 MvcResult (org.springframework.test.web.servlet.MvcResult)6 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)5 Ignore (org.junit.Ignore)3 FilterChainProxy (org.springframework.security.web.FilterChainProxy)3 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpSession (javax.servlet.http.HttpSession)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2