From 12ac7df4b39102051599c7ae706d6d6e1c752d16 Mon Sep 17 00:00:00 2001 From: Roman Konovalov Date: Tue, 17 Mar 2015 10:46:53 +0300 Subject: [PATCH 1/5] Fix services --- .../epam/eighty/repository/TagRepository.java | 2 +- .../epam/eighty/service/CustomerService.java | 2 +- .../epam/eighty/service/QuestionService.java | 16 ++++----- .../com/epam/eighty/service/TagService.java | 2 +- .../com/epam/eighty/service/TopicService.java | 11 +++--- .../service/impl/CustomerServiceImpl.java | 10 +++--- .../service/impl/QuestionServiceImpl.java | 32 ++++++++--------- .../eighty/service/impl/TagServiceImpl.java | 8 ++--- .../eighty/service/impl/TopicServiceImpl.java | 36 +++++++++---------- .../eighty/web/api/CustomerController.java | 2 +- .../eighty/web/api/QuestionController.java | 36 +++++++++---------- .../epam/eighty/web/api/TagController.java | 2 +- .../epam/eighty/web/api/TopicController.java | 4 +-- .../eighty/repository/TagRepossitoryTest.java | 4 +-- .../eighty/service/CustomerServiceTest.java | 2 +- .../eighty/service/QuestionServiceTest.java | 14 ++++---- .../epam/eighty/service/TagServiceTest.java | 4 +-- .../epam/eighty/service/TopicServiceTest.java | 10 +++--- .../web/api/CustomerControllerTest.java | 2 +- .../web/api/QuestionControllerTest.java | 24 ++++--------- .../eighty/web/api/TagControllerTest.java | 2 +- .../eighty/web/api/TopicControllerTest.java | 20 ++--------- 22 files changed, 102 insertions(+), 143 deletions(-) diff --git a/src/main/java/com/epam/eighty/repository/TagRepository.java b/src/main/java/com/epam/eighty/repository/TagRepository.java index 7b6fdfd..43dbe7d 100644 --- a/src/main/java/com/epam/eighty/repository/TagRepository.java +++ b/src/main/java/com/epam/eighty/repository/TagRepository.java @@ -20,7 +20,7 @@ public interface TagRepository extends BaseRepository { List getTopNFromAllTags(Long limit); @Query(value = "MATCH (tag:`Tag`) WHERE tag.tag =~ ('.*' + {0} + '.*') RETURN tag ORDER BY tag.tag", elementClass = Tag.class) - List getSortedListOfTagsByName(String tagName); + List getSortedTagsMatchingName(String tagName); @Query(value = "MATCH (tag:`Tag`) WHERE NOT(tag<-[:has]-()) DELETE tag", elementClass = Tag.class) void removeTagsWithoutQuestion(); diff --git a/src/main/java/com/epam/eighty/service/CustomerService.java b/src/main/java/com/epam/eighty/service/CustomerService.java index 86ba861..dfb1ca9 100644 --- a/src/main/java/com/epam/eighty/service/CustomerService.java +++ b/src/main/java/com/epam/eighty/service/CustomerService.java @@ -17,7 +17,7 @@ public interface CustomerService { */ List getAllCustomers(); - List getSortedSetOfCustomersByName(String customerName); + List getSortedCustomersMatchingName(String customerName); /** * Retrieves a list of customer {@link com.epam.eighty.domain.Customer} entities whose questions present in topic with given id . diff --git a/src/main/java/com/epam/eighty/service/QuestionService.java b/src/main/java/com/epam/eighty/service/QuestionService.java index b975164..fd12114 100644 --- a/src/main/java/com/epam/eighty/service/QuestionService.java +++ b/src/main/java/com/epam/eighty/service/QuestionService.java @@ -1,25 +1,23 @@ package com.epam.eighty.service; -import com.epam.eighty.domain.Question; +import java.util.List; import org.springframework.data.domain.Pageable; -import java.util.List; -import java.util.Optional; -import java.util.Set; +import com.epam.eighty.domain.Question; /** * @author Aliaksandr_Padalka */ public interface QuestionService { - Optional getQuestionById(Long id); - Set getAllQuestions(); + Question getQuestionById(Long id); + List getAllQuestions(); void addQuestion(Question question, Long topicId); void updateQuestion(Question question); void deleteQuestion(Long id); - List getQuestionsPage(Long topicId, Pageable pageable); - List getQuestionsByTopicAndTag(Long topicId, String tag); + List getQuestionsByTopicId(Long topicId, Pageable pageable); + List getQuestionsByTopicIdAndTag(Long topicId, String tag); List getQuestionsByTag(String tag); /** @@ -28,5 +26,5 @@ public interface QuestionService { * @param customer a name of customer * @return a list of all customer's questions */ - List getQuestionsByCustomer(String customer); + List getQuestionsByCustomerName(String customer); } diff --git a/src/main/java/com/epam/eighty/service/TagService.java b/src/main/java/com/epam/eighty/service/TagService.java index baa47dc..61437c7 100644 --- a/src/main/java/com/epam/eighty/service/TagService.java +++ b/src/main/java/com/epam/eighty/service/TagService.java @@ -13,6 +13,6 @@ public interface TagService { Tag getTagByTag(String title); List getAllTags(); List getTopNFromAllTags(Long limit); - List getSortedSetOfTagsByName(String tagName); + List getSortedTagsMatchingName(String tagName); } diff --git a/src/main/java/com/epam/eighty/service/TopicService.java b/src/main/java/com/epam/eighty/service/TopicService.java index baac21e..0028aaf 100644 --- a/src/main/java/com/epam/eighty/service/TopicService.java +++ b/src/main/java/com/epam/eighty/service/TopicService.java @@ -1,10 +1,9 @@ package com.epam.eighty.service; -import com.epam.eighty.domain.Topic; - import java.util.List; import java.util.Optional; -import java.util.Set; + +import com.epam.eighty.domain.Topic; /** * @author Aliaksandr_Padalka @@ -12,11 +11,11 @@ public interface TopicService { Optional getRoot(); - Optional getTopicById(Long id); - Set getAllTopics(); + Topic getTopicById(Long id); + List getAllTopics(); void updateTopic(Topic topic); void deleteTopic(Long id); - Optional getFullTopicById(Long id); + Topic getFullTopicById(Long id); Topic createTopic(Topic topic, Long id); List getRootTopicsForTopic(Long id); } diff --git a/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java b/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java index df3a967..788c66d 100644 --- a/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java +++ b/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java @@ -20,26 +20,24 @@ @Transactional public class CustomerServiceImpl implements CustomerService { - private static final String ANY_SYMBOL = ".*"; - @Autowired private CustomerRepository customerRepository; @SuppressWarnings("unchecked") @Override public List getAllCustomers() { - Collection customers = customerRepository.findAll().as(Collection.class); + final Collection customers = customerRepository.findAll().as(Collection.class); return customers.stream().filter(customer -> customer.getCount() != null).collect(Collectors.toList()); } @Override - public List getSortedSetOfCustomersByName(final String customerName) { - return customerRepository.getSortedCustomersMatchingName(ANY_SYMBOL + customerName + ANY_SYMBOL); + public List getSortedCustomersMatchingName(final String customerName) { + return customerRepository.getSortedCustomersMatchingName(customerName); } @Override public List getCustomersByTopicId(final Long topicId) { - List customerList = customerRepository.getCustomersByTopicId(topicId); + final List customerList = customerRepository.getCustomersByTopicId(topicId); customerList .forEach(customer -> customer.setCountInTopic(customerRepository.getQuestionsNumberInTopicByCustomer(customer.getName(), diff --git a/src/main/java/com/epam/eighty/service/impl/QuestionServiceImpl.java b/src/main/java/com/epam/eighty/service/impl/QuestionServiceImpl.java index 670c6ba..3d3db03 100644 --- a/src/main/java/com/epam/eighty/service/impl/QuestionServiceImpl.java +++ b/src/main/java/com/epam/eighty/service/impl/QuestionServiceImpl.java @@ -1,15 +1,6 @@ package com.epam.eighty.service.impl; -import com.epam.eighty.domain.Question; -import com.epam.eighty.domain.Topic; -import com.epam.eighty.exception.TopicNotFoundException; -import com.epam.eighty.repository.QuestionRepository; -import com.epam.eighty.repository.TopicRepository; -import com.epam.eighty.service.QuestionService; - import java.util.List; -import java.util.Optional; -import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; @@ -17,6 +8,13 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.epam.eighty.domain.Question; +import com.epam.eighty.domain.Topic; +import com.epam.eighty.exception.TopicNotFoundException; +import com.epam.eighty.repository.QuestionRepository; +import com.epam.eighty.repository.TopicRepository; +import com.epam.eighty.service.QuestionService; + /** * @author Aliaksandr_Padalka */ @@ -33,19 +31,19 @@ public class QuestionServiceImpl implements QuestionService { private Neo4jOperations template; @Override - public Optional getQuestionById(final Long id) { - return questionRepo.findOne(id); + public Question getQuestionById(final Long id) { + return questionRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id)); } @SuppressWarnings("unchecked") @Override - public Set getAllQuestions() { - return questionRepo.findAll().as(Set.class); + public List getAllQuestions() { + return questionRepo.findAll().as(List.class); } @Override public void addQuestion(final Question question, final Long id) { - Topic topic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id)); + final Topic topic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id)); questionRepo.save(question); topic.getQuestions().add(question); topicRepo.save(topic); @@ -62,12 +60,12 @@ public void deleteQuestion(final Long id) { } @Override - public List getQuestionsPage(final Long topicId, final Pageable pageable) { + public List getQuestionsByTopicId(final Long topicId, final Pageable pageable) { return questionRepo.getQuestionsByTopicId(topicId, pageable).getContent(); } @Override - public List getQuestionsByTopicAndTag(final Long topicId, final String tag) { + public List getQuestionsByTopicIdAndTag(final Long topicId, final String tag) { return questionRepo.getQuestionsByTopicIdAndTag(topicId, tag); } @@ -77,7 +75,7 @@ public List getQuestionsByTag(final String tag) { } @Override - public List getQuestionsByCustomer(final String customer) { + public List getQuestionsByCustomerName(final String customer) { return questionRepo.getQuestionsByCustomerName(customer); } diff --git a/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java b/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java index 59c4027..cb645e9 100644 --- a/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java +++ b/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java @@ -17,14 +17,12 @@ @Transactional public class TagServiceImpl implements TagService { - private static final String ANY_SYMBOL = ".*"; - @Autowired private TagRepository tagRepo; @Override public List getTagsByTopicId(final Long topicId) { - List tagsList = tagRepo.getTagsByTopicId(topicId); + final List tagsList = tagRepo.getTagsByTopicId(topicId); tagsList .forEach(tag -> tag.setCountInTopic(tagRepo.getQuestionsNumberInTopicByTag(tag.getTag(), topicId))); return tagsList; @@ -47,8 +45,8 @@ public List getTopNFromAllTags(final Long limit) { } @Override - public List getSortedSetOfTagsByName(final String tagName) { - return tagRepo.getSortedListOfTagsByName(ANY_SYMBOL + tagName + ANY_SYMBOL); + public List getSortedTagsMatchingName(final String tagName) { + return tagRepo.getSortedTagsMatchingName(tagName); } } diff --git a/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java b/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java index 900229f..9a20e81 100644 --- a/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java +++ b/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java @@ -1,6 +1,7 @@ package com.epam.eighty.service.impl; import com.epam.eighty.domain.Topic; +import com.epam.eighty.exception.TopicNotFoundException; import com.epam.eighty.repository.TopicRepository; import com.epam.eighty.service.TopicService; @@ -11,7 +12,6 @@ import java.util.List; import java.util.Optional; -import java.util.Set; /** * @author Aliaksandr_Padalka @@ -28,7 +28,7 @@ public class TopicServiceImpl implements TopicService { @Override public Optional getRoot() { - Optional root = topicRepo.findBySchemaPropertyValue("title", "root"); + final Optional root = topicRepo.findBySchemaPropertyValue("title", "root"); root.ifPresent(someRoot -> someRoot.getTopics().forEach(template::fetch) ); @@ -36,28 +36,26 @@ public Optional getRoot() { } @Override - public Optional getFullTopicById(final Long id) { - Optional topic = topicRepo.findOne(id); - topic.ifPresent(t -> { - t.getTopics().forEach(template::fetch); - t.getQuestions().forEach(template::fetch); - }); + public Topic getFullTopicById(final Long id) { + final Topic topic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id)); + topic.getTopics().forEach(template::fetch); + topic.getQuestions().forEach(template::fetch); + return topic; } @Override - public Optional getTopicById(final Long id) { - Optional topic = topicRepo.findOne(id); - topic.ifPresent(t -> - t.getTopics().forEach(template::fetch) - ); + public Topic getTopicById(final Long id) { + final Topic topic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id)); + topic.getTopics().forEach(template::fetch); + return topic; } @SuppressWarnings("unchecked") @Override - public Set getAllTopics() { - return topicRepo.findAll().as(Set.class); + public List getAllTopics() { + return topicRepo.findAll().as(List.class); } @Override @@ -72,11 +70,9 @@ public void deleteTopic(final Long id) { @Override public Topic createTopic(final Topic topic, final Long id) { - Optional parentTopic = topicRepo.findOne(id); - parentTopic.ifPresent(t -> { - t.getTopics().add(topic); - topicRepo.save(t); - }); + final Topic parentTopic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id)); + parentTopic.getTopics().add(topic); + topicRepo.save(parentTopic); return topic; } diff --git a/src/main/java/com/epam/eighty/web/api/CustomerController.java b/src/main/java/com/epam/eighty/web/api/CustomerController.java index 4e3e76d..db7d627 100644 --- a/src/main/java/com/epam/eighty/web/api/CustomerController.java +++ b/src/main/java/com/epam/eighty/web/api/CustomerController.java @@ -40,7 +40,7 @@ public class CustomerController { @ResponseBody @Cacheable(value = "customer", key = "#customerName") public List getSortedSetOfCustomersByName(@ApiParam(name = "customerName", required = true, value = "sorted set of customers by part of customer name") @PathVariable("customerName") final String customerName) { - return customerService.getSortedSetOfCustomersByName(customerName); + return customerService.getSortedCustomersMatchingName(customerName); } @ApiOperation(value = "Find all customers", notes = "Get all customers", httpMethod = "GET", response = Customer.class, produces = "application/json") diff --git a/src/main/java/com/epam/eighty/web/api/QuestionController.java b/src/main/java/com/epam/eighty/web/api/QuestionController.java index 044affd..2a16b4a 100644 --- a/src/main/java/com/epam/eighty/web/api/QuestionController.java +++ b/src/main/java/com/epam/eighty/web/api/QuestionController.java @@ -1,14 +1,8 @@ package com.epam.eighty.web.api; -import com.codahale.metrics.annotation.Timed; -import com.epam.eighty.domain.Question; -import com.epam.eighty.exception.QuestionNotFoundException; -import com.epam.eighty.service.QuestionService; -import com.wordnik.swagger.annotations.Api; -import com.wordnik.swagger.annotations.ApiOperation; -import com.wordnik.swagger.annotations.ApiResponse; -import com.wordnik.swagger.annotations.ApiResponses; -import com.wordnik.swagger.annotations.ApiParam; +import java.util.List; + +import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; @@ -17,16 +11,20 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.PathVariable; - -import java.util.List; -import java.util.Set; -import javax.servlet.http.HttpServletResponse; +import com.codahale.metrics.annotation.Timed; +import com.epam.eighty.domain.Question; +import com.epam.eighty.service.QuestionService; +import com.wordnik.swagger.annotations.Api; +import com.wordnik.swagger.annotations.ApiOperation; +import com.wordnik.swagger.annotations.ApiParam; +import com.wordnik.swagger.annotations.ApiResponse; +import com.wordnik.swagger.annotations.ApiResponses; /** * @author Aliaksandr_Padalka @@ -71,7 +69,7 @@ public Question updateQuestion(@ApiParam(name = "question", required = true, val @ResponseBody @Cacheable(value = "question", key = "#id") public Question getQuestion(@ApiParam(name = "questionId", required = true, value = "question id") @PathVariable("id") final Long id) { - return questionService.getQuestionById(id).orElseThrow(() -> new QuestionNotFoundException(id)); + return questionService.getQuestionById(id); } @ApiOperation(value = "Create new question", notes = "Create new question", httpMethod = "POST", response = Question.class, produces = "application/json") @@ -117,7 +115,7 @@ public void deleteQuestion(@ApiParam(name = "questionId", required = true, value public List getAllQuestionsForTopic(@ApiParam(name = "topicId", required = true, value = "topic id") @PathVariable final Long id, @ApiParam(name = "page", required = false, value = "page (@PageableDefault, Pageable)") @PageableDefault(page = 0, size = DEFAULT_PAGE_SIZE, sort = DEFAULT_SORTING) final Pageable p) { - return questionService.getQuestionsPage(id, p); + return questionService.getQuestionsByTopicId(id, p); } @ApiOperation(value = "Find questions for topic and tag", notes = "Get all questions from topic and tag", httpMethod = "GET", produces = "application/json") @@ -128,7 +126,7 @@ public List getAllQuestionsForTopic(@ApiParam(name = "topicId", requir @Cacheable(value = "question", key = "'topic.' + #id +'.tag.' + #tag") public List getAllQuestionsForTopicWithTag(@ApiParam(name = "topicId", required = true, value = "topic id") @PathVariable final Long id, @ApiParam(name = "tag", required = true, value = "tag's value") @PathVariable final String tag) { - return questionService.getQuestionsByTopicAndTag(id, findDOT(tag)); + return questionService.getQuestionsByTopicIdAndTag(id, findDOT(tag)); } @ApiOperation(value = "Find all questions", notes = "Get all questions", httpMethod = "GET", produces = "application/json") @@ -137,7 +135,7 @@ public List getAllQuestionsForTopicWithTag(@ApiParam(name = "topicId", @RequestMapping(method = RequestMethod.GET) @ResponseBody @Cacheable(value = "question", key = "'all'") - public Set getAllQuestions() { + public List getAllQuestions() { return questionService.getAllQuestions(); } @@ -158,7 +156,7 @@ public List getAllQuestionsByTag(@ApiParam(name = "tag", required = tr @ResponseBody @Cacheable(value = "question", key = "'all.customer.' + #customer") public List getAllQuestionsByCustomer(@ApiParam(name = "customer", required = true, value = "customer's value") @PathVariable final String customer) { - return questionService.getQuestionsByCustomer(findDOT(customer)); + return questionService.getQuestionsByCustomerName(findDOT(customer)); } private String findDOT(final String string) { diff --git a/src/main/java/com/epam/eighty/web/api/TagController.java b/src/main/java/com/epam/eighty/web/api/TagController.java index fec657f..1992003 100644 --- a/src/main/java/com/epam/eighty/web/api/TagController.java +++ b/src/main/java/com/epam/eighty/web/api/TagController.java @@ -75,7 +75,7 @@ public List getAllTags() { @ResponseBody @Cacheable(value = "tag", key = "'topic.' + #tagName") public List getSortedSetOfTagsByName(@ApiParam(name = "tag", required = true, value = "sorted set of tags by part of tag name") @PathVariable("tagName") final String tagName) { - return tagService.getSortedSetOfTagsByName(tagName); + return tagService.getSortedTagsMatchingName(tagName); } @ApiOperation(value = "Find top tags", notes = "get top tags", httpMethod = "GET", response = Tag.class, produces = "application/json") diff --git a/src/main/java/com/epam/eighty/web/api/TopicController.java b/src/main/java/com/epam/eighty/web/api/TopicController.java index 941399c..74fc386 100644 --- a/src/main/java/com/epam/eighty/web/api/TopicController.java +++ b/src/main/java/com/epam/eighty/web/api/TopicController.java @@ -52,7 +52,7 @@ public class TopicController { @ResponseBody @Cacheable(value = "topic", key = "#id") public Topic getTopic(@ApiParam(name = "topicId", required = true, value = "topic id") @PathVariable("id") final Long id) { - return topicService.getTopicById(id).orElseThrow(() -> new TopicNotFoundException(id)); + return topicService.getTopicById(id); } @ApiOperation(value = "Find topic by id with questions", notes = "Get topic by id with questions", httpMethod = "GET", response = Topic.class, produces = "application/json") @@ -65,7 +65,7 @@ public Topic getTopic(@ApiParam(name = "topicId", required = true, value = "topi @ResponseBody @Cacheable(value = "topic", key = "'full.' + #id") public Topic getFullTopic(@ApiParam(name = "topicId", required = true, value = "topic id") @PathVariable("id") final Long id) { - return topicService.getFullTopicById(id).orElseThrow(() -> new TopicNotFoundException(id)); + return topicService.getFullTopicById(id); } @ApiOperation(value = "Find root topic", notes = "Get root topic", httpMethod = "GET", response = Topic.class, produces = "application/json") diff --git a/src/test/java/com/epam/eighty/repository/TagRepossitoryTest.java b/src/test/java/com/epam/eighty/repository/TagRepossitoryTest.java index 5e13946..8cb88a3 100644 --- a/src/test/java/com/epam/eighty/repository/TagRepossitoryTest.java +++ b/src/test/java/com/epam/eighty/repository/TagRepossitoryTest.java @@ -92,7 +92,7 @@ public void test_RemoveTagsWithoutQuestions() { @Test public void test_getSortedSetOfTagsByName() { - List tags = tagRepo.getSortedListOfTagsByName("o"); + List tags = tagRepo.getSortedTagsMatchingName("o"); assertEquals(4, tags.size()); assertEquals("double", tags.get(0).getTag()); @@ -100,7 +100,7 @@ public void test_getSortedSetOfTagsByName() { assertEquals("object", tags.get(2).getTag()); assertEquals("singleton", tags.get(3).getTag()); - tags = tagRepo.getSortedListOfTagsByName("fake tag"); + tags = tagRepo.getSortedTagsMatchingName("fake tag"); assertEquals(0, tags.size()); } diff --git a/src/test/java/com/epam/eighty/service/CustomerServiceTest.java b/src/test/java/com/epam/eighty/service/CustomerServiceTest.java index 76bd095..1c6c0a9 100644 --- a/src/test/java/com/epam/eighty/service/CustomerServiceTest.java +++ b/src/test/java/com/epam/eighty/service/CustomerServiceTest.java @@ -89,7 +89,7 @@ public void test_getSortedSetOfCustomersByName() { customerRepository.getSortedCustomersMatchingName(Mockito.anyString())) .thenReturn(list); - List customers = customerService.getSortedSetOfCustomersByName(EMPTY_STRING); + List customers = customerService.getSortedCustomersMatchingName(EMPTY_STRING); assertNotNull(customers); assertFalse(customers.isEmpty()); diff --git a/src/test/java/com/epam/eighty/service/QuestionServiceTest.java b/src/test/java/com/epam/eighty/service/QuestionServiceTest.java index 39ff976..0374ad8 100644 --- a/src/test/java/com/epam/eighty/service/QuestionServiceTest.java +++ b/src/test/java/com/epam/eighty/service/QuestionServiceTest.java @@ -102,10 +102,10 @@ public void setUp() { public void test_getAllQuestions() { when(questionRepo.findAll()).thenReturn(results); - Set set = questionService.getAllQuestions(); + List questions = questionService.getAllQuestions(); - assertNotNull(set); - assertEquals(set, fakes); + assertNotNull(questions); + assertEquals(questions, fakes); } @Test @@ -131,7 +131,7 @@ public void test_deleteQuestion() { public void test_getQuestionById() { when(questionRepo.findOne(fake.get().getId())).thenReturn(fake); - Question question = questionService.getQuestionById(1L).get(); + Question question = questionService.getQuestionById(1L); assertNotNull(question); assertEquals(question, fake.get()); @@ -141,7 +141,7 @@ public void test_getQuestionById() { public void test_getQuestionsPage() { when(questionRepo.getQuestionsByTopicId(root.get().getId(), null)).thenReturn(slice); - List questions = questionService.getQuestionsPage(root.get().getId(), null); + List questions = questionService.getQuestionsByTopicId(root.get().getId(), null); assertNotNull(questions); assertEquals(questions, list); @@ -152,7 +152,7 @@ public void test_getQuestionsPage() { public void test_getQuestionsByTopicAndTag() { when(questionRepo.getQuestionsByTopicIdAndTag(root.get().getId(), tag.getTag())).thenReturn(list); - List questions = questionService.getQuestionsByTopicAndTag(root.get().getId(), tag.getTag()); + List questions = questionService.getQuestionsByTopicIdAndTag(root.get().getId(), tag.getTag()); assertNotNull(questions); assertEquals(questions, list); @@ -172,7 +172,7 @@ public void test_getQuestionsByTag() { public void test_getQuestionsByCustomer() { when(questionRepo.getQuestionsByCustomerName(customer.getName())).thenReturn(list); - List questions = questionService.getQuestionsByCustomer(customer.getName()); + List questions = questionService.getQuestionsByCustomerName(customer.getName()); assertNotNull(questions); assertEquals(questions, list); diff --git a/src/test/java/com/epam/eighty/service/TagServiceTest.java b/src/test/java/com/epam/eighty/service/TagServiceTest.java index 2dd6a23..3240e92 100644 --- a/src/test/java/com/epam/eighty/service/TagServiceTest.java +++ b/src/test/java/com/epam/eighty/service/TagServiceTest.java @@ -128,9 +128,9 @@ public void test_getTopNFromAllTags() { @Test public void test_getSortedSetOfTagsByName() { - when(tagRepo.getSortedListOfTagsByName(ANY_SYMBOL + fake.get().getTag()+ ANY_SYMBOL)).thenReturn(list); + when(tagRepo.getSortedTagsMatchingName(ANY_SYMBOL + fake.get().getTag()+ ANY_SYMBOL)).thenReturn(list); - List set = tagService.getSortedSetOfTagsByName(fake.get().getTag()); + List set = tagService.getSortedTagsMatchingName(fake.get().getTag()); assertNotNull(set); assertEquals(set, tags); diff --git a/src/test/java/com/epam/eighty/service/TopicServiceTest.java b/src/test/java/com/epam/eighty/service/TopicServiceTest.java index 7775ea8..a5b1d4c 100644 --- a/src/test/java/com/epam/eighty/service/TopicServiceTest.java +++ b/src/test/java/com/epam/eighty/service/TopicServiceTest.java @@ -87,10 +87,10 @@ public void setUp() { public void test_getAllTopics() { when(topicRepo.findAll()).thenReturn(results); - Set set = topicService.getAllTopics(); + List topics = topicService.getAllTopics(); - assertNotNull(set); - assertEquals(set, fakes); + assertNotNull(topics); + assertEquals(topics, fakes); } @Test @@ -115,7 +115,7 @@ public void test_createTopic() { @Test public void test_getTopicById() { when(topicRepo.findOne(fake.get().getId())).thenReturn(fake); - Topic topic = topicService.getTopicById(fake.get().getId()).get(); + Topic topic = topicService.getTopicById(fake.get().getId()); assertNotNull(topic); assertEquals(topic, fake.get()); } @@ -123,7 +123,7 @@ public void test_getTopicById() { @Test public void test_getFullTopicById() { when(topicRepo.findOne(fake.get().getId())).thenReturn(fake); - Topic topic = topicService.getFullTopicById(fake.get().getId()).get(); + Topic topic = topicService.getFullTopicById(fake.get().getId()); assertNotNull(topic); assertEquals(topic, fake.get()); } diff --git a/src/test/java/com/epam/eighty/web/api/CustomerControllerTest.java b/src/test/java/com/epam/eighty/web/api/CustomerControllerTest.java index 420e243..2b89ea4 100644 --- a/src/test/java/com/epam/eighty/web/api/CustomerControllerTest.java +++ b/src/test/java/com/epam/eighty/web/api/CustomerControllerTest.java @@ -59,7 +59,7 @@ public void test_getAllCustomersByTopicId() { @Test public void test_getSortedSetOfCustomersByName() { - when(customerService.getSortedSetOfCustomersByName(TEST_STRING)).thenReturn(customerSet); + when(customerService.getSortedCustomersMatchingName(TEST_STRING)).thenReturn(customerSet); assertTrue(customerController.getSortedSetOfCustomersByName(TEST_STRING).equals(customerSet)); } diff --git a/src/test/java/com/epam/eighty/web/api/QuestionControllerTest.java b/src/test/java/com/epam/eighty/web/api/QuestionControllerTest.java index 2cd8df0..efedf70 100644 --- a/src/test/java/com/epam/eighty/web/api/QuestionControllerTest.java +++ b/src/test/java/com/epam/eighty/web/api/QuestionControllerTest.java @@ -5,7 +5,6 @@ import static org.mockito.Mockito.when; import java.util.Collections; -import java.util.Optional; import javax.servlet.http.HttpServletResponse; @@ -18,7 +17,6 @@ import org.springframework.data.domain.Pageable; import com.epam.eighty.domain.Question; -import com.epam.eighty.exception.QuestionNotFoundException; import com.epam.eighty.service.QuestionService; /** @@ -67,16 +65,16 @@ public void test_deleteQuestion_successful_case() { @Test public void test_getAllQuestionsForTopic() { - when(questionService.getQuestionsPage(TEST_LONG, pageable)).thenReturn(Collections. emptyList()); + when(questionService.getQuestionsByTopicId(TEST_LONG, pageable)).thenReturn(Collections. emptyList()); questionController.getAllQuestionsForTopic(TEST_LONG, pageable); - verify(questionService, Mockito.times(1)).getQuestionsPage(1l, pageable); + verify(questionService, Mockito.times(1)).getQuestionsByTopicId(1l, pageable); } @Test public void test_getAllQuestionsForTopicWithTag() { - when(questionService.getQuestionsByTopicAndTag(TEST_LONG, TEST_STRING)).thenReturn(Collections. emptyList()); + when(questionService.getQuestionsByTopicIdAndTag(TEST_LONG, TEST_STRING)).thenReturn(Collections. emptyList()); questionController.getAllQuestionsForTopicWithTag(TEST_LONG, TEST_STRING); - verify(questionService, Mockito.times(1)).getQuestionsByTopicAndTag(1L, "fake"); + verify(questionService, Mockito.times(1)).getQuestionsByTopicIdAndTag(1L, "fake"); } @Test @@ -94,25 +92,17 @@ public void test_getAllQuestionsByTag() { @Test public void test_getAllQuestionsByCustomer() { - when(questionService.getQuestionsByCustomer("fake.fake")).thenReturn(Collections. emptyList()); + when(questionService.getQuestionsByCustomerName("fake.fake")).thenReturn(Collections. emptyList()); questionController.getAllQuestionsByCustomer("fake|fake"); - verify(questionService, Mockito.times(1)).getQuestionsByCustomer("fake.fake"); + verify(questionService, Mockito.times(1)).getQuestionsByCustomerName("fake.fake"); } @Test public void test_getQuestion_successful_case_with_existing_questuion() { - Optional optionalQuestion = Optional.ofNullable(question); - when(questionService.getQuestionById(TEST_LONG)).thenReturn(optionalQuestion); + when(questionService.getQuestionById(TEST_LONG)).thenReturn(question); assertTrue(questionController.getQuestion(TEST_LONG).equals(question)); } - @Test(expected = QuestionNotFoundException.class) - public void test_getQuestion_successful_case_with_not_existing_questuion() { - Optional optionalQuestion = Optional.empty(); - when(questionService.getQuestionById(TEST_LONG)).thenReturn(optionalQuestion); - questionController.getQuestion(TEST_LONG); - } - @Test public void test_updateQuestion_successful_case() { questionController.updateQuestion(question, response); diff --git a/src/test/java/com/epam/eighty/web/api/TagControllerTest.java b/src/test/java/com/epam/eighty/web/api/TagControllerTest.java index c372a57..245844d 100644 --- a/src/test/java/com/epam/eighty/web/api/TagControllerTest.java +++ b/src/test/java/com/epam/eighty/web/api/TagControllerTest.java @@ -72,7 +72,7 @@ public void test_getAllTagsByTopicId() { @Test public void test_getSortedSetOfTagsByName() { - when(tagService.getSortedSetOfTagsByName(TEST_STRING)).thenReturn(tagSet); + when(tagService.getSortedTagsMatchingName(TEST_STRING)).thenReturn(tagSet); assertTrue(tagController.getSortedSetOfTagsByName(TEST_STRING).equals(tagSet)); } diff --git a/src/test/java/com/epam/eighty/web/api/TopicControllerTest.java b/src/test/java/com/epam/eighty/web/api/TopicControllerTest.java index d7cf211..ec110b1 100644 --- a/src/test/java/com/epam/eighty/web/api/TopicControllerTest.java +++ b/src/test/java/com/epam/eighty/web/api/TopicControllerTest.java @@ -50,32 +50,16 @@ public void setUp() { @Test public void test_getTopic_successful_case_with_existing_topic() { - Optional optionalTopic = Optional.ofNullable(topic); - when(topicService.getTopicById(TEST_LONG)).thenReturn(optionalTopic); + when(topicService.getTopicById(TEST_LONG)).thenReturn(topic); assertTrue(topicController.getTopic(TEST_LONG).equals(topic)); -} - - @Test(expected = TopicNotFoundException.class) - public void test_getTopic_successful_case_with_not_existing_topic() { - Optional optionalTopic = Optional.empty(); - when(topicService.getTopicById(TEST_LONG)).thenReturn(optionalTopic); - topicController.getTopic(TEST_LONG); } @Test public void test_getFullTopic_successful_case_with_existing_topic() { - Optional optionalTopic = Optional.ofNullable(topic); - when(topicService.getFullTopicById(TEST_LONG)).thenReturn(optionalTopic); + when(topicService.getFullTopicById(TEST_LONG)).thenReturn(topic); assertTrue(topicController.getFullTopic(TEST_LONG).equals(topic)); } - @Test(expected = TopicNotFoundException.class) - public void test_getFullTopic_successful_case_with_not_existing_topic() { - Optional optionalTopic = Optional.empty(); - when(topicService.getFullTopicById(TEST_LONG)).thenReturn(optionalTopic); - topicController.getFullTopic(TEST_LONG); - } - @Test public void test_getRootTopic_successful_case_with_existing_root() throws IOException { Optional optionalTopic = Optional.ofNullable(topic); From aa0133808362a6436eebaae72d6333a10f665300 Mon Sep 17 00:00:00 2001 From: Roman Konovalov Date: Tue, 17 Mar 2015 18:39:09 +0300 Subject: [PATCH 2/5] Rename services methods --- .../com/epam/eighty/service/CustomerService.java | 2 +- .../java/com/epam/eighty/service/TagService.java | 2 +- .../eighty/service/impl/CustomerServiceImpl.java | 7 +++---- .../epam/eighty/service/impl/TagServiceImpl.java | 7 +++---- .../epam/eighty/service/impl/TopicServiceImpl.java | 4 +--- .../epam/eighty/web/api/CustomerController.java | 2 +- .../com/epam/eighty/web/api/TagController.java | 2 +- .../epam/eighty/service/CustomerServiceTest.java | 14 ++++++-------- .../com/epam/eighty/service/TagServiceTest.java | 2 +- .../eighty/web/api/CustomerControllerTest.java | 2 +- .../com/epam/eighty/web/api/TagControllerTest.java | 2 +- 11 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/epam/eighty/service/CustomerService.java b/src/main/java/com/epam/eighty/service/CustomerService.java index dfb1ca9..9aeeb6e 100644 --- a/src/main/java/com/epam/eighty/service/CustomerService.java +++ b/src/main/java/com/epam/eighty/service/CustomerService.java @@ -17,7 +17,7 @@ public interface CustomerService { */ List getAllCustomers(); - List getSortedCustomersMatchingName(String customerName); + List getCustomersMatchingName(String customerName); /** * Retrieves a list of customer {@link com.epam.eighty.domain.Customer} entities whose questions present in topic with given id . diff --git a/src/main/java/com/epam/eighty/service/TagService.java b/src/main/java/com/epam/eighty/service/TagService.java index 00e9953..2913f61 100644 --- a/src/main/java/com/epam/eighty/service/TagService.java +++ b/src/main/java/com/epam/eighty/service/TagService.java @@ -12,5 +12,5 @@ public interface TagService { List getTagsByTopicId(Long topicId); List getAllTags(); List getTopNFromAllTags(Long limit); - List getSortedTagsMatchingName(String tagName); + List getTagsMatchingName(String tagName); } diff --git a/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java b/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java index 73c174a..58690ef 100644 --- a/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java +++ b/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java @@ -31,17 +31,16 @@ public List getAllCustomers() { } @Override - public List getSortedCustomersMatchingName(final String customerName) { + public List getCustomersMatchingName(final String customerName) { return customerRepository.getCustomersMatchingName(customerName); } @Override public List getCustomersByTopicId(final Long topicId) { - final List customerList = customerRepository.getCustomersByTopicId(topicId); + final List customerList = customerRepository.getCustomersByTopicId(topicId); customerList - .forEach(customer -> customer.setCountInTopic(customerRepository.getQuestionsNumberInTopicByCustomer(customer.getName(), - topicId))); + .forEach(customer -> customer.setCountInTopic(customerRepository.getQuestionsNumberInTopicByCustomer(customer.getName(), topicId))); return customerList; } diff --git a/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java b/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java index b2b5aed..0d50d73 100644 --- a/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java +++ b/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java @@ -22,9 +22,8 @@ public class TagServiceImpl implements TagService { @Override public List getTagsByTopicId(final Long topicId) { - final List tagsList = tagRepo.getTagsByTopicId(topicId); - tagsList - .forEach(tag -> tag.setCountInTopic(tagRepo.getQuestionsNumberInTopicByTag(tag.getTag(), topicId))); + final List tagsList = tagRepo.getTagsByTopicId(topicId); + tagsList.forEach(tag -> tag.setCountInTopic(tagRepo.getQuestionsNumberInTopicByTag(tag.getTag(), topicId))); return tagsList; } @@ -40,7 +39,7 @@ public List getTopNFromAllTags(final Long limit) { } @Override - public List getSortedTagsMatchingName(final String tagName) { + public List getTagsMatchingName(final String tagName) { return tagRepo.getTagsMatchingName(tagName); } diff --git a/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java b/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java index e6f573f..6b0c6a0 100644 --- a/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java +++ b/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java @@ -29,9 +29,7 @@ public class TopicServiceImpl implements TopicService { @Override public Optional getRoot() { final Optional root = topicRepo.findBySchemaPropertyValue("title", "root"); - root.ifPresent(someRoot -> - someRoot.getTopics().forEach(template::fetch) - ); + root.ifPresent(someRoot -> someRoot.getTopics().forEach(template::fetch)); return root; } diff --git a/src/main/java/com/epam/eighty/web/api/CustomerController.java b/src/main/java/com/epam/eighty/web/api/CustomerController.java index db7d627..6af76c7 100644 --- a/src/main/java/com/epam/eighty/web/api/CustomerController.java +++ b/src/main/java/com/epam/eighty/web/api/CustomerController.java @@ -40,7 +40,7 @@ public class CustomerController { @ResponseBody @Cacheable(value = "customer", key = "#customerName") public List getSortedSetOfCustomersByName(@ApiParam(name = "customerName", required = true, value = "sorted set of customers by part of customer name") @PathVariable("customerName") final String customerName) { - return customerService.getSortedCustomersMatchingName(customerName); + return customerService.getCustomersMatchingName(customerName); } @ApiOperation(value = "Find all customers", notes = "Get all customers", httpMethod = "GET", response = Customer.class, produces = "application/json") diff --git a/src/main/java/com/epam/eighty/web/api/TagController.java b/src/main/java/com/epam/eighty/web/api/TagController.java index 7aa1983..95737c1 100644 --- a/src/main/java/com/epam/eighty/web/api/TagController.java +++ b/src/main/java/com/epam/eighty/web/api/TagController.java @@ -63,7 +63,7 @@ public List getAllTags() { @ResponseBody @Cacheable(value = "tag", key = "'topic.' + #tagName") public List getSortedSetOfTagsByName(@ApiParam(name = "tag", required = true, value = "sorted set of tags by part of tag name") @PathVariable("tagName") final String tagName) { - return tagService.getSortedTagsMatchingName(tagName); + return tagService.getTagsMatchingName(tagName); } @ApiOperation(value = "Find top tags", notes = "get top tags", httpMethod = "GET", response = Tag.class, produces = "application/json") diff --git a/src/test/java/com/epam/eighty/service/CustomerServiceTest.java b/src/test/java/com/epam/eighty/service/CustomerServiceTest.java index 6b4023c..1682012 100644 --- a/src/test/java/com/epam/eighty/service/CustomerServiceTest.java +++ b/src/test/java/com/epam/eighty/service/CustomerServiceTest.java @@ -17,16 +17,16 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.powermock.modules.junit4.PowerMockRunner; +import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.neo4j.conversion.QueryResultBuilder; import org.springframework.data.neo4j.conversion.Result; -import com.epam.eighty.domain.Topic; import com.epam.eighty.domain.Customer; +import com.epam.eighty.domain.Topic; import com.epam.eighty.repository.CustomerRepository; import com.epam.eighty.service.impl.CustomerServiceImpl; -@RunWith(PowerMockRunner.class) +@RunWith(MockitoJUnitRunner.class) public class CustomerServiceTest { private static final String EMPTY_STRING = ""; @@ -85,12 +85,10 @@ public void setUp() { @Test public void test_getSortedSetOfCustomersByName() { - Mockito.when( - customerRepository.getCustomersMatchingName(Mockito.anyString())) - .thenReturn(list); + Mockito.when(customerRepository.getCustomersMatchingName(Mockito.anyString())).thenReturn(list); + + List customers = customerService.getCustomersMatchingName(EMPTY_STRING); - List customers = customerService.getSortedCustomersMatchingName(EMPTY_STRING); - assertNotNull(customers); assertFalse(customers.isEmpty()); } diff --git a/src/test/java/com/epam/eighty/service/TagServiceTest.java b/src/test/java/com/epam/eighty/service/TagServiceTest.java index 331a737..3763576 100644 --- a/src/test/java/com/epam/eighty/service/TagServiceTest.java +++ b/src/test/java/com/epam/eighty/service/TagServiceTest.java @@ -109,7 +109,7 @@ public void test_getTopNFromAllTags() { public void test_getSortedSetOfTagsByName() { when(tagRepo.getTagsMatchingName(ANY_SYMBOL + fake.get().getTag()+ ANY_SYMBOL)).thenReturn(list); - List set = tagService.getSortedTagsMatchingName(fake.get().getTag()); + List set = tagService.getTagsMatchingName(fake.get().getTag()); assertNotNull(set); assertEquals(set, tags); diff --git a/src/test/java/com/epam/eighty/web/api/CustomerControllerTest.java b/src/test/java/com/epam/eighty/web/api/CustomerControllerTest.java index 2b89ea4..68929de 100644 --- a/src/test/java/com/epam/eighty/web/api/CustomerControllerTest.java +++ b/src/test/java/com/epam/eighty/web/api/CustomerControllerTest.java @@ -59,7 +59,7 @@ public void test_getAllCustomersByTopicId() { @Test public void test_getSortedSetOfCustomersByName() { - when(customerService.getSortedCustomersMatchingName(TEST_STRING)).thenReturn(customerSet); + when(customerService.getCustomersMatchingName(TEST_STRING)).thenReturn(customerSet); assertTrue(customerController.getSortedSetOfCustomersByName(TEST_STRING).equals(customerSet)); } diff --git a/src/test/java/com/epam/eighty/web/api/TagControllerTest.java b/src/test/java/com/epam/eighty/web/api/TagControllerTest.java index 28f3a6a..c27d946 100644 --- a/src/test/java/com/epam/eighty/web/api/TagControllerTest.java +++ b/src/test/java/com/epam/eighty/web/api/TagControllerTest.java @@ -66,7 +66,7 @@ public void test_getAllTagsByTopicId() { @Test public void test_getSortedSetOfTagsByName() { - when(tagService.getSortedTagsMatchingName(TEST_STRING)).thenReturn(tagSet); + when(tagService.getTagsMatchingName(TEST_STRING)).thenReturn(tagSet); assertTrue(tagController.getSortedSetOfTagsByName(TEST_STRING).equals(tagSet)); } From c93b8e0dc03287c42266eb0150312fa86df39716 Mon Sep 17 00:00:00 2001 From: Roman Konovalov Date: Wed, 18 Mar 2015 11:58:28 +0300 Subject: [PATCH 3/5] Refactor services unit tests --- pom.xml | 6 + .../eighty/service/CustomerServiceTest.java | 88 ++++------ .../eighty/service/QuestionServiceTest.java | 164 ++++++++---------- .../epam/eighty/service/TagServiceTest.java | 77 ++++---- .../epam/eighty/service/TopicServiceTest.java | 136 ++++++++------- 5 files changed, 224 insertions(+), 247 deletions(-) diff --git a/pom.xml b/pom.xml index d63cac6..bdd2d68 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,12 @@ ${slf4j.version} + + org.hamcrest + hamcrest-all + 1.3 + test + junit junit diff --git a/src/test/java/com/epam/eighty/service/CustomerServiceTest.java b/src/test/java/com/epam/eighty/service/CustomerServiceTest.java index 1682012..967a38c 100644 --- a/src/test/java/com/epam/eighty/service/CustomerServiceTest.java +++ b/src/test/java/com/epam/eighty/service/CustomerServiceTest.java @@ -1,14 +1,10 @@ package com.epam.eighty.service; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.when; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.junit.Before; import org.junit.Test; @@ -28,14 +24,12 @@ @RunWith(MockitoJUnitRunner.class) public class CustomerServiceTest { + + private static final long QUESTIONS_NUMBER = 9999L; - private static final String EMPTY_STRING = ""; - - private Customer fake; private Topic root; - private List list; - private List fakeList; - private Result fakeResult; + private List customers; + private Result results; @InjectMocks private CustomerServiceImpl customerService; @@ -43,73 +37,63 @@ public class CustomerServiceTest { @Mock private CustomerRepository customerRepository; - private Set customers; - @Before public void setUp() { MockitoAnnotations.initMocks(this); - fake = new Customer(); - fake.setId(10L); - fake.setName("fake"); - - Customer fake1 = new Customer(); - fake1.setId(1001L); - fake1.setName("fake1"); - fake1.setCount(1L); - Customer fake2 = new Customer(); - fake2.setId(1002L); - fake2.setName("fake2"); - fake2.setCount(1L); - Customer fake3 = new Customer(); - fake3.setId(1003L); - fake3.setName("fake3"); - - customers = new HashSet<>(); - customers.add(fake1); - customers.add(fake2); + + Customer customer1 = new Customer(); + customer1.setId(1001L); + customer1.setName("fake1"); + customer1.setCount(1L); + Customer customer2 = new Customer(); + customer2.setId(1002L); + customer2.setName("fake2"); + customer2.setCount(1L); + Customer customer3 = new Customer(); + customer3.setId(1003L); + customer3.setName("fake3"); root = new Topic(); root.setId(0L); - list = new ArrayList<>(); - list.add(fake1); - list.add(fake2); + customers = new ArrayList<>(); + customers.add(customer1); + customers.add(customer2); - fakeList = new ArrayList<>(); - fakeList.add(fake1); - fakeList.add(fake2); - fakeList.add(fake3); + List fakeCustomers = new ArrayList<>(); + fakeCustomers.add(customer1); + fakeCustomers.add(customer2); + fakeCustomers.add(customer3); - fakeResult = new QueryResultBuilder<>(fakeList); + results = new QueryResultBuilder<>(fakeCustomers); } @Test - public void test_getSortedSetOfCustomersByName() { - Mockito.when(customerRepository.getCustomersMatchingName(Mockito.anyString())).thenReturn(list); + public void test_getCustomersMatchingName() { + Mockito.when(customerRepository.getCustomersMatchingName(Mockito.anyString())).thenReturn(customers); - List customers = customerService.getCustomersMatchingName(EMPTY_STRING); + List actualCustomers = customerService.getCustomersMatchingName(Mockito.anyString()); - assertNotNull(customers); - assertFalse(customers.isEmpty()); + assertEquals(actualCustomers, customers); } @Test public void test_getCustomersByTopicId() { - when(customerRepository.getCustomersByTopicId(root.getId())).thenReturn(list); + when(customerRepository.getCustomersByTopicId(root.getId())).thenReturn(customers); + when(customerRepository.getQuestionsNumberInTopicByCustomer(Mockito.anyString(), Mockito.anyLong())).thenReturn(QUESTIONS_NUMBER); - List tagList = customerService.getCustomersByTopicId(root.getId()); + List actualCustomers = customerService.getCustomersByTopicId(root.getId()); - assertNotNull(tagList); - assertEquals(tagList, list); + assertEquals(actualCustomers, customers); + actualCustomers.forEach(customer -> assertEquals(QUESTIONS_NUMBER, customer.getCountInTopic().longValue())); } @Test public void test_getAllCustomers() { - when(customerRepository.findAll()).thenReturn(fakeResult); + when(customerRepository.findAll()).thenReturn(results); - List customersList = customerService.getAllCustomers(); + List actualCustomers = customerService.getAllCustomers(); - assertNotNull(customersList); - assertEquals(customersList, list); + assertEquals(actualCustomers, customers); } } diff --git a/src/test/java/com/epam/eighty/service/QuestionServiceTest.java b/src/test/java/com/epam/eighty/service/QuestionServiceTest.java index 0374ad8..d47cc79 100644 --- a/src/test/java/com/epam/eighty/service/QuestionServiceTest.java +++ b/src/test/java/com/epam/eighty/service/QuestionServiceTest.java @@ -1,50 +1,48 @@ package com.epam.eighty.service; -import com.epam.eighty.domain.Customer; -import com.epam.eighty.domain.Question; -import com.epam.eighty.domain.Tag; -import com.epam.eighty.domain.Topic; -import com.epam.eighty.repository.QuestionRepository; -import com.epam.eighty.repository.TopicRepository; -import com.epam.eighty.service.impl.QuestionServiceImpl; +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Optional; -import java.util.Set; - -import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; - -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.domain.Slice; import org.springframework.data.domain.SliceImpl; import org.springframework.data.neo4j.conversion.QueryResultBuilder; import org.springframework.data.neo4j.conversion.Result; +import com.epam.eighty.domain.Customer; +import com.epam.eighty.domain.Question; +import com.epam.eighty.domain.Tag; +import com.epam.eighty.domain.Topic; +import com.epam.eighty.exception.TopicNotFoundException; +import com.epam.eighty.repository.QuestionRepository; +import com.epam.eighty.repository.TopicRepository; +import com.epam.eighty.service.impl.QuestionServiceImpl; + /** * @author Aliaksandr_Padalka */ @RunWith(MockitoJUnitRunner.class) public class QuestionServiceTest { + + private static final long TOPIC_ID = 5L; + private static final String TAG_NAME = "tag name"; - private Optional fake; - private Optional root; - private Tag tag; - private Customer customer; + private Question question; + private Topic root; - private Set fakes; private Result results; - List list; + private List questions; private Slice slice; @Mock @@ -56,126 +54,116 @@ public class QuestionServiceTest { @Before public void setUp() { - root = Optional.of(new Topic()); - root.get().setId(0L); + root = new Topic(); + root.setId(0L); - tag = new Tag(); + Tag tag = new Tag(); tag.setId(100L); tag.setTag("tag"); - customer = new Customer(); + Customer customer = new Customer(); customer.setId(200L); customer.setName("customer"); - fake = Optional.of(new Question()); - fake.get().setId(1L); - fake.get().setQuestion("fake question"); - - Question fake0 = new Question(); - fake0.setId(10L); - fake0.setQuestion("fake question 0"); + question = new Question(); + question.setId(1L); + question.setQuestion("fake question"); - Question fake1 = new Question(); - fake1.setId(11L); - fake1.setQuestion("fake question 1"); + Question question0 = new Question(); + question0.setId(10L); + question0.setQuestion("fake question 0"); - Question fake2 = new Question(); - fake2.setId(12L); - fake2.setQuestion("fake question 2"); + Question question1 = new Question(); + question1.setId(11L); + question1.setQuestion("fake question 1"); - fakes = new HashSet<>(); + Question question2 = new Question(); + question2.setId(12L); + question2.setQuestion("fake question 2"); - fakes.add(fake0); - fakes.add(fake1); - fakes.add(fake2); + questions = new ArrayList<>(); + questions.add(question0); + questions.add(question1); + questions.add(question2); - list = new ArrayList<>(); - list.add(fake0); - list.add(fake1); - list.add(fake2); - - results = new QueryResultBuilder<>(fakes); - slice = new SliceImpl<>(list); + results = new QueryResultBuilder<>(questions); + slice = new SliceImpl<>(questions); } @Test public void test_getAllQuestions() { when(questionRepo.findAll()).thenReturn(results); - List questions = questionService.getAllQuestions(); + List actualQuestions = questionService.getAllQuestions(); - assertNotNull(questions); - assertEquals(questions, fakes); + assertThat(actualQuestions, contains(questions.toArray())); } @Test public void test_addQuestion() { - when(topicRepo.findOne(root.get().getId())).thenReturn(root); - questionService.addQuestion(fake.get(), root.get().getId()); - verify(questionRepo).save(fake.get()); + when(topicRepo.findOne(root.getId())).thenReturn(Optional.of(root)); + + questionService.addQuestion(question, root.getId()); + + verify(questionRepo).save(question); + verify(topicRepo).save(root); + assertThat(root.getQuestions(), contains(question)); } - @Test - public void test_updateQuestion() { - questionService.updateQuestion(fake.get()); - verify(questionRepo).save(fake.get()); + @Test(expected = TopicNotFoundException.class) + public void test_addQuestionNegative() { + when(topicRepo.findOne(root.getId())).thenReturn(Optional.empty()); + + questionService.addQuestion(question, root.getId()); } @Test - public void test_deleteQuestion() { - questionService.deleteQuestion(fake.get().getId()); - verify(questionRepo).delete(fake.get().getId()); + public void test_updateQuestion() { + questionService.updateQuestion(question); + verify(questionRepo).save(question); } @Test - public void test_getQuestionById() { - when(questionRepo.findOne(fake.get().getId())).thenReturn(fake); - - Question question = questionService.getQuestionById(1L); - - assertNotNull(question); - assertEquals(question, fake.get()); + public void test_deleteQuestion() { + questionService.deleteQuestion(question.getId()); + + verify(questionRepo).delete(question.getId()); } @Test - public void test_getQuestionsPage() { - when(questionRepo.getQuestionsByTopicId(root.get().getId(), null)).thenReturn(slice); + public void test_getQuestionsByTopicId() { + when(questionRepo.getQuestionsByTopicId(TOPIC_ID, null)).thenReturn(slice); - List questions = questionService.getQuestionsByTopicId(root.get().getId(), null); + List actualQuestions = questionService.getQuestionsByTopicId(TOPIC_ID, null); - assertNotNull(questions); - assertEquals(questions, list); + assertThat(actualQuestions, contains(questions.toArray())); } - @Test - public void test_getQuestionsByTopicAndTag() { - when(questionRepo.getQuestionsByTopicIdAndTag(root.get().getId(), tag.getTag())).thenReturn(list); + public void test_getQuestionsByTopicIdAndTag() { + when(questionRepo.getQuestionsByTopicIdAndTag(TOPIC_ID, TAG_NAME)).thenReturn(questions); - List questions = questionService.getQuestionsByTopicIdAndTag(root.get().getId(), tag.getTag()); + List actualQuestions = questionService.getQuestionsByTopicIdAndTag(TOPIC_ID, TAG_NAME); - assertNotNull(questions); - assertEquals(questions, list); + assertThat(actualQuestions, contains(questions.toArray())); } @Test public void test_getQuestionsByTag() { - when(questionRepo.getQuestionsByTag(tag.getTag())).thenReturn(list); + when(questionRepo.getQuestionsByTag(TAG_NAME)).thenReturn(questions); - List questions = questionService.getQuestionsByTag(tag.getTag()); + List actualQuestions = questionService.getQuestionsByTag(TAG_NAME); - assertNotNull(questions); - assertEquals(questions, list); + assertThat(actualQuestions, contains(questions.toArray())); } @Test public void test_getQuestionsByCustomer() { - when(questionRepo.getQuestionsByCustomerName(customer.getName())).thenReturn(list); + when(questionRepo.getQuestionsByCustomerName(TAG_NAME)).thenReturn(questions); - List questions = questionService.getQuestionsByCustomerName(customer.getName()); + List actualQuestions = questionService.getQuestionsByCustomerName(TAG_NAME); - assertNotNull(questions); - assertEquals(questions, list); + assertThat(actualQuestions, contains(questions.toArray())); } } diff --git a/src/test/java/com/epam/eighty/service/TagServiceTest.java b/src/test/java/com/epam/eighty/service/TagServiceTest.java index 3763576..9c877da 100644 --- a/src/test/java/com/epam/eighty/service/TagServiceTest.java +++ b/src/test/java/com/epam/eighty/service/TagServiceTest.java @@ -1,18 +1,19 @@ package com.epam.eighty.service; +import static org.hamcrest.Matchers.contains; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.data.neo4j.conversion.QueryResultBuilder; import org.springframework.data.neo4j.conversion.Result; @@ -28,14 +29,13 @@ @RunWith(MockitoJUnitRunner.class) public class TagServiceTest { + private static final long QUESTIONS_NUMBER = 9999L; private static final long LIMIT = 5L; - private static final String ANY_SYMBOL = ".*"; + private static final String TAG_NAME = "tag name"; - private Optional fake; private Result results; private List tags; private Topic root; - private List list; @Mock private TagRepository tagRepo; @@ -44,76 +44,63 @@ public class TagServiceTest { @Before public void setUp() { - fake = Optional.of(new Tag()); - fake.get().setId(10L); - fake.get().setTag("fake"); - - Tag fake1 = new Tag(); - fake1.setId(1001L); - fake1.setTag("fake1"); - Tag fake2 = new Tag(); - fake2.setId(1002L); - fake2.setTag("fake2"); - Tag fake3 = new Tag(); - fake3.setId(1003L); - fake3.setTag("fake3"); + Tag tag1 = new Tag(); + tag1.setId(1001L); + tag1.setTag("fake1"); + Tag tag2 = new Tag(); + tag2.setId(1002L); + tag2.setTag("fake2"); + Tag tag3 = new Tag(); + tag3.setId(1003L); + tag3.setTag("fake3"); tags = new ArrayList<>(); - tags.add(fake1); - tags.add(fake2); - tags.add(fake3); + tags.add(tag1); + tags.add(tag2); + tags.add(tag3); root = new Topic(); root.setId(0L); - list = new ArrayList<>(); - list.add(fake1); - list.add(fake2); - list.add(fake3); - results = new QueryResultBuilder<>(tags); - } @Test - public void test_findAll() { + public void test_getAllTags() { when(tagRepo.findAll()).thenReturn(results); - List set = tagService.getAllTags(); + List actualTags = tagService.getAllTags(); - assertNotNull(set); - assertEquals(set, tags); + assertThat(actualTags, contains(tags.toArray())); } @Test public void test_getTagsByTopicId() { - when(tagRepo.getTagsByTopicId(root.getId())).thenReturn(list); + when(tagRepo.getTagsByTopicId(root.getId())).thenReturn(tags); + when(tagRepo.getQuestionsNumberInTopicByTag(Mockito.anyString(), Mockito.anyLong())).thenReturn(QUESTIONS_NUMBER); - List tagList = tagService.getTagsByTopicId(root.getId()); + List actualTags = tagService.getTagsByTopicId(root.getId()); - assertNotNull(tagList); - assertEquals(tagList, list); + actualTags.forEach(tag -> assertEquals(QUESTIONS_NUMBER, tag.getCountInTopic().longValue())); + assertThat(actualTags, contains(tags.toArray())); } @Test public void test_getTopNFromAllTags() { - when(tagRepo.getTopNFromAllTags(LIMIT)).thenReturn(list); + when(tagRepo.getTopNFromAllTags(LIMIT)).thenReturn(tags); - List tagList = tagService.getTopNFromAllTags(LIMIT); + List actualTags = tagService.getTopNFromAllTags(LIMIT); - assertNotNull(tagList); - assertEquals(tagList, list); + assertThat(actualTags, contains(tags.toArray())); } @Test - public void test_getSortedSetOfTagsByName() { - when(tagRepo.getTagsMatchingName(ANY_SYMBOL + fake.get().getTag()+ ANY_SYMBOL)).thenReturn(list); + public void test_getTagsMatchingName() { + when(tagRepo.getTagsMatchingName(TAG_NAME)).thenReturn(tags); - List set = tagService.getTagsMatchingName(fake.get().getTag()); + List actualTags = tagService.getTagsMatchingName(TAG_NAME); - assertNotNull(set); - assertEquals(set, tags); + assertThat(actualTags, contains(tags.toArray())); } - } diff --git a/src/test/java/com/epam/eighty/service/TopicServiceTest.java b/src/test/java/com/epam/eighty/service/TopicServiceTest.java index ee4213b..b5549df 100644 --- a/src/test/java/com/epam/eighty/service/TopicServiceTest.java +++ b/src/test/java/com/epam/eighty/service/TopicServiceTest.java @@ -1,8 +1,14 @@ package com.epam.eighty.service; -import com.epam.eighty.domain.Topic; -import com.epam.eighty.repository.TopicRepository; -import com.epam.eighty.service.impl.TopicServiceImpl; +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -14,12 +20,10 @@ import org.springframework.data.neo4j.conversion.Result; import org.springframework.data.neo4j.template.Neo4jOperations; -import java.util.*; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import com.epam.eighty.domain.Topic; +import com.epam.eighty.exception.TopicNotFoundException; +import com.epam.eighty.repository.TopicRepository; +import com.epam.eighty.service.impl.TopicServiceImpl; /** * @author Aliaksandr_Padalka @@ -27,11 +31,10 @@ @RunWith(MockitoJUnitRunner.class) public class TopicServiceTest { - private Optional fake; - private Optional root; + private Topic topic; + private Topic root; private Result results; - private Set fakes; - private List list; + private List topics; @Mock private TopicRepository topicRepo; @@ -44,88 +47,97 @@ public class TopicServiceTest { @Before public void setUp() { - root = Optional.of(new Topic()); - root.get().setTitle("root"); - root.get().setId(0L); + root = new Topic(); + root.setTitle("root"); + root.setId(0L); - fake = Optional.of(new Topic()); - fake.get().setTitle("fake title"); - fake.get().setId(1000L); + topic = new Topic(); + topic.setTitle("fake title"); + topic.setId(1000L); - Topic fake0 = new Topic(); - fake0.setTitle("fake title 0"); - fake0.setId(10L); + Topic topic0 = new Topic(); + topic0.setTitle("fake title 0"); + topic0.setId(10L); - Topic fake1 = new Topic(); - fake1.setTitle("fake title 1"); - fake1.setId(11L); + Topic topic1 = new Topic(); + topic1.setTitle("fake title 1"); + topic1.setId(11L); - Topic fake2 = new Topic(); - fake2.setTitle("fake title 2"); - fake2.setId(12L); + Topic topic2 = new Topic(); + topic2.setTitle("fake title 2"); + topic2.setId(12L); - fakes = new HashSet<>(); + topics = new ArrayList<>(); + topics.add(topic0); + topics.add(topic1); + topics.add(topic2); - Set set = new HashSet<>(); - set.add(fake1); - set.add(fake2); - fake0.setTopics(set); - - fakes.add(fake0); - fakes.add(fake1); - fakes.add(fake2); - - results = new QueryResultBuilder<>(fakes); - - list = new ArrayList<>(); - list.add(fake0); - list.add(fake1); - list.add(fake2); + results = new QueryResultBuilder<>(topics); } @Test public void test_getAllTopics() { when(topicRepo.findAll()).thenReturn(results); - List topics = topicService.getAllTopics(); + List actualTopics = topicService.getAllTopics(); - assertNotNull(topics); - assertEquals(topics, fakes); + assertThat(actualTopics, contains(topics.toArray())); } @Test public void test_updateTopic() { - topicService.updateTopic(fake.get()); - verify(topicRepo).save(fake.get()); + topicService.updateTopic(root); + + verify(topicRepo).save(root); } @Test public void test_deleteTopic() { - topicService.deleteTopic(fake.get().getId()); - verify(topicRepo).delete(fake.get().getId()); + topicService.deleteTopic(topic.getId()); + + verify(topicRepo).delete(topic.getId()); } @Test public void test_createTopic() { - when(topicRepo.findOne(root.get().getId())).thenReturn(root); - topicService.createTopic(fake.get(), root.get().getId()); - verify(topicRepo).save(root.get()); + when(topicRepo.findOne(root.getId())).thenReturn(Optional.of(root)); + + topicService.createTopic(topic, root.getId()); + + verify(topicRepo).save(root); + assertThat(root.getTopics(), contains(topic)); + } + + @Test(expected = TopicNotFoundException.class) + public void test_createTopicNegative() { + when(topicRepo.findOne(root.getId())).thenReturn(Optional.empty()); + + topicService.createTopic(topic, root.getId()); } @Test public void test_getTopicById() { - when(topicRepo.findOne(fake.get().getId())).thenReturn(fake); - Topic topic = topicService.getTopicById(fake.get().getId()); - assertNotNull(topic); - assertEquals(topic, fake.get()); + when(topicRepo.findOne(topic.getId())).thenReturn(Optional.of(topic)); + + Topic actualTopic = topicService.getTopicById(topic.getId()); + + assertEquals(actualTopic, topic); + } + + @Test(expected = TopicNotFoundException.class) + public void test_getTopicByIdNegative() { + when(topicRepo.findOne(topic.getId())).thenReturn(Optional.empty()); + + topicService.getTopicById(topic.getId()); } @Test public void test_getRoot() { - when(topicRepo.findBySchemaPropertyValue("title", "root")).thenReturn(root); - Topic topic = topicService.getRoot().get(); - assertNotNull(topic); - assertEquals(topic, root.get()); + when(topicRepo.findBySchemaPropertyValue("title", "root")).thenReturn(Optional.of(root)); + + Optional actualTopic = topicService.getRoot(); + + assertEquals(actualTopic.get(), root); } } From 9be9c20bc8cc3882d988d89e1da79ebc10ba2dc2 Mon Sep 17 00:00:00 2001 From: Roman Konovalov Date: Wed, 18 Mar 2015 14:49:48 +0300 Subject: [PATCH 4/5] Add javadoc for CustomerService --- .../com/epam/eighty/service/CustomerService.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/epam/eighty/service/CustomerService.java b/src/main/java/com/epam/eighty/service/CustomerService.java index 9aeeb6e..7d63820 100644 --- a/src/main/java/com/epam/eighty/service/CustomerService.java +++ b/src/main/java/com/epam/eighty/service/CustomerService.java @@ -11,16 +11,22 @@ public interface CustomerService { /** - * Retrieves a set of all Customer {@link com.epam.eighty.domain.Customer} entities from the datastore. + * Retrieves a list of all Customer {@link com.epam.eighty.domain.Customer} entities from the datastore. * - * @return the result set + * @return the result list */ List getAllCustomers(); + /** + * Retrieves a list of customer {@link com.epam.eighty.domain.Customer} entities whose names match the given customerName. + * + * @param customerName + * @return a list of customers + */ List getCustomersMatchingName(String customerName); /** - * Retrieves a list of customer {@link com.epam.eighty.domain.Customer} entities whose questions present in topic with given id . + * Retrieves a list of customer {@link com.epam.eighty.domain.Customer} entities whose questions present in topic with given id. * * @param id topic id * @return a list of customers From bc00b4befacb99d95326847acbf991627cab62d1 Mon Sep 17 00:00:00 2001 From: Roman Konovalov Date: Wed, 18 Mar 2015 17:15:34 +0300 Subject: [PATCH 5/5] Add javadoc to TopicService --- .../epam/eighty/service/CustomerService.java | 6 +-- .../com/epam/eighty/service/TopicService.java | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/epam/eighty/service/CustomerService.java b/src/main/java/com/epam/eighty/service/CustomerService.java index 7d63820..a44fb35 100644 --- a/src/main/java/com/epam/eighty/service/CustomerService.java +++ b/src/main/java/com/epam/eighty/service/CustomerService.java @@ -11,14 +11,14 @@ public interface CustomerService { /** - * Retrieves a list of all Customer {@link com.epam.eighty.domain.Customer} entities from the datastore. + * Retrieves a list of all {@link com.epam.eighty.domain.Customer Customer} entities from the datastore. * * @return the result list */ List getAllCustomers(); /** - * Retrieves a list of customer {@link com.epam.eighty.domain.Customer} entities whose names match the given customerName. + * Retrieves a list of {@link com.epam.eighty.domain.Customer Customer} entities whose names match the given {@code customerName}. * * @param customerName * @return a list of customers @@ -26,7 +26,7 @@ public interface CustomerService { List getCustomersMatchingName(String customerName); /** - * Retrieves a list of customer {@link com.epam.eighty.domain.Customer} entities whose questions present in topic with given id. + * Retrieves a list of {@link com.epam.eighty.domain.Customer Customer} entities whose questions present in topic with the given {@code id}. * * @param id topic id * @return a list of customers diff --git a/src/main/java/com/epam/eighty/service/TopicService.java b/src/main/java/com/epam/eighty/service/TopicService.java index 8d5b895..7b71c5e 100644 --- a/src/main/java/com/epam/eighty/service/TopicService.java +++ b/src/main/java/com/epam/eighty/service/TopicService.java @@ -4,16 +4,57 @@ import java.util.Optional; import com.epam.eighty.domain.Topic; +import com.epam.eighty.exception.TopicNotFoundException; /** * @author Aliaksandr_Padalka */ public interface TopicService { + /** + * Returns root {@link com.epam.eighty.domain.Topic Topic} entity wrapped in {@link java.util.Optional Optional}. + * + * @return root topic + */ Optional getRoot(); + + /** + * Returns {@link com.epam.eighty.domain.Topic Topic} entity with the given {@code id}. + * + * @param id topic id + * @return topic + * @throws TopicNotFoundException when topic with the given {@code id} not found + */ Topic getTopicById(Long id); + + /** + * Returns a list of all {@link com.epam.eighty.domain.Topic Topic} entities from the datastore. + * + * @return the result list + */ List getAllTopics(); + + /** + * Updates the given {@code topic} in the datastore. + * + * @param topic topic to update + */ void updateTopic(Topic topic); + + /** + * Deletes topic with the given {@code id} in the datastore. + * + * @param id topic id + */ void deleteTopic(Long id); + + /** + * Adds the given {@code topic} to topic with the given {@code id} and save it. + * + * @param topic to add + * @param id topic id to add to + * @return added topic + * @throws TopicNotFoundException when topic with the given {@code id} not found + */ Topic createTopic(Topic topic, Long id); }