All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jmms.testing.actions.CrudProvider Maven / Gradle / Ivy

/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://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.
 */

package jmms.testing.actions;

import jmms.core.Api;
import jmms.core.Entities;
import jmms.core.model.MetaEntity;
import jmms.core.model.MetaTestAction;
import jmms.core.model.MetaTestCase;
import jmms.core.modules.EntityModule;
import jmms.core.parser.SimpleParser;
import jmms.testing.DataGenerator;
import jmms.testing.DefaultTestStepProvider;
import jmms.testing.TestAction;
import jmms.testing.TestTarget;
import leap.core.annotation.Inject;
import leap.lang.Maps;
import leap.lang.Strings;

import java.util.Map;

public class CrudProvider extends DefaultTestStepProvider {

    public static final String TYPE = "crud";

    protected @Inject DataGenerator dg;
    protected @Inject Entities      entities;

    @Override
    public TestAction createTestAction(TestTarget target, MetaTestAction meta) {
        //exec -> {ACTION} {Entity}
        //CREATE User
        //UPDATE User
        //DELETE User
        //FIND   User

        SimpleParser parser = new SimpleParser(meta.getExec());
        String  action = parser.nextWord();
        String  name   = parser.nextWord();
        Integer rounds = null;

        if(!Strings.isEmpty(name) && Strings.isDigits(name)) {
            rounds = Integer.parseInt(name);
            name   = parser.nextWord();
        }

        if(Strings.isEmpty(name)) {
            throw new IllegalStateException("Invalid action '" + meta.getExec() + "'");
        }

        Api          api    = target.getApi();
        MetaEntity   entity = api.getMeta().getEntity(name);
        EntityModule module = entities.require(entity.getName());

        if("create".equalsIgnoreCase(action)) {
            return newCreateAction(meta, parser, target, module, rounds);
        }

        if("delete".equalsIgnoreCase(action)) {
            return newDeleteAction(meta, parser, target, module);
        }

        if("find".equalsIgnoreCase(action)) {
            return newFindAction(meta, parser, target, module);
        }

        throw new IllegalStateException("Invalid action '" + action + "' at '" + meta.getExec() + "'");
    }

    protected CrudCreateAction newCreateAction(MetaTestAction meta, SimpleParser parser, TestTarget target, EntityModule module, Integer rounds) {
        if(null == rounds) {
            rounds = 1;
        }

        boolean cascade = cascade(meta, parser);
        if(cascade) {
            return new CrudCreateAction(meta, target, module, null, cascade, rounds);
        }else {
            Map record = record("CREATE", meta, parser);
            return new CrudCreateAction(meta, target, module, record, cascade, rounds);
        }
    }

    protected CrudDeleteAction newDeleteAction(MetaTestAction meta, SimpleParser parser, TestTarget target, EntityModule module) {
        Object  id      = id("DELETE", meta, parser);
        boolean cascade = cascade(meta, parser);

        return new CrudDeleteAction(meta, target, module, id, cascade);
    }

    protected CrudFindAction newFindAction(MetaTestAction meta, SimpleParser parser, TestTarget target, EntityModule module) {
        Object id = id("FIND", meta, parser);
        return new CrudFindAction(meta, target, module, id);
    }

    protected  T record(String type, MetaTestAction meta, SimpleParser parser) {
        Object record = meta.getParam("record");
        if(null == record) {
            throw new IllegalStateException("The 'record' param must be exists for '" + type + "' action");
        }
        return (T)record;
    }

    protected Object id(String type, MetaTestAction meta, SimpleParser parser) {
        Object id = meta.getParam("id");
        if(null == id) {
            throw new IllegalStateException("The 'id' param must be exists for '" + type + "' action");
        }
        return id;
    }

    protected boolean cascade(MetaTestAction meta, SimpleParser parser) {
        if(parser.rest().equalsIgnoreCase("cascade")) {
            return true;
        }
        return Maps.getBoolean(meta.getParams(), "cascade", false);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy