Search in sources :

Example 1 with ValidationJsonException

use of org.ligoj.bootstrap.core.validation.ValidationJsonException in project ligoj-api by ligoj.

the class ParameterValueResource method checkCompletude.

/**
 * Check optional but secure assertions.
 */
private void checkCompletude(final BasicParameterValueVo vo, final Parameter parameter) {
    Arrays.stream(new Supplier<?>[] { vo::getText, vo::getBool, vo::getDate, vo::getIndex, vo::getInteger, vo::getTags, vo::getSelections }).map(Supplier::get).filter(Objects::nonNull).skip(1).findFirst().ifPresent(e -> {
        final ValidationJsonException exception = new ValidationJsonException();
        exception.addError(parameter.getId(), "Too many values");
        throw exception;
    });
}
Also used : ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) Supplier(java.util.function.Supplier)

Example 2 with ValidationJsonException

use of org.ligoj.bootstrap.core.validation.ValidationJsonException in project ligoj-api by ligoj.

the class CurlCacheTokenTest method getTokenCache.

@Test
public void getTokenCache() {
    final Object sync = new Object();
    AtomicInteger counter = new AtomicInteger();
    Assertions.assertEquals("ok", cacheToken.getTokenCache(sync, "key", k -> {
        if (counter.incrementAndGet() == 2) {
            return "ok";
        }
        return null;
    }, 2, () -> new ValidationJsonException()));
    Assertions.assertEquals(2, counter.get());
}
Also used : Test(org.junit.jupiter.api.Test) BeforeEach(org.junit.jupiter.api.BeforeEach) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.junit.jupiter.api.Assertions) ApplicationContext(org.springframework.context.ApplicationContext) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) Test(org.junit.jupiter.api.Test)

Example 3 with ValidationJsonException

use of org.ligoj.bootstrap.core.validation.ValidationJsonException in project ligoj-api by ligoj.

the class MatcherUtilTest method assertThrowsValidationNotSameProperty.

@Test
public void assertThrowsValidationNotSameProperty() {
    final ValidationJsonException violationException = new ValidationJsonException();
    final List<Map<String, Serializable>> errors = new ArrayList<>();
    final Map<String, Serializable> error = new HashMap<>();
    error.put("rule", "any");
    errors.add(error);
    violationException.getErrors().put("any", errors);
    Assertions.assertEquals("expected: <firstName> but was: <[any]>", Assertions.assertThrows(AssertionFailedError.class, () -> {
        MatcherUtil.assertThrows(violationException, "firstName", "message");
    }).getMessage());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 4 with ValidationJsonException

use of org.ligoj.bootstrap.core.validation.ValidationJsonException in project ligoj-api by ligoj.

the class CurlCacheTokenTest method getTokenCacheFailed.

@Test
public void getTokenCacheFailed() {
    final Object sync = new Object();
    AtomicInteger counter = new AtomicInteger();
    Assertions.assertThrows(ValidationJsonException.class, () -> {
        Assertions.assertEquals("", cacheToken.getTokenCache(sync, "key", k -> {
            counter.incrementAndGet();
            return null;
        }, 2, () -> new ValidationJsonException()));
    });
    Assertions.assertEquals(2, counter.get());
    Assertions.assertEquals("ok", cacheToken.getTokenCache(sync, "key", k -> {
        if (counter.incrementAndGet() == 4) {
            return "ok";
        }
        return null;
    }, 2, () -> new ValidationJsonException()));
    Assertions.assertEquals(4, counter.get());
}
Also used : Test(org.junit.jupiter.api.Test) BeforeEach(org.junit.jupiter.api.BeforeEach) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.junit.jupiter.api.Assertions) ApplicationContext(org.springframework.context.ApplicationContext) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) Test(org.junit.jupiter.api.Test)

Example 5 with ValidationJsonException

use of org.ligoj.bootstrap.core.validation.ValidationJsonException in project ligoj-api by ligoj.

the class MatcherUtilTest method assertThrowsValidationNotSameMessage.

@Test
public void assertThrowsValidationNotSameMessage() {
    final ValidationJsonException violationException = new ValidationJsonException();
    final List<Map<String, Serializable>> errors = new ArrayList<>();
    final Map<String, Serializable> error = new HashMap<>();
    error.put("rule", "any");
    errors.add(error);
    violationException.getErrors().put("firstName", errors);
    Assertions.assertEquals("expected: <message> but was: <any>", Assertions.assertThrows(AssertionFailedError.class, () -> {
        MatcherUtil.assertThrows(violationException, "firstName", "message");
    }).getMessage());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationJsonException (org.ligoj.bootstrap.core.validation.ValidationJsonException)9 Test (org.junit.jupiter.api.Test)5 Serializable (java.io.Serializable)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AfterEach (org.junit.jupiter.api.AfterEach)2 Assertions (org.junit.jupiter.api.Assertions)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Mockito (org.mockito.Mockito)2 ApplicationContext (org.springframework.context.ApplicationContext)2 Supplier (java.util.function.Supplier)1 Subscription (org.ligoj.app.model.Subscription)1 ProvInstancePrice (org.ligoj.app.plugin.prov.model.ProvInstancePrice)1 ProvQuote (org.ligoj.app.plugin.prov.model.ProvQuote)1 ProvStorageType (org.ligoj.app.plugin.prov.model.ProvStorageType)1