From 2f05b557060c9e692c928fccc4a2a37eda2130a9 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 19 Jan 2019 01:36:41 +0300 Subject: [PATCH 1/5] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BD=D0=B5=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D1=83=D1=8E=20lombok=20=D0=B0=D0=BD=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8E=20=D0=BD=D0=B0=20=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD=D1=83=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 3 ++- .../java/org/sadtech/autoresponder/Autoresponder.java | 8 ++++---- .../autoresponder/service/impl/UnitServiceImpl.java | 6 +++--- .../submodule/{parser => insertwords}/InsertWords.java | 2 +- .../{parser => insertwords}/InsertWordsTest.java | 3 ++- 5 files changed, 12 insertions(+), 10 deletions(-) rename src/main/java/org/sadtech/autoresponder/submodule/{parser => insertwords}/InsertWords.java (95%) rename src/test/java/org/sadtech/autoresponder/submodule/{parser => insertwords}/InsertWordsTest.java (91%) diff --git a/pom.xml b/pom.xml index 0763275..2406501 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.autoresponder core - 1.0 + 1.1-SNAPSHOT @@ -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..da2a281 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -1,12 +1,12 @@ package org.sadtech.autoresponder; -import com.sun.istack.internal.NotNull; import lombok.AllArgsConstructor; +import lombok.NonNull; import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.service.PersonService; import org.sadtech.autoresponder.service.UnitService; -import org.sadtech.autoresponder.submodule.parser.InsertWords; +import org.sadtech.autoresponder.submodule.insertwords.InsertWords; import java.util.List; @@ -16,7 +16,7 @@ public class Autoresponder { private UnitService unitService; private PersonService personService; - public String answer(@NotNull Integer idPerson, @NotNull String message) { + public String answer(@NonNull Integer idPerson, @NonNull String message) { Person person = CheckAndAddPerson(idPerson); Unit unit; if (person.getUnit() == null) { @@ -28,7 +28,7 @@ public class Autoresponder { return unit.getAnswer(); } - public String answer(@NotNull Integer idPerson, @NotNull String message, @NotNull List words) { + public String answer(@NonNull Integer idPerson, @NonNull String message, @NonNull List words) { String answer = answer(idPerson, message); InsertWords insertWords = new InsertWords(); insertWords.setInText(answer); 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..aa8f8e0 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java @@ -1,7 +1,7 @@ package org.sadtech.autoresponder.service.impl; -import com.sun.istack.internal.NotNull; import lombok.AllArgsConstructor; +import lombok.NonNull; import lombok.extern.log4j.Log4j; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.repository.UnitRepository; @@ -17,7 +17,7 @@ public class UnitServiceImpl implements UnitService { private UnitRepository unitRepository; - public Unit nextUnit(Unit unit, @NotNull String message) { + public Unit nextUnit(Unit unit, @NonNull String message) { ArrayList nextUnits = (ArrayList) unit.getNextUnits(); if (nextUnits.size() > 0) { Parser parser = new Parser(); @@ -38,7 +38,7 @@ public class UnitServiceImpl implements UnitService { } @Override - public Unit getUnitById(@NotNull Integer idUnit) { + public Unit getUnitById(@NonNull Integer idUnit) { return unitRepository.getUnitById(idUnit); } 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/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; From 65ecc3d38c12eeb56251841f29fb10bc2317513d Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sun, 20 Jan 2019 22:59:33 +0300 Subject: [PATCH 2/5] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81-=D0=BB=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Теперь автоответчик возвращает не строку, а любой объект, который является наследником unit. Для этого: * Перенес поиск следующего юнита в основной класс * Добавил компоратор для сортировки юнитов по приоритету * Юнит теперь абстрактный класс * Удален репозиторий юнита * У юнита удалено поле id * ЮнитСервис теперь хранит в себе все ЮнитРепозитории --- .../sadtech/autoresponder/Autoresponder.java | 43 +-- .../sadtech/autoresponder/entity/Person.java | 41 ++- .../sadtech/autoresponder/entity/Unit.java | 82 +++++- .../compare/UnitPriorityComparator.java | 19 ++ .../repository/PersonRepository.java | 3 + .../repository/UnitRepository.java | 3 +- .../repository/impl/UnitRepositoryMap.java | 42 --- .../autoresponder/service/UnitService.java | 9 +- .../service/impl/PersonServiceImpl.java | 6 +- .../service/impl/UnitServiceImpl.java | 50 ++-- .../autoresponder/AutoresponderTest.java | 251 +++++++++--------- 11 files changed, 312 insertions(+), 237 deletions(-) create mode 100644 src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java delete mode 100644 src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryMap.java diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index da2a281..6e6246c 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -1,42 +1,38 @@ package org.sadtech.autoresponder; -import lombok.AllArgsConstructor; -import lombok.NonNull; 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.insertwords.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(@NonNull Integer idPerson, @NonNull 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(@NonNull Integer idPerson, @NonNull String message, @NonNull 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); @@ -48,4 +44,15 @@ public class Autoresponder { 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..eeccefc 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..6ced573 --- /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..c75b018 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java @@ -8,8 +8,11 @@ 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 aa8f8e0..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 lombok.AllArgsConstructor; -import lombok.NonNull; 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, @NonNull 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(@NonNull 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/test/java/org/sadtech/autoresponder/AutoresponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java index 7d79844..29879d2 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java @@ -1,127 +1,126 @@ -package org.sadtech.autoresponder; - -import org.junit.Assert; -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.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; - -public class AutoresponderTest { - - private Person person = new Person(); - private Unit unit = new Unit(); - private Unit unit2 = new Unit(); - private Unit unit3 = new Unit(); - private ArrayList units = new ArrayList<>(); - private UnitRepositoryMap unitRepository = new UnitRepositoryMap(); - private PersonRepositoryMap personRepository = new PersonRepositoryMap(); - - private UnitServiceImpl unitService = new UnitServiceImpl(unitRepository); - private PersonServiceImpl personService = new PersonServiceImpl(personRepository); - private Autoresponder autoresponder = new Autoresponder(unitService, personService); - - - @Before - public void before() { - 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); - unit.setAnswer("Здравствуйте, вы написали в нашу компанию!"); - unit.setMatchThreshold(100); - - units.add(unit2); - units.add(unit3); - - 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); - personRepository.addPerson(person); - } - - @Test - public void usualAnswer() { - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {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); - } - - @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() { - Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); - } - - @Test - public void answerNoPerson() { - Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Здравствуйте, вы написали в нашу компанию!"); - Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); - } +//package org.sadtech.autoresponder; +// +//import org.junit.Assert; +//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.impl.PersonRepositoryMap; +//import org.sadtech.autoresponder.service.impl.PersonServiceImpl; +//import org.sadtech.autoresponder.service.impl.UnitServiceImpl; +// +//import java.util.ArrayList; +//import java.util.HashSet; +// +//public class AutoresponderTest { +// +// private Person person = new Person(); +// private Unit unit = new Unit(); +// private Unit unit2 = new Unit(); +// private Unit unit3 = new Unit(); +// private ArrayList units = new ArrayList<>(); +// private UnitRepositoryMap unitRepository = new UnitRepositoryMap(); +// private PersonRepositoryMap personRepository = new PersonRepositoryMap(); +// +// private UnitServiceImpl unitService = new UnitServiceImpl(unitRepository); +// private PersonServiceImpl personService = new PersonServiceImpl(personRepository); +// private Autoresponder autoresponder = new Autoresponder(unitService, personService); +// +// +// @Before +// public void before() { +// 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); +// unit.setAnswer("Здравствуйте, вы написали в нашу компанию!"); +// unit.setMatchThreshold(100); +// +// units.add(unit2); +// units.add(unit3); +// +// 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); +// personRepository.addPerson(person); +// } +// +// @Test +// public void usualAnswer() { +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {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); +// } +// +// @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() { +// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); +// } +// +// @Test +// public void answerNoPerson() { +// Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Здравствуйте, вы написали в нашу компанию!"); +// Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); +// } } \ No newline at end of file From c9851384b8b8ecc3099b4b8bacd2482f59ba7f91 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sun, 20 Jan 2019 23:02:09 +0300 Subject: [PATCH 3/5] BugFix --- src/main/java/org/sadtech/autoresponder/Autoresponder.java | 3 +-- src/test/java/org/sadtech/autoresponder/AutoresponderTest.java | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 6e6246c..46171e9 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -37,8 +37,7 @@ public class Autoresponder { if (personService.checkPerson(idPerson)) { person = personService.getPersonById(idPerson); } else { - person = new Person(); - person.setId(idPerson); + person = new Person(idPerson); personService.addPerson(person); } return person; diff --git a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java index 29879d2..b883179 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java @@ -123,4 +123,4 @@ // Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Здравствуйте, вы написали в нашу компанию!"); // Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); // } -} \ No newline at end of file +//} \ No newline at end of file From 2a057e6b67b2226267cfaf053b0c2e5b3af97bff Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Mon, 21 Jan 2019 00:35:52 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D1=83=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=B8=20=D0=BD=D0=B0=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D0=BE=D0=B9?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../compare/UnitPriorityComparator.java | 2 +- .../autoresponder/AutoresponderTest.java | 268 ++++++++++-------- 2 files changed, 143 insertions(+), 127 deletions(-) diff --git a/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java b/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java index 6ced573..41003ad 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java +++ b/src/main/java/org/sadtech/autoresponder/entity/compare/UnitPriorityComparator.java @@ -8,7 +8,7 @@ public class UnitPriorityComparator implements Comparator { @Override public int compare(Unit o1, Unit o2) { - if (o1.getPriority() > o2.getPriority()) { + if (o1.getPriority() < o2.getPriority()) { return -1; } else if (o1.getPriority().equals(o2.getPriority())) { return 0; diff --git a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java index b883179..bb12355 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java @@ -1,126 +1,142 @@ -//package org.sadtech.autoresponder; -// -//import org.junit.Assert; -//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.impl.PersonRepositoryMap; -//import org.sadtech.autoresponder.service.impl.PersonServiceImpl; -//import org.sadtech.autoresponder.service.impl.UnitServiceImpl; -// -//import java.util.ArrayList; -//import java.util.HashSet; -// -//public class AutoresponderTest { -// -// private Person person = new Person(); -// private Unit unit = new Unit(); -// private Unit unit2 = new Unit(); -// private Unit unit3 = new Unit(); -// private ArrayList units = new ArrayList<>(); -// private UnitRepositoryMap unitRepository = new UnitRepositoryMap(); -// private PersonRepositoryMap personRepository = new PersonRepositoryMap(); -// -// private UnitServiceImpl unitService = new UnitServiceImpl(unitRepository); -// private PersonServiceImpl personService = new PersonServiceImpl(personRepository); -// private Autoresponder autoresponder = new Autoresponder(unitService, personService); -// -// -// @Before -// public void before() { -// 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); -// unit.setAnswer("Здравствуйте, вы написали в нашу компанию!"); -// unit.setMatchThreshold(100); -// -// units.add(unit2); -// units.add(unit3); -// -// 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); -// personRepository.addPerson(person); -// } -// -// @Test -// public void usualAnswer() { -// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {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); -// } -// -// @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() { -// Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); -// } -// -// @Test -// public void answerNoPerson() { -// Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Здравствуйте, вы написали в нашу компанию!"); -// Assert.assertEquals(autoresponder.answer(100, "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); -// } -//} \ No newline at end of file +package org.sadtech.autoresponder; + +import org.junit.Assert; +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.service.impl.PersonServiceImpl; +import org.sadtech.autoresponder.service.impl.UnitServiceImpl; + +import java.util.*; + +public class AutoresponderTest { + + 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 TextUnitRepositoryList unitRepository = new TextUnitRepositoryList(); + private PersonRepositoryMap personRepository = new PersonRepositoryMap(); + + 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.setLevel(true); + unit.setPriority(50); + unit.setKeyWords(words); + unit.setAnswer("Здравствуйте, вы написали в нашу компанию!"); + unit.setMatchThreshold(100); + + units.add(unit2); + units.add(unit3); + + unit.setNextUnits(units); + + unit2.setAnswer("Ответ с {0} параметрами!"); + unit2.setPriority(60); + unit2.setKeyWords(words); + unit2.setMatchThreshold(100); + + unit3.setAnswer("Второй Ответ с {0} параметрами!"); + unit3.setPriority(50); + unit3.setKeyWords(words2); + unit3.setMatchThreshold(100); + + person.setUnit(unit); + + unitRepository.addUnit(unit); + unitRepository.addUnit(unit2); + personRepository.addPerson(person); + } + + @Test + public void usualAnswer() { + Unit unit = autoresponder.answer(person.getId(), "Привет это тест срабатывания"); + Assert.assertEquals(((TextUnit) unit).getAnswer(), "Ответ с {0} параметрами!"); + } + + + @Test + public void NoAnswer() { + person.setUnit(null); + autoresponder.answer(person.getId(), "Привет это срабатывания"); + } + + + @Test + public void answerByPriority() { + Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); + } + + @Test + public void answerNoPerson() { + 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 From aa227a2020a46308a26c858eb978c0aeb074443b Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Mon, 21 Jan 2019 00:40:02 +0300 Subject: [PATCH 5/5] Release-1.1.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Теперь автоответчик возвращает не строку, а абстрактный объект. --- pom.xml | 2 +- .../org/sadtech/autoresponder/entity/Unit.java | 2 +- .../autoresponder/repository/PersonRepository.java | 1 - .../autoresponder/submodule/parser/Parser.java | 14 ++++++++------ 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 2406501..46e6368 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.sadtech.autoresponder core - 1.1-SNAPSHOT + 1.1.0-RELEASE diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index eeccefc..f56602b 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -13,7 +13,7 @@ public abstract class Unit { private List nextUnits; public Unit() { - level=false; + level = false; } public Unit(Set keyWords, Integer matchThreshold, Integer priority, Boolean level, List nextUnits) { diff --git a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java index c75b018..35312f5 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java @@ -2,7 +2,6 @@ package org.sadtech.autoresponder.repository; import org.sadtech.autoresponder.entity.Person; -import java.util.Collection; import java.util.Map; public interface PersonRepository { 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);