diff --git a/pom.xml b/pom.xml index dd014f0..5473141 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,7 @@ 1.18.4 1.2.17 4.12 + 2.23.4 @@ -47,6 +48,12 @@ ${junit.ver} test + + org.mockito + mockito-core + ${mocito.ver} + test + diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index b4b7f4b..50cb85f 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -19,7 +19,9 @@ public class Autoresponder { public String answer(@NotNull Integer idPerson, @NotNull String message) { Person person = personService.getPersonById(idPerson); Unit unit = person.getUnit(); - return unitService.nextUnit(unit, message).getAnswer(); + unit = unitService.nextUnit(unit, message); + person.setUnit(unit); + return unit.getAnswer(); } public String answer(@NotNull Integer idPerson, @NotNull String message, @NotNull List words) { diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index bf3a536..3e3add3 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -13,5 +13,6 @@ public class Unit { private String answer; private Integer priority; private Set keyWords; + private Integer matchThreshold; } diff --git a/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java index 32ed579..0d41eb1 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java @@ -1,14 +1,15 @@ package org.sadtech.autoresponder.service.impl; +import lombok.AllArgsConstructor; import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.repository.PersonRepository; import org.sadtech.autoresponder.service.PersonService; +@AllArgsConstructor public class PersonServiceImpl implements PersonService { private PersonRepository personRepository; - @Override public Person getPersonById(Integer integer) { return personRepository.getPersonById(integer); diff --git a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java index e4b58e9..b209f6b 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java @@ -2,14 +2,19 @@ package org.sadtech.autoresponder.service.impl; import com.sun.istack.internal.NotNull; import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j; +import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.repository.UnitRepository; import org.sadtech.autoresponder.service.UnitService; import org.sadtech.autoresponder.submodule.parser.Parser; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; +@Log4j @AllArgsConstructor public class UnitServiceImpl implements UnitService { @@ -17,13 +22,14 @@ public class UnitServiceImpl implements UnitService { public Unit nextUnit(@NotNull Unit unit, @NotNull String message) { ArrayList nextUnits = (ArrayList) unit.getNextUnits(); - Unit unitReturn = new Unit(); if (nextUnits.size() > 0) { Parser parser = new Parser(); parser.setText(message); parser.parse(); + Unit unitReturn = new Unit(); + unitReturn.setPriority(0); for (Unit nextUnit : nextUnits) { - if (nextUnit.getKeyWords().retainAll(parser.getWords()) && (nextUnit.getPriority()>unitReturn.getPriority())) { + if (!Collections.disjoint(nextUnit.getKeyWords(), parser.getWords()) && (nextUnit.getPriority()>unitReturn.getPriority())) { unitReturn = nextUnit; } } diff --git a/src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java b/src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java index 3e64e4b..d0bb508 100644 --- a/src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java +++ b/src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java @@ -3,6 +3,7 @@ package org.sadtech.autoresponder.submodule.parser; import lombok.Getter; import lombok.Setter; +import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -10,7 +11,7 @@ import java.util.regex.Pattern; public class Parser { @Getter - private Set words; + private Set words = new HashSet<>(); @Setter private String text; @@ -19,7 +20,7 @@ public class Parser { Pattern p = Pattern.compile("[а-яА-Я0-9]+"); Matcher m = p.matcher(text); while (m.find()) { - words.add(m.group()); + words.add(m.group().toLowerCase()); } } 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..5a95f98 --- /dev/null +++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java @@ -0,0 +1,76 @@ +package org.sadtech.autoresponder; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.sadtech.autoresponder.entity.Person; +import org.sadtech.autoresponder.entity.Unit; +import org.sadtech.autoresponder.repository.impl.PersonRepositoryImpl; +import org.sadtech.autoresponder.repository.impl.UnitRepositoryImpl; +import org.sadtech.autoresponder.service.impl.PersonServiceImpl; +import org.sadtech.autoresponder.service.impl.UnitServiceImpl; + +import java.util.ArrayList; +import java.util.HashSet; + +import static org.junit.Assert.*; + +public class AutoresponderTest { + + private Person person = new Person(); + private Unit unit = new Unit(); + private Unit unit2 = new Unit(); + private HashSet words = new HashSet<>(); + private ArrayList units = new ArrayList<>(); + private UnitRepositoryImpl unitRepository = new UnitRepositoryImpl(); + private PersonRepositoryImpl personRepository = new PersonRepositoryImpl(); + + private UnitServiceImpl unitService = new UnitServiceImpl(unitRepository); + private PersonServiceImpl personService = new PersonServiceImpl(personRepository); + private Autoresponder autoresponder = new Autoresponder(unitService, personService); + + + @Before + public void before() { + words.add("тест"); + + unit.setIdUnit(1); + unit.setMatchThreshold(100); + + units.add(unit2); + + unit.setNextUnits(units); + + unit2.setIdUnit(2); + unit2.setAnswer("Ответ с {0} параметрами!"); + unit2.setPriority(100); + unit2.setKeyWords(words); + unit2.setMatchThreshold(100); + + person.setUnit(unit); + person.setId(1); + + unitRepository.addUnit(unit); + unitRepository.addUnit(unit2); + personRepository.addPerson(person); + } + + @Test + public void answer() { + Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); + } + + @Test + public void answer1() { + ArrayList words = new ArrayList<>(); + words.add("одним"); + Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним параметрами!"); + } + + @Test + public void answer2() { + String test = autoresponder.answer(person.getId(), "Привет это срабатывания"); + Assert.assertNull(test); + } +} \ No newline at end of file diff --git a/src/test/java/org/sadtech/autoresponder/submodule/parser/ParserTest.java b/src/test/java/org/sadtech/autoresponder/submodule/parser/ParserTest.java new file mode 100644 index 0000000..402a24a --- /dev/null +++ b/src/test/java/org/sadtech/autoresponder/submodule/parser/ParserTest.java @@ -0,0 +1,26 @@ +package org.sadtech.autoresponder.submodule.parser; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.*; + +public class ParserTest { + + private Parser parser = new Parser(); + private Set words = new HashSet<>(); + + @Test + public void parse() { + parser.setText("Проверка парсера на правильность"); + parser.parse(); + words.add("проверка"); + words.add("парсера"); + words.add("на"); + words.add("правильность"); + Assert.assertEquals(parser.getWords(), words); + } +} \ No newline at end of file