Skip to content

Commit

Permalink
OGRN: Check value ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
valery1707 committed Jul 24, 2017
1 parent 95691de commit 7a651e7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 17 deletions.
19 changes: 19 additions & 0 deletions src/main/java/name/valery1707/validator/Checker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package name.valery1707.validator;

public final class Checker {
public static byte checkRange(byte value, int min, int max, String name) {
if (value < min || value > max) {
throw new IllegalArgumentException(String.format("%s must be between %d and %d", name, min, max));
} else {
return value;
}
}

public static int checkRange(int value, int min, int max, String name) {
if (value < min || value > max) {
throw new IllegalArgumentException(String.format("%s must be between %d and %d", name, min, max));
} else {
return value;
}
}
}
17 changes: 5 additions & 12 deletions src/main/java/name/valery1707/validator/russian/inn/InnInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static name.valery1707.validator.Checker.checkRange;

@SuppressWarnings("WeakerAccess")
public class InnInfo {
/**
Expand Down Expand Up @@ -32,18 +34,9 @@ public InnInfo(int subject, int localTax, int id, boolean juridical) {
}

public InnInfo(byte subject, byte localTax, int id, boolean juridical) {
if (subject < 0 || subject > 99) {
throw new IllegalArgumentException("Subject must be between 1 and 99");
}
if (localTax < 0 || localTax > 99) {
throw new IllegalArgumentException("Local tax must be between 1 and 99");
}
if (id < 0 || ((juridical && id > 99999) || id > 999999)) {
throw new IllegalArgumentException("Local tax must be between 1 and 99");
}
this.subject = subject;
this.localTax = localTax;
this.id = id;
this.subject = checkRange(subject, 0, 99, "Subject");
this.localTax = checkRange(localTax, 0, 99, "Local tax");
this.id = checkRange(id, 0, juridical ? 99999 : 999999, "ID");
this.juridical = juridical;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static name.valery1707.validator.Checker.checkRange;

/**
* ОГРН - Основной Государственный Регистрационный Номер
* <p/>
Expand Down Expand Up @@ -53,11 +55,11 @@ public class OgrnInfo {
* @param juridical ОГРН/ОГРНИП
*/
private OgrnInfo(byte type, byte year, byte subject, byte tax, int id, boolean juridical) {
this.type = type;
this.year = year;
this.subject = subject;
this.tax = tax;
this.id = id;
this.type = checkRange(type, 0, 9, "Type");
this.year = checkRange(year, 0, 99, "Year");
this.subject = checkRange(subject, 0, 99, "Subject");
this.tax = juridical ? checkRange(tax, 0, 99, "Tax") : tax;
this.id = checkRange(id, 1, juridical ? 99999 : 999999999, "ID");
this.juridical = juridical;
}

Expand Down
111 changes: 111 additions & 0 deletions src/test/java/name/valery1707/validator/russian/ogrn/OgrnInfoTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
package name.valery1707.validator.russian.ogrn;

import name.valery1707.validator.russian.inn.InnInfo;
import org.junit.Test;

import static name.valery1707.validator.russian.ogrn.OgrnInfo.parse;
import static org.assertj.core.api.Assertions.assertThat;

public class OgrnInfoTest {

@Test(expected = IllegalArgumentException.class)
public void testParseInvalid_null() throws Exception {
InnInfo.parse(null);
}

@Test(expected = IllegalArgumentException.class)
public void testParseInvalid_length() throws Exception {
InnInfo.parse("123");
}

@Test(expected = IllegalArgumentException.class)
public void testParseInvalid_format() throws Exception {
InnInfo.parse("qwerty1234567");
}

@Test(expected = IllegalArgumentException.class)
public void testParseInvalid_crc() throws Exception {
InnInfo.parse("1234567890123");
}

@Test
public void testParseValid_juridical() throws Exception {
OgrnInfo info = parse("1037739010891");
Expand Down Expand Up @@ -34,4 +55,94 @@ public void testFormat() throws Exception {
assertThat(parse("1037739010891").format()).isEqualTo("1037739010891");
assertThat(parse("304500116000157").format()).isEqualTo("304500116000157");
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_type_min() throws Exception {
new OgrnInfo(-1, 1, 1, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_type_max() throws Exception {
new OgrnInfo(100, 1, 1, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_year_min() throws Exception {
new OgrnInfo(1, -1, 1, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_year_max() throws Exception {
new OgrnInfo(1, 100, 1, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_subject_min() throws Exception {
new OgrnInfo(1, 1, -1, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_subject_max() throws Exception {
new OgrnInfo(1, 1, 100, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_tax_min() throws Exception {
new OgrnInfo(1, 1, 1, -1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_tax_max() throws Exception {
new OgrnInfo(1, 1, 1, 100, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_id_min() throws Exception {
new OgrnInfo(1, 1, 1, 1, 0);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrn_id_max() throws Exception {
new OgrnInfo(1, 1, 1, 1, 99999 + 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrnip_type_min() throws Exception {
new OgrnInfo(-1, 1, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrnip_type_max() throws Exception {
new OgrnInfo(100, 1, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrnip_year_min() throws Exception {
new OgrnInfo(1, -1, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrnip_year_max() throws Exception {
new OgrnInfo(1, 100, 1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrnip_subject_min() throws Exception {
new OgrnInfo(1, 1, -1, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrnip_subject_max() throws Exception {
new OgrnInfo(1, 1, 100, 1);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrnip_id_min() throws Exception {
new OgrnInfo(1, 1, 1, 0);
}

@Test(expected = IllegalArgumentException.class)
public void testCreateOgrnip_id_max() throws Exception {
new OgrnInfo(1, 1, 1, 999999999 + 1);
}
}

0 comments on commit 7a651e7

Please sign in to comment.