From d480ee1b6115fb614a7692146778559d77effa77 Mon Sep 17 00:00:00 2001 From: Den Street Date: Fri, 11 Mar 2016 23:31:23 +0200 Subject: [PATCH 1/2] topicController test --- .../app/controllers/TopicControllerTest.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/ayfaar/app/controllers/TopicControllerTest.java b/src/test/java/org/ayfaar/app/controllers/TopicControllerTest.java index b4da77bb..6736297e 100644 --- a/src/test/java/org/ayfaar/app/controllers/TopicControllerTest.java +++ b/src/test/java/org/ayfaar/app/controllers/TopicControllerTest.java @@ -10,16 +10,19 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import static com.jayway.restassured.RestAssured.given; import static com.jayway.restassured.RestAssured.when; @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles("dev") @SpringApplicationConfiguration(Application.class) -@WebIntegrationTest +@WebIntegrationTest(randomPort = true) public class TopicControllerTest { @Test public void testGet() { + when(). + get("/api/topic/{name}/add-child/{child}", "1","2").then().log().all(); when(). get("/api/topic/{name}", "1"). then(). @@ -32,4 +35,44 @@ public void testGet() { body("parents", Matchers.hasItem("4")) ; } + + @Test + + public void testAddFor() { + given(). + parameters("name", "19999", "uri", "тема:19999"). + when(). + post("/api/topic/for"). + then(). + log().all(). + statusCode(HttpStatus.SC_OK) + + ; + } + + @Test + public void testAddChild() { + + when(). + get("/api/topic/{name}/add-child/{child}", "1", "2"). + then(). + log().all(). + statusCode(HttpStatus.SC_OK) + + ; + } + + + + @Test + public void testUnLink() { + + when(). + get("/api/topic/{name}/unlink/{linked}", "1", "2"). + then(). + log().all(). + statusCode(HttpStatus.SC_OK) + + ; + } } \ No newline at end of file From 7fd7bcaf314b23d6a0e8a71c895b103545878ce4 Mon Sep 17 00:00:00 2001 From: Sllouyssgort Date: Sat, 12 Mar 2016 01:45:38 +0300 Subject: [PATCH 2/2] test review --- .../app/controllers/TopicController.java | 1 - .../app/controllers/TopicControllerTest.java | 67 ++++++++++--------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/ayfaar/app/controllers/TopicController.java b/src/main/java/org/ayfaar/app/controllers/TopicController.java index f82931e1..a2a8aafa 100644 --- a/src/main/java/org/ayfaar/app/controllers/TopicController.java +++ b/src/main/java/org/ayfaar/app/controllers/TopicController.java @@ -68,7 +68,6 @@ public void importTopics(@RequestBody String topics) throws Exception { @RequestMapping(value = "for", method = POST) public Topic addFor(@RequestParam String uri, @RequestParam String name) throws Exception { - hasLength(name); Topic topic = commonDao.get(Topic.class, "name", name); if (topic == null) topic = commonDao.save(new Topic(name)); linkDao.save(new Link(topic, commonDao.get(UriGenerator.getClassByUri(uri), uri))); diff --git a/src/test/java/org/ayfaar/app/controllers/TopicControllerTest.java b/src/test/java/org/ayfaar/app/controllers/TopicControllerTest.java index 6736297e..4f970f5c 100644 --- a/src/test/java/org/ayfaar/app/controllers/TopicControllerTest.java +++ b/src/test/java/org/ayfaar/app/controllers/TopicControllerTest.java @@ -16,49 +16,51 @@ @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles("dev") @SpringApplicationConfiguration(Application.class) -@WebIntegrationTest(randomPort = true) +@WebIntegrationTest public class TopicControllerTest { @Test public void testGet() { - - when(). - get("/api/topic/{name}/add-child/{child}", "1","2").then().log().all(); when(). - get("/api/topic/{name}", "1"). - then(). - log().all(). - statusCode(HttpStatus.SC_OK). + get("/api/topic/{name}/add-child/{child}", "1", "2"). + then() + .log().all(). + statusCode(HttpStatus.SC_OK); + + // todo добавить ещё чайлда "3" и парента "4" - body("name", Matchers.is("1")). - body("uri", Matchers.is("тема:1")). - body("children", Matchers.hasItems("3", "2")). - body("parents", Matchers.hasItem("4")) + when(). + get("/api/topic/{name}", "1"). + then(). + log().all(). + statusCode(HttpStatus.SC_OK). + body("name", Matchers.is("1")). + body("uri", Matchers.is("тема:1")). + body("children", Matchers.hasItems("3", "2")). + body("parents", Matchers.hasItem("4")) ; } @Test - public void testAddFor() { - given(). - parameters("name", "19999", "uri", "тема:19999"). - when(). - post("/api/topic/for"). - then(). - log().all(). - statusCode(HttpStatus.SC_OK) - + given(). + contentType("application/x-www-form-urlencoded; charset=UTF-8"). // это чтобы русский понимал в параметрах + param("name", "1"). // имя темы + param("uri", "видео:youtube:_8vYBLrOq-w"). // uri объекта к которому прилинковать тему + when(). + post("/api/topic/for"). + then(). + log().all(). + statusCode(HttpStatus.SC_OK) ; } @Test public void testAddChild() { - when(). - get("/api/topic/{name}/add-child/{child}", "1", "2"). - then(). - log().all(). - statusCode(HttpStatus.SC_OK) - + get("/api/topic/{name}/add-child/{child}", "1", "2"). + then(). + log().all(). + statusCode(HttpStatus.SC_OK) ; } @@ -66,13 +68,14 @@ public void testAddChild() { @Test public void testUnLink() { + // сначала прилинковываем + when().get("/api/topic/{name}/add-child/{child}", "1", "2").then().statusCode(HttpStatus.SC_OK); when(). - get("/api/topic/{name}/unlink/{linked}", "1", "2"). - then(). - log().all(). - statusCode(HttpStatus.SC_OK) - + get("/api/topic/{name}/unlink/{linked}", "1", "2"). + then(). + log().all(). + statusCode(HttpStatus.SC_OK) ; } } \ No newline at end of file