From 386b2265c6d31824992b0fcd74580fdc2855878c Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Mon, 14 Jan 2019 14:09:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * В Api внедрил проверку существования юзера и его добавление * В Unit добавленно новое поле для формирвоания меню * Исправления методов доступа * Настроена логика формирования меню * Добавлен новый метод проверки существования Person * Продолжаю покрытие тестами Api --- .../sadtech/autoresponder/Autoresponder.java | 24 +++++-- .../sadtech/autoresponder/entity/Unit.java | 1 + .../repository/UnitRepository.java | 3 + .../repository/impl/PersonRepositoryImpl.java | 2 +- .../repository/impl/UnitRepositoryImpl.java | 15 ++++- .../autoresponder/service/PersonService.java | 1 + .../autoresponder/service/UnitService.java | 6 +- .../service/impl/PersonServiceImpl.java | 5 ++ .../service/impl/UnitServiceImpl.java | 15 +++-- .../submodule/parser/InsertWords.java | 2 +- .../autoresponder/AutoresponderTest.java | 64 +++++++++++++++++-- 11 files changed, 115 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 50cb85f..d895efe 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -17,15 +17,19 @@ public class Autoresponder { private PersonService personService; public String answer(@NotNull Integer idPerson, @NotNull String message) { - Person person = personService.getPersonById(idPerson); - Unit unit = person.getUnit(); - unit = unitService.nextUnit(unit, message); + Person person = addPerson(idPerson); + Unit unit; + if (person.getUnit() == null) { + unit = unitService.nextUnit(unitService.menuUnit(), message); + } else { + unit = unitService.nextUnit(person.getUnit(), message); + } person.setUnit(unit); return unit.getAnswer(); } public String answer(@NotNull Integer idPerson, @NotNull String message, @NotNull List words) { - Person person = personService.getPersonById(idPerson); + Person person = addPerson(idPerson); Unit unit = unitService.nextUnit(person.getUnit(), message); InsertWords insertWords = new InsertWords(); insertWords.setInText(unit.getAnswer()); @@ -33,4 +37,16 @@ public class Autoresponder { return insertWords.getOutText(); } + private Person addPerson(Integer idPerson) { + Person person; + if (personService.checkPerson(idPerson)) { + person = personService.getPersonById(idPerson); + } else { + person = new Person(); + person.setId(idPerson); + personService.addPerson(person); + } + return person; + } + } diff --git a/src/main/java/org/sadtech/autoresponder/entity/Unit.java b/src/main/java/org/sadtech/autoresponder/entity/Unit.java index 3e3add3..18f408c 100644 --- a/src/main/java/org/sadtech/autoresponder/entity/Unit.java +++ b/src/main/java/org/sadtech/autoresponder/entity/Unit.java @@ -14,5 +14,6 @@ public class Unit { private Integer priority; private Set keyWords; private Integer matchThreshold; + private Boolean level = false; } diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java index 2b7817e..0f4679a 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java @@ -3,6 +3,7 @@ package org.sadtech.autoresponder.repository; import org.sadtech.autoresponder.entity.Unit; import java.util.Collection; +import java.util.List; public interface UnitRepository { @@ -13,4 +14,6 @@ public interface UnitRepository { void addUnits(Collection units); void removeUnit(Unit idUnit); + + List menuUnits(); } diff --git a/src/main/java/org/sadtech/autoresponder/repository/impl/PersonRepositoryImpl.java b/src/main/java/org/sadtech/autoresponder/repository/impl/PersonRepositoryImpl.java index f6822ff..8e48c26 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/impl/PersonRepositoryImpl.java +++ b/src/main/java/org/sadtech/autoresponder/repository/impl/PersonRepositoryImpl.java @@ -7,7 +7,7 @@ import java.util.*; public class PersonRepositoryImpl implements PersonRepository { - Map people = new HashMap<>(); + private Map people = new HashMap<>(); @Override public void addPerson(Person person) { diff --git a/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryImpl.java b/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryImpl.java index 17b73a2..a9c58e0 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryImpl.java +++ b/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryImpl.java @@ -3,9 +3,7 @@ package org.sadtech.autoresponder.repository.impl; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.repository.UnitRepository; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; +import java.util.*; public class UnitRepositoryImpl implements UnitRepository { @@ -30,4 +28,15 @@ public class UnitRepositoryImpl implements UnitRepository { 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/PersonService.java b/src/main/java/org/sadtech/autoresponder/service/PersonService.java index 66b993a..da46400 100644 --- a/src/main/java/org/sadtech/autoresponder/service/PersonService.java +++ b/src/main/java/org/sadtech/autoresponder/service/PersonService.java @@ -6,5 +6,6 @@ public interface PersonService { Person getPersonById(Integer integer); void addPerson(Person person); + boolean checkPerson(Integer idPerson); } diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitService.java b/src/main/java/org/sadtech/autoresponder/service/UnitService.java index 326a1ad..42e6c4f 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitService.java @@ -1,14 +1,12 @@ package org.sadtech.autoresponder.service; -import lombok.NonNull; -import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Unit; -import java.util.List; - public interface UnitService { Unit nextUnit(Unit unit, String message); Unit getUnitById(Integer idUnit); + Unit menuUnit(); + } 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 0d41eb1..5325b22 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java @@ -19,4 +19,9 @@ public class PersonServiceImpl implements PersonService { public void addPerson(Person person) { personRepository.addPerson(person); } + + @Override + public boolean checkPerson(Integer idPerson) { + return personRepository.getPersonById(idPerson) != null; + } } 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 b209f6b..3d88777 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java @@ -3,16 +3,13 @@ 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 @@ -20,7 +17,7 @@ public class UnitServiceImpl implements UnitService { private UnitRepository unitRepository; - public Unit nextUnit(@NotNull Unit unit, @NotNull String message) { + public Unit nextUnit(Unit unit, @NotNull String message) { ArrayList nextUnits = (ArrayList) unit.getNextUnits(); if (nextUnits.size() > 0) { Parser parser = new Parser(); @@ -29,7 +26,7 @@ public class UnitServiceImpl implements UnitService { Unit unitReturn = new Unit(); unitReturn.setPriority(0); for (Unit nextUnit : nextUnits) { - if (!Collections.disjoint(nextUnit.getKeyWords(), parser.getWords()) && (nextUnit.getPriority()>unitReturn.getPriority())) { + if (!Collections.disjoint(nextUnit.getKeyWords(), parser.getWords()) && (nextUnit.getPriority() > unitReturn.getPriority())) { unitReturn = nextUnit; } } @@ -37,6 +34,7 @@ public class UnitServiceImpl implements UnitService { } else { return null; } + } @Override @@ -44,6 +42,13 @@ public class UnitServiceImpl implements UnitService { return unitRepository.getUnitById(idUnit); } + @Override + public Unit menuUnit() { + Unit menuUnit = new Unit(); + menuUnit.setNextUnits(unitRepository.menuUnits()); + return menuUnit; + } + } diff --git a/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java b/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java index 452e31f..f44889e 100644 --- a/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java +++ b/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java @@ -19,7 +19,7 @@ public class InsertWords { Matcher m = pattern.matcher(inText); // Инициализация Matcher StringBuffer result = new StringBuffer(); // Буфер для конечного значения while (m.find()) { // Проверка на совпадение - if (words.get(Integer.parseInt(m.group(1))) != null) { + if (Integer.parseInt(m.group(1)) < words.size()) { m.appendReplacement(result, words.get(Integer.parseInt(m.group(1)))); // Подставляем значение из HashMap } else { m.appendReplacement(result, m.group(0)); diff --git a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java index 5a95f98..63c2ad4 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoresponderTest.java @@ -21,7 +21,7 @@ public class AutoresponderTest { private Person person = new Person(); private Unit unit = new Unit(); private Unit unit2 = new Unit(); - private HashSet words = new HashSet<>(); + private Unit unit3 = new Unit(); private ArrayList units = new ArrayList<>(); private UnitRepositoryImpl unitRepository = new UnitRepositoryImpl(); private PersonRepositoryImpl personRepository = new PersonRepositoryImpl(); @@ -33,21 +33,36 @@ public class AutoresponderTest { @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(100); + 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); @@ -57,20 +72,59 @@ public class AutoresponderTest { } @Test - public void answer() { + public void usualAnswer() { Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания"), "Ответ с {0} параметрами!"); } @Test - public void answer1() { + public void answerOneParameter() { ArrayList words = new ArrayList<>(); words.add("одним"); Assert.assertEquals(autoresponder.answer(person.getId(), "Привет это тест срабатывания", words), "Ответ с одним параметрами!"); } @Test - public void answer2() { + public void NoAnswer() { 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