diff --git a/pom.xml b/pom.xml index 0763275..46e6368 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.autoresponder core - 1.0 + 1.1.0-RELEASE @@ -41,6 +41,7 @@ ${log4j} + junit diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index d30ea01..46171e9 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -1,51 +1,57 @@ package org.sadtech.autoresponder; -import com.sun.istack.internal.NotNull; -import lombok.AllArgsConstructor; import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Unit; +import org.sadtech.autoresponder.entity.compare.UnitPriorityComparator; import org.sadtech.autoresponder.service.PersonService; import org.sadtech.autoresponder.service.UnitService; -import org.sadtech.autoresponder.submodule.parser.InsertWords; +import org.sadtech.autoresponder.submodule.parser.Parser; +import java.util.Collections; import java.util.List; -@AllArgsConstructor public class Autoresponder { private UnitService unitService; private PersonService personService; - public String answer(@NotNull Integer idPerson, @NotNull String message) { - Person person = CheckAndAddPerson(idPerson); + public Autoresponder(UnitService unitService, PersonService personService) { + this.unitService = unitService; + this.personService = personService; + } + + public Unit answer(Integer idPerson, String message) { + Person person = checkAndAddPerson(idPerson); Unit unit; if (person.getUnit() == null) { - unit = unitService.nextUnit(unitService.menuUnit(), message); + unit = nextUnit(unitService.menuUnit(), message); } else { - unit = unitService.nextUnit(person.getUnit(), message); + unit = nextUnit(person.getUnit().getNextUnits(), message); } person.setUnit(unit); - return unit.getAnswer(); + return unit; } - public String answer(@NotNull Integer idPerson, @NotNull String message, @NotNull List words) { - String answer = answer(idPerson, message); - InsertWords insertWords = new InsertWords(); - insertWords.setInText(answer); - insertWords.insert(words); - return insertWords.getOutText(); - } - - private Person CheckAndAddPerson(Integer idPerson) { + private Person checkAndAddPerson(Integer idPerson) { Person person; if (personService.checkPerson(idPerson)) { person = personService.getPersonById(idPerson); } else { - person = new Person(); - person.setId(idPerson); + person = new Person(idPerson); personService.addPerson(person); } return person; } + private Unit nextUnit(List nextUnits, String message) { + if (nextUnits.size() > 0) { + Parser parser = new Parser(); + parser.setText(message); + parser.parse(); + return nextUnits.stream().filter(nextUnit -> !Collections.disjoint(nextUnit.getKeyWords(), parser.getWords())).max(new UnitPriorityComparator()).get(); + } else { + return null; + } + } + } diff --git a/src/main/java/org/sadtech/autoresponder/entity/Person.java b/src/main/java/org/sadtech/autoresponder/entity/Person.java index ab4a6b3..86fb8d1 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Person.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Person.java @@ -1,11 +1,48 @@ package org.sadtech.autoresponder.entity; -import lombok.Data; +import java.util.Objects; -@Data public class Person { private Integer id; private Unit unit; + public Person(Integer id, Unit unit) { + this.id = id; + this.unit = unit; + } + + public Person(Integer id) { + this.id = id; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Unit getUnit() { + return unit; + } + + public void setUnit(Unit unit) { + this.unit = unit; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Person person = (Person) o; + return Objects.equals(id, person.id) && + Objects.equals(unit, person.unit); + } + + @Override + public int hashCode() { + return Objects.hash(id, unit); + } } diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 18f408c..f56602b 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -1,19 +1,83 @@ package org.sadtech.autoresponder.entity; -import lombok.Data; - import java.util.List; +import java.util.Objects; import java.util.Set; -@Data -public class Unit { +public abstract class Unit { - private Integer idUnit; - private List nextUnits; - private String answer; - private Integer priority; private Set keyWords; private Integer matchThreshold; - private Boolean level = false; + private Integer priority; + private Boolean level; + private List nextUnits; + public Unit() { + level = false; + } + + public Unit(Set keyWords, Integer matchThreshold, Integer priority, Boolean level, List nextUnits) { + this.keyWords = keyWords; + this.matchThreshold = matchThreshold; + this.priority = priority; + this.level = level; + this.nextUnits = nextUnits; + } + + public Set getKeyWords() { + return keyWords; + } + + public void setKeyWords(Set keyWords) { + this.keyWords = keyWords; + } + + public Integer getMatchThreshold() { + return matchThreshold; + } + + public void setMatchThreshold(Integer matchThreshold) { + this.matchThreshold = matchThreshold; + } + + public Integer getPriority() { + return priority; + } + + public void setPriority(Integer priority) { + this.priority = priority; + } + + public Boolean getLevel() { + return level; + } + + public void setLevel(Boolean level) { + this.level = level; + } + + public List getNextUnits() { + return nextUnits; + } + + public void setNextUnits(List nextUnits) { + this.nextUnits = nextUnits; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Unit unit = (Unit) o; + return Objects.equals(keyWords, unit.keyWords) && + Objects.equals(matchThreshold, unit.matchThreshold) && + Objects.equals(priority, unit.priority) && + Objects.equals(level, unit.level) && + Objects.equals(nextUnits, unit.nextUnits); + } + + @Override + public int hashCode() { + return Objects.hash(keyWords, matchThreshold, priority, level, nextUnits); + } } diff --git a/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java b/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java new file mode 100644 index 0000000..41003ad --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java @@ -0,0 +1,19 @@ +package org.sadtech.autoresponder.entity.compare; + +import org.sadtech.autoresponder.entity.Unit; + +import java.util.Comparator; + +public class UnitPriorityComparator implements Comparator { + + @Override + public int compare(Unit o1, Unit o2) { + if (o1.getPriority() < o2.getPriority()) { + return -1; + } else if (o1.getPriority().equals(o2.getPriority())) { + return 0; + } + return 1; + } + +} diff --git a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java index a9c668e..35312f5 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java @@ -2,14 +2,16 @@ package org.sadtech.autoresponder.repository; import org.sadtech.autoresponder.entity.Person; -import java.util.Collection; import java.util.Map; public interface PersonRepository { void addPerson(Person person); + void removePerson(Person person); + void addPersonAll(Map personCollection); + Person getPersonById(Integer idPerson); } diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java index 0f4679a..d069a1e 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java @@ -7,8 +7,6 @@ import java.util.List; public interface UnitRepository { - Unit getUnitById(Integer idUnit); - void addUnit(Unit unit); void addUnits(Collection units); @@ -16,4 +14,5 @@ public interface UnitRepository { void removeUnit(Unit idUnit); List menuUnits(); + } diff --git a/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryMap.java b/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryMap.java deleted file mode 100644 index 7ed7c7e..0000000 --- a/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryMap.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.sadtech.autoresponder.repository.impl; - -import org.sadtech.autoresponder.entity.Unit; -import org.sadtech.autoresponder.repository.UnitRepository; - -import java.util.*; - -public class UnitRepositoryMap implements UnitRepository { - - private Map units = new HashMap<>(); - - @Override - public Unit getUnitById(Integer idUnit) { - return units.get(idUnit); - } - - @Override - public void addUnit(Unit unit) { - units.put(unit.getIdUnit(), unit); - } - - @Override - public void addUnits(Collection units) { - units.addAll(units); - } - - @Override - public void removeUnit(Unit idUnit) { - units.remove(idUnit.getIdUnit()); - } - - @Override - public List menuUnits() { - ArrayList unitsMenu = new ArrayList<>(); - for (Integer integer : units.keySet()) { - if (units.get(integer).getLevel()) { - unitsMenu.add(units.get(integer)); - } - } - return unitsMenu; - } -} diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitService.java b/src/main/java/org/sadtech/autoresponder/service/UnitService.java index c319e0e..45285af 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitService.java @@ -1,13 +1,14 @@ package org.sadtech.autoresponder.service; import org.sadtech.autoresponder.entity.Unit; +import org.sadtech.autoresponder.repository.UnitRepository; + +import java.util.List; public interface UnitService { - Unit nextUnit(Unit unit, String message); + List menuUnit(); - Unit getUnitById(Integer idUnit); - - Unit menuUnit(); + void addUnitRepository(UnitRepository unitRepository); } 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 5325b22..d7933f2 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java @@ -1,15 +1,17 @@ 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; + public PersonServiceImpl(PersonRepository personRepository) { + this.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 3d88777..3a975bb 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java @@ -1,52 +1,38 @@ 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.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.Collections; +import java.util.List; @Log4j -@AllArgsConstructor public class UnitServiceImpl implements UnitService { - private UnitRepository unitRepository; + private List unitRepositories; - public Unit nextUnit(Unit unit, @NotNull String message) { - ArrayList nextUnits = (ArrayList) unit.getNextUnits(); - 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 (!Collections.disjoint(nextUnit.getKeyWords(), parser.getWords()) && (nextUnit.getPriority() > unitReturn.getPriority())) { - unitReturn = nextUnit; - } - } - return unitReturn; - } else { - return null; + public UnitServiceImpl() { + unitRepositories = new ArrayList<>(); + } + + public UnitServiceImpl(List unitRepositories) { + this.unitRepositories = unitRepositories; + } + + @Override + public List menuUnit() { + List units = new ArrayList<>(); + for (UnitRepository unitRepository : unitRepositories) { + units.addAll(unitRepository.menuUnits()); } - + return units; } @Override - public Unit getUnitById(@NotNull Integer idUnit) { - return unitRepository.getUnitById(idUnit); - } - - @Override - public Unit menuUnit() { - Unit menuUnit = new Unit(); - menuUnit.setNextUnits(unitRepository.menuUnits()); - return menuUnit; + public void addUnitRepository(UnitRepository unitRepository) { + unitRepositories.add(unitRepository); } diff --git a/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java b/src/main/java/org/sadtech/autoresponder/submodule/insertwords/InsertWords.java similarity index 95% rename from src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java rename to src/main/java/org/sadtech/autoresponder/submodule/insertwords/InsertWords.java index f44889e..cb9642b 100644 --- a/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java +++ b/src/main/java/org/sadtech/autoresponder/submodule/insertwords/InsertWords.java @@ -1,4 +1,4 @@ -package org.sadtech.autoresponder.submodule.parser; +package org.sadtech.autoresponder.submodule.insertwords; import lombok.Getter; import lombok.Setter; 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 d0bb508..5db9916 100644 --- a/src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java +++ b/src/main/java/org/sadtech/autoresponder/submodule/parser/Parser.java @@ -1,8 +1,5 @@ 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; @@ -10,12 +7,17 @@ import java.util.regex.Pattern; public class Parser { - @Getter private Set words = new HashSet<>(); - - @Setter private String text; + public Set getWords() { + return words; + } + + public void setText(String text) { + this.text = text; + } + public void parse() { Pattern p = Pattern.compile("[а-яА-Я0-9]+"); Matcher m = p.matcher(text); diff --git a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java index 7d79844..bb12355 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java @@ -5,38 +5,38 @@ import org.junit.Before; import org.junit.Test; import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Unit; +import org.sadtech.autoresponder.repository.UnitRepository; import org.sadtech.autoresponder.repository.impl.PersonRepositoryMap; -import org.sadtech.autoresponder.repository.impl.UnitRepositoryMap; import org.sadtech.autoresponder.service.impl.PersonServiceImpl; import org.sadtech.autoresponder.service.impl.UnitServiceImpl; -import java.util.ArrayList; -import java.util.HashSet; +import java.util.*; public class AutoresponderTest { - private Person person = new Person(); - private Unit unit = new Unit(); - private Unit unit2 = new Unit(); - private Unit unit3 = new Unit(); + private Person person = new Person(1); + private TextUnit unit = new TextUnit(); + private TextUnit unit2 = new TextUnit(); + private TextUnit unit3 = new TextUnit(); private ArrayList units = new ArrayList<>(); - private UnitRepositoryMap unitRepository = new UnitRepositoryMap(); + private TextUnitRepositoryList unitRepository = new TextUnitRepositoryList(); private PersonRepositoryMap personRepository = new PersonRepositoryMap(); - private UnitServiceImpl unitService = new UnitServiceImpl(unitRepository); + private UnitServiceImpl unitService = new UnitServiceImpl(); private PersonServiceImpl personService = new PersonServiceImpl(personRepository); private Autoresponder autoresponder = new Autoresponder(unitService, personService); @Before public void before() { + unitService.addUnitRepository(unitRepository); + HashSet words = new HashSet<>(); HashSet words2 = new HashSet<>(); words.add("тест"); words2.add("тест"); words2.add("привет"); - unit.setIdUnit(1); unit.setLevel(true); unit.setPriority(50); unit.setKeyWords(words); @@ -48,20 +48,17 @@ public class AutoresponderTest { unit.setNextUnits(units); - unit2.setIdUnit(2); unit2.setAnswer("Ответ с {0} параметрами!"); unit2.setPriority(60); unit2.setKeyWords(words); unit2.setMatchThreshold(100); - unit3.setIdUnit(3); unit3.setAnswer("Второй Ответ с {0} параметрами!"); unit3.setPriority(50); unit3.setKeyWords(words2); unit3.setMatchThreshold(100); person.setUnit(unit); - person.setId(1); unitRepository.addUnit(unit); unitRepository.addUnit(unit2); @@ -70,49 +67,17 @@ public class AutoresponderTest { @Test public void usualAnswer() { - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); + Unit unit = autoresponder.answer(person.getId(), "Привет это тест срабатывания"); + Assert.assertEquals(((TextUnit) unit).getAnswer(), "Ответ с {0} параметрами!"); } - @Test - public void answerOneParameter() { - ArrayList words = new ArrayList<>(); - words.add("одним"); - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним параметрами!"); - } @Test public void NoAnswer() { person.setUnit(null); - String test = autoresponder.answer(person.getId(), "Привет это срабатывания"); - Assert.assertNull(test); + autoresponder.answer(person.getId(), "Привет это срабатывания"); } - @Test - public void answerTwoParameter() { - ArrayList words = new ArrayList<>(); - words.add("одним"); - words.add("двумя"); - unit2.setAnswer("Ответ с {0} и {1}"); - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним и двумя"); - } - - @Test - public void incorrectSettingsWords() { - ArrayList words = new ArrayList<>(); - words.add("одним"); - words.add("двумя"); - unit2.setAnswer("Ответ с {1} и {3}"); - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с двумя и {3}"); - } - - @Test - public void answerRepeatSingleParameter() { - ArrayList words = new ArrayList<>(); - words.add("одним"); - words.add("двумя"); - unit2.setAnswer("Ответ с {1} и {1}"); - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с двумя и двумя"); - } @Test public void answerByPriority() { @@ -121,7 +86,57 @@ public class AutoresponderTest { @Test public void answerNoPerson() { - Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Здравствуйте, вы написали в нашу компанию!"); - Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); + TextUnit textUnit = (TextUnit) autoresponder.answer(100, "Привет это тест срабатывания"); + Assert.assertEquals(textUnit.getAnswer(), "Здравствуйте, вы написали в нашу компанию!"); + textUnit = (TextUnit) autoresponder.answer(100, "Привет это тест срабатывания"); + Assert.assertEquals(textUnit.getAnswer(), "Ответ с {0} параметрами!"); + } + + + private class TextUnit extends Unit { + private String answer; + + public TextUnit() { + super(); + } + + public String getAnswer() { + return answer; + } + + public void setAnswer(String answer) { + this.answer = answer; + } + } + + private class TextUnitRepositoryList implements UnitRepository { + + List textUnits = new ArrayList<>(); + + @Override + public void addUnit(Unit unit) { + textUnits.add(unit); + } + + @Override + public void addUnits(Collection units) { + textUnits.addAll(units); + } + + @Override + public void removeUnit(Unit idUnit) { + textUnits.remove(idUnit); + } + + @Override + public List menuUnits() { + List units = new ArrayList<>(); + for (Unit textUnit : textUnits) { + if (textUnit.getLevel()) { + units.add(textUnit); + } + } + return units; + } } } \ No newline at end of file diff --git a/src/test/java/org/sadtech/autoresponder/submodule/parser/InsertWordsTest.java b/src/test/java/org/sadtech/autoresponder/submodule/insertwords/InsertWordsTest.java similarity index 91% rename from src/test/java/org/sadtech/autoresponder/submodule/parser/InsertWordsTest.java rename to src/test/java/org/sadtech/autoresponder/submodule/insertwords/InsertWordsTest.java index f0d74b5..763bd68 100644 --- a/src/test/java/org/sadtech/autoresponder/submodule/parser/InsertWordsTest.java +++ b/src/test/java/org/sadtech/autoresponder/submodule/insertwords/InsertWordsTest.java @@ -1,8 +1,9 @@ -package org.sadtech.autoresponder.submodule.parser; +package org.sadtech.autoresponder.submodule.insertwords; import org.junit.After; import org.junit.Assert; import org.junit.Test; +import org.sadtech.autoresponder.submodule.insertwords.InsertWords; import java.util.ArrayList;