forked from eclipse-tractusx/item-relationship-service
-
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.
- Loading branch information
Showing
4 changed files
with
177 additions
and
1 deletion.
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
53 changes: 53 additions & 0 deletions
53
...a/org/eclipse/tractusx/irs/policystore/validators/BusinessPartnerNumberListValidator.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,53 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
package org.eclipse.tractusx.irs.policystore.validators; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
public class BusinessPartnerNumberListValidator | ||
implements ConstraintValidator<ValidListOfBusinessPartnerNumbers, List<String>> { | ||
|
||
public static final String BPN_REGEX = "(BPN)[LSA][\\w\\d]{10}[\\w\\d]{2}"; | ||
|
||
private static final Pattern PATTERN = Pattern.compile(BPN_REGEX); | ||
|
||
@Override | ||
public boolean isValid(List<String> value, ConstraintValidatorContext context) { | ||
if (value == null || value.isEmpty()) { | ||
return true; | ||
} | ||
|
||
for (int i = 0; i < value.size(); i++) { | ||
if (!PATTERN.matcher(value.get(i)).matches()) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate( | ||
"The business partner number at index %d is invalid (should conform to pattern '%s')".formatted( | ||
i, BPN_REGEX)).addConstraintViolation(); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...va/org/eclipse/tractusx/irs/policystore/validators/ValidListOfBusinessPartnerNumbers.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,43 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
package org.eclipse.tractusx.irs.policystore.validators; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
@Documented | ||
@Constraint(validatedBy = BusinessPartnerNumberListValidator.class) | ||
@Target({ ElementType.FIELD, | ||
ElementType.PARAMETER | ||
}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ValidListOfBusinessPartnerNumbers { | ||
String message() default "Invalid list of business partner numbers"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
} |
79 changes: 79 additions & 0 deletions
79
...g/eclipse/tractusx/irs/policystore/validators/BusinessPartnerNumberListValidatorTest.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,79 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
package org.eclipse.tractusx.irs.policystore.validators; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Answers; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class BusinessPartnerNumberListValidatorTest { | ||
|
||
public static final String VALID_BPN_1 = "BPNL1234567890AB"; | ||
public static final String VALID_BPN_2 = "BPNL123456789012"; | ||
|
||
@InjectMocks | ||
private BusinessPartnerNumberListValidator validator; | ||
|
||
@Captor | ||
private ArgumentCaptor<String> messageCaptor; | ||
|
||
@Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
private ConstraintValidatorContext contextMock; | ||
|
||
@Test | ||
void withEmptyListOfStrings() { | ||
assertThat(validator.isValid(Collections.emptyList(), contextMock)).isTrue(); | ||
} | ||
|
||
@Test | ||
void withNull() { | ||
assertThat(validator.isValid(null, contextMock)).isTrue(); | ||
} | ||
|
||
@Test | ||
void withValidListOfStrings() { | ||
List<String> validList = Arrays.asList(VALID_BPN_1, VALID_BPN_2); | ||
assertThat(validator.isValid(validList, contextMock)).isTrue(); | ||
} | ||
|
||
@Test | ||
void withListContainingInvalidBPN() { | ||
List<String> invalidList = Arrays.asList(VALID_BPN_1, "INVALID_BPN", VALID_BPN_2); | ||
assertThat(validator.isValid(invalidList, contextMock)).isFalse(); | ||
// verify(contextMock).buildConstraintViolationWithTemplate( | ||
//// startsWith("Element at index 1 does not match the pattern")); | ||
verify(contextMock).buildConstraintViolationWithTemplate(messageCaptor.capture()); | ||
assertThat(messageCaptor.getValue()).contains("BPN").contains(" index 1 ").contains("invalid"); | ||
} | ||
} |