diff --git a/pom.xml b/pom.xml
index 408b8dc..522d8ab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
org.sadtech.autoresponder
autoresponder
- 1.6.1-RELEASE
+ 1.6.2-RELEASE
jar
@@ -32,6 +32,12 @@
slf4j-api
${slf4j.ver}
+
+
+ junit
+ junit
+ 4.12
+
diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java
index 829094e..fa8b8bf 100644
--- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java
+++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java
@@ -40,12 +40,16 @@ public class Autoresponder {
this.defaultUnit = defaultUnit;
}
+ public Unit getDefaultUnit() {
+ return defaultUnit;
+ }
+
public Unit answer(Integer personId, String message) {
UnitPointer unitPointer = checkAndAddPerson(personId);
Unit unit;
try {
if (unitPointer.getUnit() == null || unitPointer.getUnit().getNextUnits() == null) {
- unit = nextUnit(startUnits, message); // выбирает unit из startUnits, если пользователь обращается впервые
+ unit = nextUnit(startUnits, message);
} else {
unit = nextUnit(unitPointer.getUnit().getNextUnits(), message);
}
@@ -58,13 +62,12 @@ public class Autoresponder {
private Unit nextUnit(Set nextUnits, String message) {
Optional unit = nextUnits.stream()
- .filter(nextUnit -> nextUnit.getPattern() != null)
- .filter(nextUnit -> patternReg(nextUnit, message))
+ .filter(nextUnit -> nextUnit.getPattern() != null && patternReg(nextUnit, message))
.max(UNIT_PRIORITY_COMPARATOR);
if (!unit.isPresent()) {
unit = nextUnits.stream()
- .filter(nextUnit -> textPercentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold())
+ .filter(nextUnit -> percentageMatch(nextUnit, Parser.parse(message)) >= nextUnit.getMatchThreshold())
.max(UNIT_PRIORITY_COMPARATOR);
}
@@ -93,7 +96,7 @@ public class Autoresponder {
return m.find();
}
- private Double textPercentageMatch(Unit unit, Set words) {
+ private Double percentageMatch(Unit unit, Set words) {
if (unit.getKeyWords() != null) {
Set temp = new HashSet<>(unit.getKeyWords());
temp.retainAll(words);
diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java
index 576bf75..acd74fb 100644
--- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java
+++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepository.java
@@ -2,17 +2,17 @@ package org.sadtech.autoresponder.repository;
import org.sadtech.autoresponder.entity.UnitPointer;
-import java.util.Map;
+import java.util.Collection;
public interface UnitPointerRepository {
- Integer add(UnitPointer unitPointer);
+ void add(UnitPointer unitPointer);
void edit(UnitPointer unitPointer);
void remove(Integer entityId);
- void addAll(Map unitPointerMap);
+ void addAll(Collection unitPointerMap);
UnitPointer findByEntityId(Integer entityId);
diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java
index 5534c55..f093d46 100644
--- a/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java
+++ b/src/main/java/org/sadtech/autoresponder/repository/UnitPointerRepositoryMap.java
@@ -2,6 +2,7 @@ package org.sadtech.autoresponder.repository;
import org.sadtech.autoresponder.entity.UnitPointer;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -10,9 +11,8 @@ public class UnitPointerRepositoryMap implements UnitPointerRepository {
private Map unitPointerMap = new HashMap<>();
@Override
- public Integer add(UnitPointer unitPointer) {
+ public void add(UnitPointer unitPointer) {
unitPointerMap.put(unitPointer.getEntityId(), unitPointer);
- return unitPointer.getEntityId();
}
@Override
@@ -26,8 +26,8 @@ public class UnitPointerRepositoryMap implements UnitPointerRepository {
}
@Override
- public void addAll(Map unitPointerMap) {
- this.unitPointerMap.putAll(unitPointerMap);
+ public void addAll(Collection unitPointers) {
+ unitPointers.parallelStream().forEach(unitPointer -> unitPointerMap.put(unitPointer.getEntityId(), unitPointer));
}
@Override
diff --git a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java
new file mode 100644
index 0000000..d0f7b11
--- /dev/null
+++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java
@@ -0,0 +1,133 @@
+package org.sadtech.autoresponder;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.sadtech.autoresponder.entity.Unit;
+import org.sadtech.autoresponder.exception.NotFoundUnitException;
+import org.sadtech.autoresponder.repository.UnitPointerRepositoryMap;
+import org.sadtech.autoresponder.service.UnitPointerServiceImpl;
+import org.sadtech.autoresponder.test.TestUnit;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+public class AutoresponderTest {
+
+ private Autoresponder autoresponder;
+
+ @Before
+ public void setUp() {
+ TestUnit dela = new TestUnit();
+ dela.setKeyWord("дела", "делишки");
+ dela.setMessage("хорошо");
+
+ TestUnit delaTop = new TestUnit();
+ delaTop.setPriority(100);
+ delaTop.setKeyWord("делишки");
+ delaTop.setMessage("отлично");
+
+ TestUnit hello = new TestUnit();
+ hello.setMessage("тест");
+ hello.setKeyWord("привет");
+ hello.setNextUnit(dela);
+ hello.setNextUnit(delaTop);
+
+ TestUnit number = new TestUnit();
+ number.setKeyWord("89101234567");
+ number.setMessage("ответ");
+
+ TestUnit regExp = new TestUnit();
+ regExp.setPattern(Pattern.compile("^((8|\\+7)[\\- ]?)?(\\(?\\d{3}\\)?[\\- ]?)?[\\d\\- ]{7,10}$"));
+ regExp.setMessage("регулярка");
+ dela.setNextUnit(regExp);
+ dela.setNextUnit(number);
+
+ TestUnit unreal = new TestUnit();
+ unreal.setKeyWord("витамин", "мультифрукт", "арбуз", "параметр");
+ unreal.setMessage("победа");
+ unreal.setMatchThreshold(100);
+
+ Set testUnits = new HashSet<>();
+ testUnits.add(hello);
+ testUnits.add(regExp);
+ testUnits.add(unreal);
+
+ UnitPointerServiceImpl unitPointerService = new UnitPointerServiceImpl(new UnitPointerRepositoryMap());
+ autoresponder = new Autoresponder(unitPointerService, testUnits);
+ }
+
+ @Test
+ public void simpleAnswer() {
+ String message = ((TestUnit) autoresponder.answer(1, "привет это тестирвоание функциональности")).getMessage();
+ Assert.assertEquals("тест", message);
+ String message1 = ((TestUnit) autoresponder.answer(2, "привет, еще одно тестирование")).getMessage();
+ Assert.assertEquals("тест", message1);
+ }
+
+ @Test
+ public void defaultAnswer() {
+ TestUnit defaultUnit = new TestUnit();
+ defaultUnit.setMessage("не знаю");
+ autoresponder.setDefaultUnit(defaultUnit);
+
+ Assert.assertEquals(((TestUnit) autoresponder.answer(2, "пока")).getMessage(), "не знаю");
+ }
+
+ @Test(expected = NotFoundUnitException.class)
+ public void notDefaultAnswer() {
+ autoresponder.answer(2, "пока");
+ }
+
+ @Test
+ public void regExpAnswer() {
+ String message = ((TestUnit) autoresponder.answer(1, "79101234567")).getMessage();
+ Assert.assertEquals(message, "регулярка");
+ }
+
+ @Test
+ public void priorityAnswer() {
+ autoresponder.answer(1, "привет");
+ autoresponder.answer(2, "привет");
+ String message = ((TestUnit) autoresponder.answer(1, "как твои делишки")).getMessage();
+ Assert.assertEquals("отлично", message);
+ String message1 = ((TestUnit) autoresponder.answer(2, "твои дела все еще хорошо?")).getMessage();
+ Assert.assertEquals("хорошо", message1);
+ }
+
+ @Test
+ public void showRegExpVsKeyWords() {
+ autoresponder.answer(1, "привет");
+ autoresponder.answer(1, "дела");
+ Assert.assertEquals("регулярка", ((TestUnit) autoresponder.answer(1, "89101234567")).getMessage());
+ }
+
+ @Test(expected = NotFoundUnitException.class)
+ public void matchThreshold() {
+ autoresponder.answer(1, "витамин я сьем, и арбуз получу");
+ String message = "параметр себе задам: покушать витамин и арбуз, а вместе все это мультифрукт";
+ String answer = ((TestUnit) autoresponder.answer(1, message)).getMessage();
+ Assert.assertEquals("победа", answer);
+ }
+
+ @Test
+ public void generalAnswer() {
+ TestUnit defaultUnit = new TestUnit();
+ defaultUnit.setMessage("не знаю");
+ autoresponder.setDefaultUnit(defaultUnit);
+ String answer = ((TestUnit) autoresponder.answer(1, "привет это тестирование функциональности")).getMessage();
+ Assert.assertEquals("тест", answer);
+ Assert.assertEquals("хорошо", ((TestUnit) autoresponder.answer(1, "как твои дела")).getMessage());
+ String answer2 = ((TestUnit) autoresponder.answer(2, "привет это тестирование функциональности")).getMessage();
+ Assert.assertEquals("тест", answer2);
+ Assert.assertEquals("не знаю", ((TestUnit) autoresponder.answer(1, "нет")).getMessage());
+ String message = "параметр себе задам: покушать витамин и арбуз, а вместе все это мультифрукт";
+ Assert.assertEquals("победа", ((TestUnit) autoresponder.answer(3, message)).getMessage());
+ Assert.assertEquals("регулярка", ((TestUnit) autoresponder.answer(1, "8912345678")).getMessage());
+ String answer3 = ((TestUnit) autoresponder.answer(1, "привет это тестирование функциональности")).getMessage();
+ Assert.assertEquals("тест", answer3);
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/sadtech/autoresponder/test/TestUnit.java b/src/test/java/org/sadtech/autoresponder/test/TestUnit.java
new file mode 100644
index 0000000..40c3cce
--- /dev/null
+++ b/src/test/java/org/sadtech/autoresponder/test/TestUnit.java
@@ -0,0 +1,32 @@
+package org.sadtech.autoresponder.test;
+
+import org.sadtech.autoresponder.entity.Unit;
+
+import java.util.Objects;
+
+public class TestUnit extends Unit {
+
+ private String message;
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof TestUnit)) return false;
+ if (!super.equals(o)) return false;
+ TestUnit testUnit = (TestUnit) o;
+ return Objects.equals(message, testUnit.message);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(super.hashCode(), message);
+ }
+}