From c8dedeb4d57feb25c838468f65c1de2517ba1d41 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 22 Aug 2019 13:11:50 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=B8=20=D0=B8=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sadtech/autoresponder/AutoResponder.java | 12 +++++--- .../exception/NotFoundUnitException.java | 10 ------- .../service/UnitPointerService.java | 2 -- .../service/UnitPointerServiceImpl.java | 5 ---- .../autoresponder/util/Description.java | 2 +- .../sadtech/autoresponder/util/Message.java | 15 ++++++++++ .../sadtech/autoresponder/util/Parser.java | 13 ++++---- .../autoresponder/AutoResponderTest.java | 30 +++++++++---------- 8 files changed, 46 insertions(+), 43 deletions(-) delete mode 100644 src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java create mode 100644 src/main/java/org/sadtech/autoresponder/util/Message.java diff --git a/src/main/java/org/sadtech/autoresponder/AutoResponder.java b/src/main/java/org/sadtech/autoresponder/AutoResponder.java index e94b3c3..ee3c730 100644 --- a/src/main/java/org/sadtech/autoresponder/AutoResponder.java +++ b/src/main/java/org/sadtech/autoresponder/AutoResponder.java @@ -55,10 +55,14 @@ public class AutoResponder { !unitPointer.isPresent() || newScenario(unitPointer.get()) ? startUnits : unitPointer.get().getUnit().getNextUnits(), message ); - unitOpt.ifPresent(unit -> { - unitPointer.orElseGet(() -> unitPointerService.create(personId)); - unitPointerService.edit(personId, unit); - }); + if (unitOpt.isPresent()) { + U unit = unitOpt.get(); + if (unitPointer.isPresent()) { + unitPointerService.edit(personId, unit); + } else { + unitPointerService.add(new UnitPointer(personId, unit)); + } + } return unitOpt.isPresent() ? unitOpt : Optional.ofNullable(defaultUnit); } diff --git a/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java b/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java deleted file mode 100644 index bbab6f6..0000000 --- a/src/main/java/org/sadtech/autoresponder/exception/NotFoundUnitException.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.sadtech.autoresponder.exception; - -/** - * Исключение, которое выбрасывается, если юнит не найден. - * - * @author upagge [07/07/2019] - */ -public class NotFoundUnitException extends RuntimeException { - -} diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java index 06a55ca..307e7d6 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerService.java @@ -25,8 +25,6 @@ public interface UnitPointerService { Optional getByEntityId(@NonNull Integer entityId); - UnitPointer create(@NonNull Integer entryId); - void edit(@NonNull Integer personId, Unit unit); diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java index c608627..728bf30 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitPointerServiceImpl.java @@ -20,11 +20,6 @@ public class UnitPointerServiceImpl implements UnitPointerService { return unitPointerRepository.findByEntityId(entityId); } - @Override - public UnitPointer create(@NonNull Integer entryId) { - return unitPointerRepository.add(new UnitPointer(entryId)); - } - @Override public void edit(@NonNull Integer personId, Unit unit) { if (check(personId)) { diff --git a/src/main/java/org/sadtech/autoresponder/util/Description.java b/src/main/java/org/sadtech/autoresponder/util/Description.java index dcb42ce..b5c7642 100644 --- a/src/main/java/org/sadtech/autoresponder/util/Description.java +++ b/src/main/java/org/sadtech/autoresponder/util/Description.java @@ -13,5 +13,5 @@ import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.SOURCE) public @interface Description { - String value(); + String value() default ""; } diff --git a/src/main/java/org/sadtech/autoresponder/util/Message.java b/src/main/java/org/sadtech/autoresponder/util/Message.java new file mode 100644 index 0000000..68954b0 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/util/Message.java @@ -0,0 +1,15 @@ +package org.sadtech.autoresponder.util; + +/** + * Класс содержащий сообщения для ошибок и логера. + * + * @author upagge [22.08.2019] + */ +final class Message { + + static final String UTILITY_CLASS = "Клас утилита"; + + private Message() { + throw new IllegalStateException(UTILITY_CLASS); + } +} diff --git a/src/main/java/org/sadtech/autoresponder/util/Parser.java b/src/main/java/org/sadtech/autoresponder/util/Parser.java index ffff117..f11c9fd 100644 --- a/src/main/java/org/sadtech/autoresponder/util/Parser.java +++ b/src/main/java/org/sadtech/autoresponder/util/Parser.java @@ -1,9 +1,12 @@ package org.sadtech.autoresponder.util; import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; + +import static org.sadtech.autoresponder.util.Message.UTILITY_CLASS; /** * Разбивает строку на множество слов, удаляя предлоги. @@ -13,13 +16,11 @@ import java.util.stream.Stream; public class Parser { @Description("Множество предлогов") - private static final Set pretexts = Stream - .of("в", "без", "до", "из", "к", "на", "по", "о", "от", "перед", "при", "с", "у", "за", "над", "об", - "под", "про", "для") - .collect(Collectors.toSet()); + private static final Set pretexts = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("в", "без", "до", "из", "к", "на", "по", "о", "от", "перед", "при", "с", "у", "за", "над", "об", + "под", "про", "для"))); private Parser() { - throw new IllegalStateException("Utility Class"); + throw new IllegalStateException(UTILITY_CLASS); } /** diff --git a/src/test/java/org/sadtech/autoresponder/AutoResponderTest.java b/src/test/java/org/sadtech/autoresponder/AutoResponderTest.java index 672908c..e6318df 100644 --- a/src/test/java/org/sadtech/autoresponder/AutoResponderTest.java +++ b/src/test/java/org/sadtech/autoresponder/AutoResponderTest.java @@ -70,9 +70,9 @@ public class AutoResponderTest { @Test public void simpleAnswer() { - String message = autoresponder.answer(1, "привет это тестирвоание функциональности").get().getMessage(); + String message = autoresponder.answer(1, "привет это тестирвоание функциональности").orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("тест", message); - String message1 = autoresponder.answer(2, "привет, еще одно тестирование").get().getMessage(); + String message1 = autoresponder.answer(2, "привет, еще одно тестирование").orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("тест", message1); } @@ -81,7 +81,7 @@ public class AutoResponderTest { TestUnit defaultUnit = TestUnit.builder().message("не знаю").build(); autoresponder.setDefaultUnit(defaultUnit); - Assert.assertEquals("не знаю", autoresponder.answer(2, "пока").get().getMessage()); + Assert.assertEquals("не знаю", autoresponder.answer(2, "пока").orElseThrow(NullPointerException::new).getMessage()); } @Test @@ -91,7 +91,7 @@ public class AutoResponderTest { @Test public void regExpAnswer() { - String message = autoresponder.answer(1, "79101234567").get().getMessage(); + String message = autoresponder.answer(1, "79101234567").orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("регулярка", message); } @@ -99,9 +99,9 @@ public class AutoResponderTest { public void priorityAnswer() { autoresponder.answer(1, "привет"); autoresponder.answer(2, "привет"); - String message = autoresponder.answer(1, "как твои делишки").get().getMessage(); + String message = autoresponder.answer(1, "как твои делишки").orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("отлично", message); - String message1 = autoresponder.answer(2, "твои дела все еще хорошо?").get().getMessage(); + String message1 = autoresponder.answer(2, "твои дела все еще хорошо?").orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("хорошо", message1); } @@ -109,14 +109,14 @@ public class AutoResponderTest { public void showRegExpVsKeyWords() { autoresponder.answer(1, "привет"); autoresponder.answer(1, "дела"); - Assert.assertEquals("регулярка", autoresponder.answer(1, "89101234567").get().getMessage()); + Assert.assertEquals("регулярка", autoresponder.answer(1, "89101234567").orElseThrow(NullPointerException::new).getMessage()); } @Test public void matchThreshold() { autoresponder.answer(1, "витамин я сьем, и арбуз получу"); String message = "параметр себе задам: покушать витамин и арбуз, а вместе все это мультифрукт"; - String answer = autoresponder.answer(1, message).get().getMessage(); + String answer = autoresponder.answer(1, message).orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("победа", answer); } @@ -124,16 +124,16 @@ public class AutoResponderTest { public void generalAnswer() { TestUnit defaultUnit = TestUnit.builder().message("не знаю").build(); autoresponder.setDefaultUnit(defaultUnit); - String answer = autoresponder.answer(1, "привет это тестирование функциональности").get().getMessage(); + String answer = autoresponder.answer(1, "привет это тестирование функциональности").orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("тест", answer); - Assert.assertEquals("хорошо", autoresponder.answer(1, "как твои дела").get().getMessage()); - String answer2 = autoresponder.answer(2, "привет это тестирование функциональности").get().getMessage(); + Assert.assertEquals("хорошо", autoresponder.answer(1, "как твои дела").orElseThrow(NullPointerException::new).getMessage()); + String answer2 = autoresponder.answer(2, "привет это тестирование функциональности").orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("тест", answer2); - Assert.assertEquals("не знаю", autoresponder.answer(1, "нет").get().getMessage()); + Assert.assertEquals("не знаю", autoresponder.answer(1, "нет").orElseThrow(NullPointerException::new).getMessage()); String message = "параметр себе задам: покушать витамин и арбуз, а вместе все это мультифрукт"; - Assert.assertEquals("победа", autoresponder.answer(3, message).get().getMessage()); - Assert.assertEquals("регулярка", autoresponder.answer(1, "8912345678").get().getMessage()); - String answer3 = autoresponder.answer(1, "привет это тестирование функциональности").get().getMessage(); + Assert.assertEquals("победа", autoresponder.answer(3, message).orElseThrow(NullPointerException::new).getMessage()); + Assert.assertEquals("регулярка", autoresponder.answer(1, "8912345678").orElseThrow(NullPointerException::new).getMessage()); + String answer3 = autoresponder.answer(1, "привет это тестирование функциональности").orElseThrow(NullPointerException::new).getMessage(); Assert.assertEquals("тест", answer3); }