Search in sources :

Example 6 with ValidationJsonException

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

the class MatcherUtilTest method assertThrowsValidation.

@Test
public void assertThrowsValidation() {
    final ValidationJsonException violationException = new ValidationJsonException();
    final List<Map<String, Serializable>> errors = new ArrayList<>();
    final Map<String, Serializable> error = new HashMap<>();
    error.put("rule", "message");
    errors.add(error);
    violationException.getErrors().put("firstName", errors);
    MatcherUtil.assertThrows(violationException, "firstName", "message");
}
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 7 with ValidationJsonException

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

the class ProvQuoteInstanceResource method persist.

private void persist(final InstanceUpload upload, final int subscription, String usage, final Integer ramMultiplier) {
    final QuoteInstanceEditionVo vo = new QuoteInstanceEditionVo();
    vo.setCpu(round(ObjectUtils.defaultIfNull(upload.getCpu(), 0d)));
    vo.setEphemeral(upload.isEphemeral());
    vo.setInternet(upload.getInternet());
    vo.setMaxVariableCost(upload.getMaxVariableCost());
    vo.setMaxQuantity(Optional.ofNullable(upload.getMaxQuantity()).map(q -> q <= 0 ? null : q).orElse(null));
    vo.setMinQuantity(upload.getMinQuantity());
    vo.setName(upload.getName());
    vo.setLocation(upload.getLocation());
    vo.setUsage(Optional.ofNullable(upload.getUsage()).map(u -> resource.findConfiguredByName(usageRepository, u, subscription).getName()).orElse(null));
    vo.setRam(ObjectUtils.defaultIfNull(ramMultiplier, 1) * ObjectUtils.defaultIfNull(upload.getRam(), 0).intValue());
    vo.setSubscription(subscription);
    // Find the lowest price
    final ProvInstancePrice instancePrice = validateLookup("instance", lookup(subscription, vo.getCpu(), vo.getRam(), upload.getConstant(), upload.getOs(), upload.getType(), upload.isEphemeral(), upload.getLocation(), upload.getUsage()), upload.getName());
    vo.setPrice(instancePrice.getId());
    final UpdatedCost newInstance = create(vo);
    final int qi = newInstance.getId();
    // Storage part
    final Integer size = Optional.ofNullable(upload.getDisk()).map(Double::intValue).orElse(0);
    if (size > 0) {
        // Size is provided, propagate the upload properties
        final QuoteStorageEditionVo svo = new QuoteStorageEditionVo();
        svo.setName(vo.getName());
        svo.setQuoteInstance(qi);
        svo.setSize(size);
        svo.setLatency(upload.getLatency());
        svo.setInstanceCompatible(true);
        svo.setOptimized(upload.getOptimized());
        svo.setLocation(upload.getLocation());
        // Find the nicest storage
        svo.setType(storageResource.lookup(subscription, size, upload.getLatency(), qi, upload.getOptimized(), upload.getLocation()).stream().findFirst().orElseThrow(() -> new ValidationJsonException("storage", "NotNull")).getPrice().getType().getName());
        // Default the storage name to the instance name
        svo.setSubscription(subscription);
        storageResource.create(svo);
    }
}
Also used : ProvInstancePrice(org.ligoj.app.plugin.prov.model.ProvInstancePrice) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException)

Example 8 with ValidationJsonException

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

the class ProvQuoteStorageResource method saveOrUpdate.

/**
 * Save or update the storage inside a quote.
 *
 * @param entity
 *            The storage entity to update.
 * @param vo
 *            The new quote storage data to persist.
 * @return The formal entity.
 */
private UpdatedCost saveOrUpdate(final ProvQuoteStorage entity, final QuoteStorageEditionVo vo) {
    DescribedBean.copy(vo, entity);
    // Check the associations
    final int subscription = vo.getSubscription();
    final ProvQuote quote = getQuoteFromSubscription(subscription);
    final String node = quote.getSubscription().getNode().getRefined().getId();
    entity.setConfiguration(quote);
    entity.setLocation(resource.findLocation(node, vo.getLocation()));
    entity.setPrice(findByTypeName(subscription, vo.getType(), vo.getLocation(), quote));
    entity.setInstanceCompatible(vo.getInstanceCompatible());
    entity.setLatency(vo.getLatency());
    entity.setOptimized(vo.getOptimized());
    entity.setSize(vo.getSize());
    entity.setQuoteInstance(checkInstance(node, vo.getQuoteInstance()));
    // Check the storage requirements to validate the linked price
    final ProvStorageType type = entity.getPrice().getType();
    if (!lookup(quote, entity.getSize(), entity.getLatency(), vo.getQuoteInstance(), entity.getOptimized(), vo.getLocation()).stream().map(qs -> qs.getPrice().getType()).anyMatch(type::equals)) {
        // The related storage type does not match these requirements
        throw new ValidationJsonException("type", "type-incompatible-requirements", type.getName());
    }
    // Save and update the costs
    final UpdatedCost cost = refreshCost(entity);
    Optional.ofNullable(entity.getQuoteInstance()).ifPresent(q -> cost.setRelatedCosts(Collections.singletonMap(q.getId(), qiResource.updateCost(q))));
    return cost;
}
Also used : ProvStorageType(org.ligoj.app.plugin.prov.model.ProvStorageType) ProvQuote(org.ligoj.app.plugin.prov.model.ProvQuote) ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException)

Example 9 with ValidationJsonException

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

the class VmScheduleResource method checkAndSave.

private VmSchedule checkAndSave(final int subscription, final VmScheduleVo schedule, final VmSchedule entity) {
    // Check the subscription is visible
    final Subscription subscriptionEntity = subscriptionResource.checkVisibleSubscription(subscription);
    if (schedule.getCron().split(" ").length == 6) {
        // Add the missing "seconds" part
        schedule.setCron(schedule.getCron() + " *");
    }
    // Check expressions first
    if (!CronExpression.isValidExpression(schedule.getCron())) {
        throw new ValidationJsonException("cron", "vm-cron");
    }
    // Every second is not accepted
    if (schedule.getCron().startsWith("* ")) {
        throw new ValidationJsonException("cron", "vm-cron-second");
    }
    entity.setSubscription(subscriptionEntity);
    entity.setOperation(schedule.getOperation());
    entity.setCron(schedule.getCron());
    // Persist the new schedules for each provided CRON
    repository.saveAndFlush(entity);
    return entity;
}
Also used : ValidationJsonException(org.ligoj.bootstrap.core.validation.ValidationJsonException) Subscription(org.ligoj.app.model.Subscription)

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