Skip to content

Commit

Permalink
update schemas, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NastasiyaT committed Sep 2, 2023
1 parent d39ed21 commit ba0e8df
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 107 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ compileJava {
dependencies {
implementation 'org.assertj:assertj-core:3.24.2'
implementation 'org.projectlombok:lombok:1.18.28'
implementation 'org.apache.commons:commons-lang3:3.12.0'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/hexlet/code/schemas/MapSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ public class MapSchema extends BaseSchema {

public MapSchema() {
super();
addCheck(item -> item instanceof Map<?, ?> || Objects.isNull(item));
addCheck(item -> Objects.isNull(item) || item instanceof Map<?, ?>);
}

@Override
public MapSchema required() {
super.required();
addCheck(item -> Objects.nonNull(item) && item instanceof Map<?, ?>);
return this;
}

public MapSchema sizeof(int quantity) {
addCheck(item -> Objects.nonNull(item) && ((Map<?, ?>) item).size() == quantity);
addCheck(item -> Objects.isNull(item) || ((Map<?, ?>) item).size() == quantity);
return this;
}

public MapSchema shape(Map<String, BaseSchema> schemas) {
addCheck(mapItem -> Objects.nonNull(mapItem)
&& schemas.entrySet().stream().allMatch(check -> {
addCheck(mapItem -> Objects.isNull(mapItem)
|| schemas.entrySet().stream().allMatch(check -> {
Object value = ((Map<?, ?>) mapItem).get(check.getKey());
return check.getValue().isValid(value);
}));
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/hexlet/code/schemas/NumberSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class NumberSchema extends BaseSchema {

public NumberSchema() {
super();
addCheck(item -> item instanceof Integer || Objects.isNull(item));
addCheck(item -> Objects.isNull(item) || item instanceof Integer);
}

@Override
Expand All @@ -16,12 +16,12 @@ public NumberSchema required() {
}

public NumberSchema positive() {
addCheck(item -> Objects.nonNull(item) && (Integer) item > 0);
addCheck(item -> Objects.isNull(item) || (Integer) item > 0);
return this;
}

public NumberSchema range(int from, int to) {
addCheck(item -> Objects.nonNull(item) && (Integer) item >= from && (Integer) item <= to);
addCheck(item -> Objects.isNull(item) || (Integer) item >= from && (Integer) item <= to);
return this;
}
}
10 changes: 6 additions & 4 deletions app/src/main/java/hexlet/code/schemas/StringSchema.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package hexlet.code.schemas;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

public class StringSchema extends BaseSchema {

public StringSchema() {
super();
addCheck(item -> item instanceof String || Objects.isNull(item));
addCheck(item -> Objects.isNull(item) || item instanceof String || StringUtils.isEmpty(item.toString()));
}

@Override
public StringSchema required() {
super.required();
addCheck(item -> !item.equals(""));
addCheck(item -> !StringUtils.isEmpty((String) item));
return this;
}

public StringSchema minLength(int length) {
addCheck(item -> Objects.nonNull(item) && ((String) item).length() >= length);
addCheck(item -> Objects.isNull(item) || item.equals("") || ((String) item).length() >= length);
return this;
}

public StringSchema contains(String phrase) {
addCheck(item -> Objects.nonNull(item) && ((String) item).contains(phrase));
addCheck(item -> Objects.isNull(item) || item.equals("") || ((String) item).contains(phrase));
return this;
}
}
79 changes: 47 additions & 32 deletions app/src/test/java/hexlet/code/MapSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import hexlet.code.schemas.BaseSchema;
import hexlet.code.schemas.MapSchema;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;

import java.util.Map;

Expand All @@ -14,48 +11,66 @@

public class MapSchemaTest {

private static final Object ITEMS_NULL = null;
private static final int MAP_SIZE = 3;

private MapSchema schema;
private static final Map<String, Object> ITEMS_CORRECT = Map.of(
"key1", "value1",
"key2", MAP_SIZE,
"key3", false
);

@BeforeEach
void getSchema() {
schema = new Validator().map();
private static final Map<String, Object> ITEMS_INCORRECT = Map.of(
"key1", "value1",
"key2", false
);

@Test
void testIsValid() {
MapSchema schema1 = new Validator().map();

assertTrue(schema1.isValid(ITEMS_NULL));
assertTrue(schema1.isValid(ITEMS_CORRECT));
assertFalse(schema1.isValid(MAP_SIZE));
}

@ParameterizedTest
@NullSource
void testMapSchemaNullValue(Object obj) {
assertTrue(schema.isValid(obj));
assertFalse(schema.required().isValid(obj));
assertFalse(schema.sizeof(MAP_SIZE).isValid(obj));
@Test
void testRequired() {
MapSchema schema2 = new Validator().map().required();

assertFalse(schema2.isValid(ITEMS_NULL));
assertTrue(schema2.isValid(ITEMS_CORRECT));
}

@Test
void testMapSchema() {
Map<String, Object> items1 = Map.of(
"key1", "value1",
"key2", MAP_SIZE,
"key3", false
);
void testSizeof() {
MapSchema schema3 = new Validator().map().sizeof(MAP_SIZE);

Map<String, Object> items2 = Map.of(
"key1", "value1",
"key3", false
assertTrue(schema3.isValid(ITEMS_NULL));
assertTrue(schema3.isValid(ITEMS_CORRECT));
assertFalse(schema3.isValid(ITEMS_INCORRECT));
}

@Test
void testShape() {
Map<String, BaseSchema> checksValid = Map.of(
"key1", new Validator().string().required(),
"key2", new Validator().number().positive()
);

assertTrue(schema.isValid(items1));
assertTrue(schema.required().isValid(items1));
assertTrue(schema.sizeof(MAP_SIZE).isValid(items1));
assertFalse(schema.isValid("new value"));
assertFalse(schema.sizeof(MAP_SIZE).isValid(items2));
MapSchema schema4 = new Validator().map().shape(checksValid);

Map<String, BaseSchema> checks = Map.of(
"key1", new Validator().string().required().contains("val"),
"key2", new Validator().number().positive()
assertTrue(schema4.isValid(ITEMS_NULL));
assertTrue(schema4.isValid(ITEMS_CORRECT));
assertFalse(schema4.isValid(ITEMS_INCORRECT));

Map<String, BaseSchema> checksInvalid = Map.of(
"key1", new Validator().string().required(),
"key3", new Validator().number()
);

assertTrue(schema.shape(checks).isValid(items1));
assertFalse(schema.shape(checks).isValid(items2));
MapSchema schema5 = new Validator().map().shape(checksInvalid);

assertFalse(schema5.isValid(ITEMS_CORRECT));
}
}
81 changes: 43 additions & 38 deletions app/src/test/java/hexlet/code/NumberSchemaTest.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,62 @@
package hexlet.code;

import hexlet.code.schemas.NumberSchema;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class NumberSchemaTest {

private static final int MIN_VALUE = 2;
private static final int MAX_VALUE = 90;
private static final Object ITEM_NULL = null;
private static final int ITEM_BASIC = 31;
private static final int ITEM_EXTREME = 109;
private static final int ITEM_ZERO = 0;
private static final int ITEM_NEGATIVE = -6;
private static final String ITEM_LINE = "no way";

private NumberSchema schema;
private static final int NUMBER_FROM = 2;
private static final int NUMBER_TO = 90;

@BeforeEach
void getSchema() {
schema = new Validator().number();
@Test
void testIsValid() {
NumberSchema schema1 = new Validator().number();

assertTrue(schema1.isValid(ITEM_NULL));
assertTrue(schema1.isValid(ITEM_BASIC));
assertTrue(schema1.isValid(ITEM_ZERO));
assertTrue(schema1.isValid(ITEM_NEGATIVE));
assertFalse(schema1.isValid(ITEM_LINE));
}

@ParameterizedTest
@NullSource
void testNumberSchemaNullValue(Object obj) {
assertTrue(schema.isValid(obj));
assertFalse(schema.required().isValid(obj));
assertFalse(schema.positive().isValid(obj));
assertFalse(schema.range(MIN_VALUE, MAX_VALUE).isValid(obj));
@Test
void testRequired() {
NumberSchema schema2 = new Validator().number().required();

assertFalse(schema2.isValid(ITEM_NULL));
assertTrue(schema2.isValid(ITEM_BASIC));
assertTrue(schema2.isValid(ITEM_ZERO));
assertTrue(schema2.isValid(ITEM_NEGATIVE));
}

@Test
void testPositive() {
NumberSchema schema3 = new Validator().number().positive();

assertTrue(schema3.isValid(ITEM_NULL));
assertTrue(schema3.isValid(ITEM_BASIC));
assertFalse(schema3.isValid(ITEM_ZERO));
assertFalse(schema3.isValid(ITEM_NEGATIVE));
}

@Test
void testNumberSchema() {
final int item1 = 31;
final int item2 = 109;
final int item3 = 0;
final int item4 = -6;

assertTrue(schema.isValid(item1));
assertTrue(schema.isValid(item3));
assertTrue(schema.isValid(item4));
assertFalse(schema.isValid("nice"));

assertTrue(schema.required().isValid(item1));
assertTrue(schema.required().isValid(item3));
assertTrue(schema.required().isValid(item4));

assertTrue(schema.required().positive().isValid(item1));
assertFalse(schema.required().positive().isValid(item3));
assertFalse(schema.required().positive().isValid(item4));

assertTrue(schema.required().range(MIN_VALUE, MAX_VALUE).isValid(item1));
assertFalse(schema.required().range(MIN_VALUE, MAX_VALUE).isValid(item2));
assertFalse(schema.required().range(MIN_VALUE, MAX_VALUE).isValid(item4));
void testRange() {
NumberSchema schema4 = new Validator().number().range(NUMBER_FROM, NUMBER_TO);

assertTrue(schema4.isValid(ITEM_NULL));
assertTrue(schema4.isValid(ITEM_BASIC));
assertFalse(schema4.isValid(ITEM_ZERO));
assertFalse(schema4.isValid(ITEM_NEGATIVE));
assertFalse(schema4.isValid(ITEM_EXTREME));
}
}
72 changes: 47 additions & 25 deletions app/src/test/java/hexlet/code/StringSchemaTest.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,67 @@
package hexlet.code;

import hexlet.code.schemas.StringSchema;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class StringSchemaTest {

private static final Object LINE_NULL = null;
private static final String LINE_EMPTY = "";
private static final String LINE = "Winter is coming!";
private static final int MIN_VALUE = 5;
private static final int MAX_VALUE = 25;
private static final String LINE_TRUE = " is";
private static final String LINE_FALSE = "no way";

private static StringSchema schema;
@Test
void testIsValid() {
StringSchema schema1 = new Validator().string();

@BeforeEach
void getSchema() {
schema = new Validator().string();
assertTrue(schema1.isValid(LINE));
assertTrue(schema1.isValid(LINE_NULL));
assertTrue(schema1.isValid(LINE_EMPTY));
assertFalse(schema1.isValid(MIN_VALUE));
}

@ParameterizedTest
@NullSource
@ValueSource(strings = {""})
void testStringSchemaNullValue(String obj) {
assertTrue(schema.isValid(obj));
assertFalse(schema.minLength(MIN_VALUE).isValid(obj));
assertFalse(schema.contains("nice").isValid(obj));
assertFalse(schema.required().isValid(obj));
@Test
void testRequired() {
StringSchema schema2 = new Validator().string().required();

assertTrue(schema2.isValid(LINE));
assertFalse(schema2.isValid(LINE_NULL));
assertFalse(schema2.isValid(LINE_EMPTY));
}

@Test
void testStringSchema() {
final String line = "Winter is coming!";

assertTrue(schema.isValid(line));
assertTrue(schema.required().contains(" is").minLength(MIN_VALUE).isValid(line));
assertFalse(schema.minLength(MAX_VALUE).contains("no way").isValid(line));
assertFalse(schema.minLength(MIN_VALUE).contains("no way").isValid(line));
assertFalse(schema.minLength(MAX_VALUE).contains(" is").isValid(line));
assertFalse(schema.required().contains(" is").minLength(MIN_VALUE).isValid(MAX_VALUE));
void testMinLength() {
StringSchema schema3 = new Validator().string().minLength(MIN_VALUE);

assertTrue(schema3.isValid(LINE));
assertTrue(schema3.isValid(LINE_NULL));
assertTrue(schema3.isValid(LINE_EMPTY));

StringSchema schema4 = new Validator().string().minLength(MAX_VALUE);

assertFalse(schema4.isValid(LINE));
assertTrue(schema4.isValid(LINE_NULL));
assertTrue(schema4.isValid(LINE_EMPTY));
}

@Test
void testContains() {
StringSchema schema5 = new Validator().string().contains(LINE_TRUE);

assertTrue(schema5.isValid(LINE));
assertTrue(schema5.isValid(LINE_NULL));
assertTrue(schema5.isValid(LINE_EMPTY));

StringSchema schema6 = new Validator().string().contains(LINE_FALSE);

assertFalse(schema6.isValid(LINE));
assertTrue(schema6.isValid(LINE_NULL));
assertTrue(schema6.isValid(LINE_EMPTY));
}
}

0 comments on commit ba0e8df

Please sign in to comment.