* Тест для парсера текста

* Тест для главного модуля
This commit is contained in:
Mark Struchkov 2019-01-14 01:49:01 +03:00
parent a8a12bb3f8
commit f48f069129
8 changed files with 126 additions and 6 deletions

View File

@ -24,6 +24,7 @@
<lombok.ver>1.18.4</lombok.ver> <lombok.ver>1.18.4</lombok.ver>
<log4j>1.2.17</log4j> <log4j>1.2.17</log4j>
<junit.ver>4.12</junit.ver> <junit.ver>4.12</junit.ver>
<mocito.ver>2.23.4</mocito.ver>
</properties> </properties>
<dependencies> <dependencies>
@ -47,6 +48,12 @@
<version>${junit.ver}</version> <version>${junit.ver}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mocito.ver}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>

View File

@ -19,7 +19,9 @@ public class Autoresponder {
public String answer(@NotNull Integer idPerson, @NotNull String message) { public String answer(@NotNull Integer idPerson, @NotNull String message) {
Person person = personService.getPersonById(idPerson); Person person = personService.getPersonById(idPerson);
Unit unit = person.getUnit(); 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<String> words) { public String answer(@NotNull Integer idPerson, @NotNull String message, @NotNull List<String> words) {

View File

@ -13,5 +13,6 @@ public class Unit {
private String answer; private String answer;
private Integer priority; private Integer priority;
private Set<String> keyWords; private Set<String> keyWords;
private Integer matchThreshold;
} }

View File

@ -1,14 +1,15 @@
package org.sadtech.autoresponder.service.impl; package org.sadtech.autoresponder.service.impl;
import lombok.AllArgsConstructor;
import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Person;
import org.sadtech.autoresponder.repository.PersonRepository; import org.sadtech.autoresponder.repository.PersonRepository;
import org.sadtech.autoresponder.service.PersonService; import org.sadtech.autoresponder.service.PersonService;
@AllArgsConstructor
public class PersonServiceImpl implements PersonService { public class PersonServiceImpl implements PersonService {
private PersonRepository personRepository; private PersonRepository personRepository;
@Override @Override
public Person getPersonById(Integer integer) { public Person getPersonById(Integer integer) {
return personRepository.getPersonById(integer); return personRepository.getPersonById(integer);

View File

@ -2,14 +2,19 @@ package org.sadtech.autoresponder.service.impl;
import com.sun.istack.internal.NotNull; import com.sun.istack.internal.NotNull;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.sadtech.autoresponder.entity.Person;
import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.entity.Unit;
import org.sadtech.autoresponder.repository.UnitRepository; import org.sadtech.autoresponder.repository.UnitRepository;
import org.sadtech.autoresponder.service.UnitService; import org.sadtech.autoresponder.service.UnitService;
import org.sadtech.autoresponder.submodule.parser.Parser; import org.sadtech.autoresponder.submodule.parser.Parser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
@Log4j
@AllArgsConstructor @AllArgsConstructor
public class UnitServiceImpl implements UnitService { public class UnitServiceImpl implements UnitService {
@ -17,13 +22,14 @@ public class UnitServiceImpl implements UnitService {
public Unit nextUnit(@NotNull Unit unit, @NotNull String message) { public Unit nextUnit(@NotNull Unit unit, @NotNull String message) {
ArrayList<Unit> nextUnits = (ArrayList<Unit>) unit.getNextUnits(); ArrayList<Unit> nextUnits = (ArrayList<Unit>) unit.getNextUnits();
Unit unitReturn = new Unit();
if (nextUnits.size() > 0) { if (nextUnits.size() > 0) {
Parser parser = new Parser(); Parser parser = new Parser();
parser.setText(message); parser.setText(message);
parser.parse(); parser.parse();
Unit unitReturn = new Unit();
unitReturn.setPriority(0);
for (Unit nextUnit : nextUnits) { 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; unitReturn = nextUnit;
} }
} }

View File

@ -3,6 +3,7 @@ package org.sadtech.autoresponder.submodule.parser;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -10,7 +11,7 @@ import java.util.regex.Pattern;
public class Parser { public class Parser {
@Getter @Getter
private Set<String> words; private Set<String> words = new HashSet<>();
@Setter @Setter
private String text; private String text;
@ -19,7 +20,7 @@ public class Parser {
Pattern p = Pattern.compile("[а-яА-Я0-9]+"); Pattern p = Pattern.compile("[а-яА-Я0-9]+");
Matcher m = p.matcher(text); Matcher m = p.matcher(text);
while (m.find()) { while (m.find()) {
words.add(m.group()); words.add(m.group().toLowerCase());
} }
} }

View File

@ -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<String> words = new HashSet<>();
private ArrayList<Unit> 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<String> 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);
}
}

View File

@ -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<String> words = new HashSet<>();
@Test
public void parse() {
parser.setText("Проверка парсера на правильность");
parser.parse();
words.add("проверка");
words.add("парсера");
words.add("на");
words.add("правильность");
Assert.assertEquals(parser.getWords(), words);
}
}