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/main/java/com/epam/eighty/service/CustomerService.java b/src/main/java/com/epam/eighty/service/CustomerService.java
index 86ba861..a44fb35 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 {@link com.epam.eighty.domain.Customer Customer} entities from the datastore.
*
- * @return the result set
+ * @return the result list
*/
List getAllCustomers();
- List getSortedSetOfCustomersByName(String 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
+ */
+ 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/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 e12af4e..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 getSortedSetOfTagsByName(String tagName);
+ List getTagsMatchingName(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 11e6c45..7b71c5e 100644
--- a/src/main/java/com/epam/eighty/service/TopicService.java
+++ b/src/main/java/com/epam/eighty/service/TopicService.java
@@ -1,19 +1,60 @@
package com.epam.eighty.service;
+import java.util.List;
import java.util.Optional;
-import java.util.Set;
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();
- Optional getTopicById(Long id);
- Set getAllTopics();
+
+ /**
+ * 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);
}
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 972056e..58690ef 100644
--- a/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java
+++ b/src/main/java/com/epam/eighty/service/impl/CustomerServiceImpl.java
@@ -20,30 +20,27 @@
@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.getCustomersMatchingName(ANY_SYMBOL + customerName + ANY_SYMBOL);
+ public List getCustomersMatchingName(final String customerName) {
+ return customerRepository.getCustomersMatchingName(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(),
- topicId)));
+ .forEach(customer -> customer.setCountInTopic(customerRepository.getQuestionsNumberInTopicByCustomer(customer.getName(), topicId)));
return customerList;
}
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 ead815f..0d50d73 100644
--- a/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java
+++ b/src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java
@@ -17,16 +17,13 @@
@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);
- 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;
}
@@ -42,8 +39,8 @@ public List getTopNFromAllTags(final Long limit) {
}
@Override
- public List getSortedSetOfTagsByName(final String tagName) {
- return tagRepo.getTagsMatchingName(ANY_SYMBOL + tagName + ANY_SYMBOL);
+ 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 2ba1964..6b0c6a0 100644
--- a/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java
+++ b/src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java
@@ -1,16 +1,17 @@
package com.epam.eighty.service.impl;
-import java.util.Optional;
-import java.util.Set;
+import com.epam.eighty.domain.Topic;
+import com.epam.eighty.exception.TopicNotFoundException;
+import com.epam.eighty.repository.TopicRepository;
+import com.epam.eighty.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import com.epam.eighty.domain.Topic;
-import com.epam.eighty.repository.TopicRepository;
-import com.epam.eighty.service.TopicService;
+import java.util.List;
+import java.util.Optional;
/**
* @author Aliaksandr_Padalka
@@ -27,26 +28,23 @@ public class TopicServiceImpl implements TopicService {
@Override
public Optional getRoot() {
- Optional root = topicRepo.findBySchemaPropertyValue("title", "root");
- root.ifPresent(someRoot ->
- someRoot.getTopics().forEach(template::fetch)
- );
+ final Optional root = topicRepo.findBySchemaPropertyValue("title", "root");
+ root.ifPresent(someRoot -> someRoot.getTopics().forEach(template::fetch));
return root;
}
@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
@@ -61,11 +59,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..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.getSortedSetOfCustomersByName(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/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 1f96a73..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.getSortedSetOfTagsByName(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/main/java/com/epam/eighty/web/api/TopicController.java b/src/main/java/com/epam/eighty/web/api/TopicController.java
index 0beffde..da1a9b5 100644
--- a/src/main/java/com/epam/eighty/web/api/TopicController.java
+++ b/src/main/java/com/epam/eighty/web/api/TopicController.java
@@ -1,31 +1,32 @@
package com.epam.eighty.web.api;
-import java.io.IOException;
-import java.util.Optional;
-
-import javax.servlet.http.HttpServletResponse;
+import com.codahale.metrics.annotation.Timed;
+import com.epam.eighty.domain.Topic;
+import com.epam.eighty.exception.TopicNotFoundException;
+import com.epam.eighty.service.DBPopulatorService;
+import com.epam.eighty.service.TopicService;
+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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
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 com.codahale.metrics.annotation.Timed;
-import com.epam.eighty.domain.Topic;
-import com.epam.eighty.exception.TopicNotFoundException;
-import com.epam.eighty.service.DBPopulatorService;
-import com.epam.eighty.service.TopicService;
-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;
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+
+import javax.servlet.http.HttpServletResponse;
/**
* @author Aliaksandr_Padalka
@@ -51,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 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 0713ba1..665257e 100644
--- a/src/test/java/com/epam/eighty/repository/TagRepossitoryTest.java
+++ b/src/test/java/com/epam/eighty/repository/TagRepossitoryTest.java
@@ -37,13 +37,13 @@ public class TagRepossitoryTest {
private ExecutionEngine engine;
@Autowired
- private String createCypherScript;
+ private String creatCypherScript;
private String deleteScript = "START n=node(*) OPTIONAL MATCH (n)-[r]-() delete n,r;";
@Before
public void prepareTestDatabase() throws IOException {
- engine.execute(createCypherScript);
+ engine.execute(creatCypherScript);
}
@After
diff --git a/src/test/java/com/epam/eighty/service/CustomerServiceTest.java b/src/test/java/com/epam/eighty/service/CustomerServiceTest.java
index 283895c..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;
@@ -17,25 +13,23 @@
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 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,75 +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);
-
- List customers = customerService.getSortedSetOfCustomersByName(EMPTY_STRING);
-
- assertNotNull(customers);
- assertFalse(customers.isEmpty());
+ public void test_getCustomersMatchingName() {
+ Mockito.when(customerRepository.getCustomersMatchingName(Mockito.anyString())).thenReturn(customers);
+
+ List actualCustomers = customerService.getCustomersMatchingName(Mockito.anyString());
+
+ 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 39ff976..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);
- Set set = questionService.getAllQuestions();
+ List actualQuestions = questionService.getAllQuestions();
- assertNotNull(set);
- assertEquals(set, 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).get();
-
- 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.getQuestionsPage(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.getQuestionsByTopicAndTag(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.getQuestionsByCustomer(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 e749e8f..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.getSortedSetOfTagsByName(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 6150a52..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);
- Set set = topicService.getAllTopics();
+ List actualTopics = topicService.getAllTopics();
- assertNotNull(set);
- assertEquals(set, 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()).get();
- 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);
}
}
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..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.getSortedSetOfCustomersByName(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/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 0c5158d..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.getSortedSetOfTagsByName(TEST_STRING)).thenReturn(tagSet);
+ when(tagService.getTagsMatchingName(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 a7dbfae..b871c97 100644
--- a/src/test/java/com/epam/eighty/web/api/TopicControllerTest.java
+++ b/src/test/java/com/epam/eighty/web/api/TopicControllerTest.java
@@ -50,16 +50,8 @@ 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