forked from Hexlet/hexlet-correction
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hexlet#260] unified error display format and add instancio, faker de…
…pendency for tests
- Loading branch information
Showing
18 changed files
with
249 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/java/io/hexlet/typoreporter/config/TestConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.hexlet.typoreporter.config; | ||
|
||
import net.datafaker.Faker; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@Configuration | ||
public class TestConfig { | ||
@Bean | ||
public Faker getFaker() { | ||
return new Faker(); | ||
} | ||
@Bean | ||
public ObjectMapper getObjectMapper() { | ||
return new ObjectMapper(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/io/hexlet/typoreporter/test/factory/AccountModelGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.hexlet.typoreporter.test.factory; | ||
|
||
import io.hexlet.typoreporter.web.model.SignupAccountModel; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.Getter; | ||
import net.datafaker.Faker; | ||
import org.instancio.Instancio; | ||
import org.instancio.Model; | ||
import org.instancio.Select; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.Serializable; | ||
|
||
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_INCORRECT_EMAIL; | ||
|
||
@Component | ||
@Getter | ||
public class AccountModelGenerator { | ||
private Model<SignupAccountModel> correctAccountModel; | ||
private Model<SignupAccountModel> incorrectAccountModel; | ||
@Autowired | ||
private Faker faker; | ||
|
||
@PostConstruct | ||
public void init() { | ||
final String password = faker.internet().password(8, 20); | ||
final String incorrectPassword = faker.internet().password(1, 7); | ||
correctAccountModel = Instancio.of(SignupAccountModel.class) | ||
.supply(Select.field(SignupAccountModel::getUsername), () -> faker.name().firstName()) | ||
.supply(Select.field(SignupAccountModel::getEmail), () -> faker.internet().emailAddress()) | ||
.supply(Select.field(SignupAccountModel::getPassword), () -> password) | ||
.supply(Select.field(SignupAccountModel::getConfirmPassword), () -> password) | ||
.supply(Select.field(SignupAccountModel::getFirstName), () -> faker.name().firstName()) | ||
.supply(Select.field(SignupAccountModel::getLastName), () -> faker.name().lastName()) | ||
.toModel(); | ||
incorrectAccountModel = Instancio.of(SignupAccountModel.class) | ||
.supply(Select.field(SignupAccountModel::getUsername), () -> faker.name().firstName() + ".") | ||
.supply(Select.field(SignupAccountModel::getEmail), () -> ACCOUNT_INCORRECT_EMAIL) | ||
.supply(Select.field(SignupAccountModel::getPassword), () -> incorrectPassword) | ||
.supply(Select.field(SignupAccountModel::getConfirmPassword), () -> incorrectPassword) | ||
.supply(Select.field(SignupAccountModel::getFirstName), () -> "") | ||
.supply(Select.field(SignupAccountModel::getLastName), () -> "") | ||
.toModel(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/io/hexlet/typoreporter/utils/BundleSourceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.hexlet.typoreporter.utils; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Locale; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@Component | ||
public class BundleSourceUtils { | ||
@Autowired | ||
private MessageSource messageSource; | ||
|
||
/** | ||
* @param key - Ключ из messages_LANG.properties, по которому необходимо получить значение | ||
* Для RU необходимо использовать new Locale("ru") | ||
* @param locale - Параметр, по которому определяется язык (локализация) | ||
* @param isReplaceToQuot - Признак необходимости замены " на " | ||
* @param args - Аргументы с типом String, которые необходимо подставить при интерполяции | ||
* @return Строка из resource bundle изменениями | ||
*/ | ||
public String getValueByKey(String key, Locale locale, boolean isReplaceToQuot, String[] args) { | ||
String value = messageSource.getMessage(key, null, locale); | ||
if (args != null) { | ||
Pattern pattern = Pattern.compile("(\\$\\{(\\w+)\\})|(\\{(\\w+)\\})"); | ||
Matcher matcher = pattern.matcher(value); | ||
while (matcher.find()) { | ||
value = value.replace(matcher.group(), "%s"); | ||
} | ||
value = isReplaceToQuot | ||
? value.replace("\"", """) | ||
: value; | ||
return String.format(value, args); | ||
} | ||
return isReplaceToQuot ? value.replace("\"", """) : value; | ||
} | ||
|
||
/** | ||
* @param key - Ключ из messages_LANG.properties, по которому необходимо получить значение | ||
* @param isReplaceToQuot - Признак необходимости замены " на " | ||
* @param args - Аргументы с типом String, которые необходимо подставить при интерполяции | ||
* @return - Строка из resource bundle изменениями, по умолчанию на английском языке | ||
*/ | ||
public String getValueByKey(String key, boolean isReplaceToQuot, String[] args) { | ||
return getValueByKey(key, Locale.ENGLISH, isReplaceToQuot, args); | ||
} | ||
|
||
/** | ||
* @param key - Ключ из messages_LANGUAGE.properties, по которому необходимо получить значение. | ||
* @param isReplaceToQuot - Признак необходимости замены " на " | ||
* @return - Строка из resource bundle изменениями, по умолчанию на английском языке | ||
*/ | ||
public String getValueByKey(String key, boolean isReplaceToQuot) { | ||
return getValueByKey(key, isReplaceToQuot, null); | ||
} | ||
|
||
/** | ||
* @param key - Ключ из messages_LANG.properties, по которому необходимо получить значение | ||
* @param args - Аргументы с типом String, которые необходимо подставить при интерполяции | ||
* @return - Строка из resource bundle, по умолчанию на английском языке | ||
*/ | ||
public String getValueByKey(String key, String[] args) { | ||
return getValueByKey(key, false, args); | ||
} | ||
|
||
/** | ||
* @param key - Ключ из messages_LANG.properties, по которому необходимо получить значение | ||
* @return - Строка из resource bundle, по умолчанию на английском языке | ||
*/ | ||
public String getValueByKey(String key) { | ||
return getValueByKey(key, null); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/io/hexlet/typoreporter/utils/ModelUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.hexlet.typoreporter.utils; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@Component | ||
public class ModelUtils { | ||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
public MultiValueMap<String, String> toFormParams(Object dto) throws Exception { | ||
return toFormParams(dto, Set.of()); | ||
} | ||
|
||
public MultiValueMap<String, String> toFormParams(Object dto, Set<String> excludeFields) throws Exception { | ||
ObjectReader reader = objectMapper.readerFor(Map.class); | ||
Map<String, String> map = reader.readValue(objectMapper.writeValueAsString(dto)); | ||
|
||
MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>(); | ||
map.entrySet().stream() | ||
.filter(e -> !excludeFields.contains(e.getKey())) | ||
.forEach(e -> multiValueMap.add(e.getKey(), (e.getValue() == null ? "" : e.getValue()))); | ||
return multiValueMap; | ||
} | ||
} |
Oops, something went wrong.