jeudi 30 mai 2019

test spring validator organization

I Have a Controller with a request with the annotation @Valid. There are many field to validate so i realize many validator. Also custom validators. How i can organize the validation tests?

I begin with testing NotNull valid.

The Request is same this. and the nested object have Validation field inside also.

@DependsByParam public class ProductionRequestResource extends ProcedureResource implements Serializable { private static final long serialVersionUID = -5759970962934525996L;

private String operatorFiscalCode;

@NotNull(message = "{validation.notNull}")
@Size(min = Ssce2BoConst.NUN_LENGTH, max = Ssce2BoConst.NUN_LENGTH, message = "{validation.size}")
private String nun;

/**
 * Il codice Belfiore del comune.
 */
@Size(min = 4, max = 4, message = "{validation.size}")
private String issuerMunicipalityCode;

@Size(min = 6, max = 6, message = "{validation.size}")
private String issuerIstatCode;

@NotNull(message = "{validation.notNull}")
@DateConstraint(message = "{validation.invalidDate}")
private String issueDate;

@NotNull(message = "{validation.notNull}")
@DateConstraint(message = "{validation.invalidDate}")
private String expiryDate;

@NotNull(message = "{validation.notNull}")
@Size(min = Ssce2BoConst.HALF_PIN_LENGTH, max = Ssce2BoConst.HALF_PIN_LENGTH, message = "{validation.size}")
private String halfPIN;

@NotNull(message = "{validation.notNull}")
@Size(min = Ssce2BoConst.HALF_PUK_LENGTH, max = Ssce2BoConst.HALF_PUK_LENGTH, message = "{validation.size}")
private String halfPUK;

@NotNull(message = "{validation.notNull}")
@Valid
private PrintedDataResource printedData;

@NotNull(message = "{validation.notNull}")
@Valid
private HolderDataResource holderData;


@NotNull
private String issuerName;

private boolean expatriation = false;

private boolean bilingual = false;

@Valid
private BiometricDataResource biometricData;

@Valid
private ShippingDataResource shippingData;

@JsonIgnore
@AssertTrue(message = "{validation.isNotValidRange}")
public boolean isCheckRange() {
    if (expiryDate != null && issueDate != null) {
        return expiryDate.compareTo(issueDate) >= 0;
    }
    return false;
}

public String getOperatorFiscalCode() {
    return operatorFiscalCode;
}

public ProductionRequestResource setOperatorFiscalCode(String operatorFiscalCode) {
    this.operatorFiscalCode = operatorFiscalCode;
    return this;
}

public String getNun() {
    return nun;
}

public ProductionRequestResource setNun(String nun) {
    this.nun = nun;
    return this;
}

/**
 * Il codice Belfiore del comune.
 */
public String getIssuerMunicipalityCode() {
    return issuerMunicipalityCode;
}

public ProductionRequestResource setIssuerMunicipalityCode(String issuerMunicipalityCode) {
    this.issuerMunicipalityCode = issuerMunicipalityCode;
    return this;
}

public String getIssuerIstatCode() {
    return issuerIstatCode;
}

public ProductionRequestResource setIssuerIstatCode(String issuerIstatCode) {
    this.issuerIstatCode = issuerIstatCode;
    return this;
}

public String getIssuerName() {
    return issuerName;
}

public ProductionRequestResource setIssuerName(String issuerName) {
    this.issuerName = issuerName;
    return this;
}

public String getIssueDate() {
    return issueDate;
}

public ProductionRequestResource setIssueDate(String issueDate) {
    this.issueDate = issueDate;
    return this;
}

public String getExpiryDate() {
    return expiryDate;
}

public ProductionRequestResource setExpiryDate(String expiryDate) {
    this.expiryDate = expiryDate;
    return this;
}

public String getHalfPIN() {
    return halfPIN;
}

public ProductionRequestResource setHalfPIN(String halfPIN) {
    this.halfPIN = halfPIN;
    return this;
}

public String getHalfPUK() {
    return halfPUK;
}

public ProductionRequestResource setHalfPUK(String halfPUK) {
    this.halfPUK = halfPUK;
    return this;
}

public boolean isExpatriation() {
    return expatriation;
}

public ProductionRequestResource setExpatriation(boolean expatriation) {
    this.expatriation = expatriation;
    return this;
}

public boolean isBilingual() {
    return bilingual;
}

public ProductionRequestResource setBilingual(boolean bilingual) {
    this.bilingual = bilingual;
    return this;
}

public PrintedDataResource getPrintedData() {
    return printedData;
}

public ProductionRequestResource setPrintedData(PrintedDataResource printedData) {
    this.printedData = printedData;
    return this;
}

public HolderDataResource getHolderData() {
    return holderData;
}

public ProductionRequestResource setHolderData(HolderDataResource holderData) {
    this.holderData = holderData;
    return this;
}

public BiometricDataResource getBiometricData() {
    return biometricData;
}

public ProductionRequestResource setBiometricData(BiometricDataResource biometricData) {
    this.biometricData = biometricData;
    return this;
}

public ShippingDataResource getShippingData() {
    return shippingData;
}

public ProductionRequestResource setShippingData(ShippingDataResource shippingData) {
    this.shippingData = shippingData;
    return this;
}

public static PrintedDataResource newPrintedData() {
    return new PrintedDataResource();
}

public static HolderDataResource newHolderData() {
    return new HolderDataResource();
}

public static BiometricDataResource newBiometricData() {
    return new BiometricDataResource();
}

public static ShippingDataResource newShippingData() {
    return new ShippingDataResource();
}

public static ItalianShipmentResource newItalianShipment() {
    return new ItalianShipmentResource();
}

public static ForeignShipmentResource newForeignShipment() {
    return new ForeignShipmentResource();
}

public static ItalianShipmentDataResource newItalianShipmentData() {
    return new ItalianShipmentDataResource();
}

public static ForeignShipmentDataResource newForeignShipmentData() {
    return new ForeignShipmentDataResource();
}

public static FingerprintsResource newFingerprints() {
    return new FingerprintsResource();
}

public static SignatureResource newSignature() {
    return new SignatureResource();
}

}

I want organize tests in rational mode.

Aucun commentaire:

Enregistrer un commentaire