Skip to content

Commit

Permalink
fix: validation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eschrewe committed Sep 10, 2024
1 parent 36a399a commit 8635810
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@
import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.Payload;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.*;
import org.eclipse.tractusx.puris.backend.common.edc.domain.model.AssetType;
import org.eclipse.tractusx.puris.backend.common.util.PatternStore;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.itemstocksamm.DirectionCharacteristic;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Date;
import java.util.List;
import java.util.UUID;
Expand All @@ -56,7 +61,6 @@ public class ErpAdapterRequest {
@NotNull
private String partnerBpnl;

@NotNull
@ValidRequestType
private AssetType requestType;

Expand All @@ -77,16 +81,23 @@ public class ErpAdapterRequest {

private DirectionCharacteristic directionCharacteristic;



// AssetType validation helpers:
@Constraint(validatedBy = RequestTypeValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
private @interface ValidRequestType {
String message() default
"Request Type must be listed in SUPPORTED_TYPES";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}

private static class RequestTypeValidator implements ConstraintValidator<ValidRequestType, AssetType> {
public static class RequestTypeValidator implements ConstraintValidator<ValidRequestType, AssetType> {
@Override
public boolean isValid(AssetType value, ConstraintValidatorContext context) {
return SUPPORTED_TYPES.contains(value);
return value != null && SUPPORTED_TYPES.contains(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,66 +21,81 @@

package org.eclipse.tractusx.puris.backend.erpadapter.logic.service;

import org.assertj.core.api.Assertions;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import org.eclipse.tractusx.puris.backend.common.edc.domain.model.AssetType;
import org.eclipse.tractusx.puris.backend.erpadapter.domain.model.ErpAdapterRequest;
import org.eclipse.tractusx.puris.backend.erpadapter.domain.repository.ErpAdapterRequestRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Date;
import java.util.UUID;

@ExtendWith(MockitoExtension.class)
public class ErpAdapterRequestServiceTest {

@Mock
private ErpAdapterRequestRepository erpAdapterRequestRepository;

@Mock
private ErpAdapterRequestClient client;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@InjectMocks
private ErpAdapterRequestService erpAdapterRequestService;
@ExtendWith(MockitoExtension.class)
public class ErpAdapterRequestValidationTest {

private static final String matNbrCustomer = "MNR-7307-AU340474.002";

private static final String supplierPartnerBpnl = "BPNL1234567890ZZ";

private static final String sammVersion = "2.0";
private static Validator validator;

@BeforeEach
void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}

@ParameterizedTest
@EnumSource(AssetType.class)
public void unsupported_type_should_fail(AssetType assetType) throws Exception {
if (ErpAdapterRequest.SUPPORTED_TYPES.contains(assetType)) {
// skip supported types
Assertions.assertThat(true).isTrue();
return;
}

public void testRequestTypeValidation(AssetType type) {
// given

UUID uuid = UUID.randomUUID();
ErpAdapterRequest erpAdapterRequest = ErpAdapterRequest.builder()
.id(UUID.randomUUID())
.requestDate(new Date())
.partnerBpnl(supplierPartnerBpnl)
.id(uuid)
.ownMaterialNumber(matNbrCustomer)
.requestType(assetType)
.sammVersion(sammVersion)
.requestType(type)
.sammVersion(type.ERP_SAMMVERSION)
.build();

// when
var violations = validator.validate(erpAdapterRequest);

// then
if (!ErpAdapterRequest.SUPPORTED_TYPES.contains(type)) {
assertEquals(1, violations.size(), "Expected validation errors for unsupported type: " + type);
} else {
assertTrue(violations.isEmpty(), "No validation errors expected for supported type: " + type);
}
}

@Test
public void testRequestTypeValidation() {
// given
ErpAdapterRequest erpAdapterRequest = ErpAdapterRequest.builder()
.id(UUID.randomUUID())
.requestDate(null) // must not be null
.partnerBpnl("wrong-bpnl") // should fail regex check
.ownMaterialNumber("illegal-material-number\n") // should fail regex check
.requestType(AssetType.ITEM_STOCK_SUBMODEL)
.sammVersion(AssetType.ITEM_STOCK_SUBMODEL.ERP_SAMMVERSION)
.build();

var storedRequest = erpAdapterRequestService.create(erpAdapterRequest);
// Asset type validation should fail when trying to store request with unsupported type
Assertions.assertThat(storedRequest).isNull();
// when
var violations = validator.validate(erpAdapterRequest);

// then
assertEquals(3, violations.size());
}


}

0 comments on commit 8635810

Please sign in to comment.