Search in sources :

Example 1 with TestScenarioResource

use of jp.ossc.nimbus.service.test.TestScenario.TestScenarioResource in project nimbus by nimbus-org.

the class TestControllerService method cancelScenario.

public synchronized void cancelScenario(String scenarioId) throws Exception {
    if (currentTestScenarioGroup == null || currentTestScenario == null) {
        return;
    }
    getLogger().write("TC___00011", new Object[] { currentTestScenarioGroup.getScenarioGroupId(), scenarioId });
    try {
        if (testStubs != null) {
            for (int i = 0; i < testStubs.length; i++) {
                testStubs[i].cancelScenario();
            }
        }
        if (testEventListeners != null) {
            for (int i = 0; i < testEventListeners.length; i++) {
                testEventListeners[i].cancelScenario(scenarioId);
            }
        }
        TestScenarioGroupContext groupContext = (TestScenarioGroupContext) contextMap.get(currentTestScenarioGroup.getScenarioGroupId());
        TestScenarioContext context = groupContext.getTestScenarioContext(scenarioId);
        if (context != null) {
            TestCaseContext[] testCaseContexts = context.getTestCaseContexts();
            if (testCaseContexts != null) {
                for (int i = 0; i < testCaseContexts.length; i++) {
                    TestCase.Status caseStatus = testCaseContexts[i].getStatus();
                    if (caseStatus != null && caseStatus.getState() == TestCase.Status.STARTED) {
                        cancelTestCase(testCaseContexts[i].getTestCase().getScenarioId(), testCaseContexts[i].getTestCase().getTestCaseId());
                    }
                }
            }
        } else {
            currentTestScenario = null;
            return;
        }
        TestScenarioResource testScenarioResource = context.getTestScenario().getTestScenarioResource();
        if (testPhase == null || testScenarioResource.isExecutable(testPhase)) {
            String[] finallyActionIds = context.getTestScenario().getTestScenarioResource().getFinallyActionIds();
            TestContextImpl scenarioTestContext = (TestContextImpl) context.getScenarioTestContext();
            TestScenarioImpl.StatusImpl status = (TestScenarioImpl.StatusImpl) context.getTestScenario().getStatus();
            context.setStatus(status);
            try {
                executeAction(context, scenarioTestContext, status, finallyActionIds, true, false, false);
            } catch (NotSupportActionException e) {
                throw new TestException("This action is not support at FinallyAction of Scenario. action=" + e.getAction().getClass().getName());
            }
            TestCaseContext[] testCaseContexts = context.getTestCaseContexts();
            boolean result = context.isAllActionSuccess();
            if (result) {
                for (int i = 0; i < testCaseContexts.length; i++) {
                    if (!testCaseContexts[i].isAllActionSuccess()) {
                        result = false;
                    }
                    List actionContextList = testCaseContexts[i].getActionContextList();
                    for (int j = 0; j < actionContextList.size(); j++) {
                        TestActionContext actionContext = (TestActionContext) actionContextList.get(j);
                        Reader[] readers = actionContext.getResources();
                        if (readers != null) {
                            for (int k = 0; k < readers.length; k++) {
                                if (readers[k] instanceof RetryReader) {
                                    ((RetryReader) readers[k]).closeInner();
                                } else {
                                    readers[k].close();
                                }
                            }
                        }
                    }
                }
            }
            status.setResult(result);
            status.setState(TestScenario.Status.CANCELED);
        }
    } catch (Exception e) {
        getLogger().write("TC___00012", new Object[] { currentTestScenarioGroup.getScenarioGroupId(), scenarioId }, e);
        throw e;
    } finally {
        currentTestScenario = null;
        currentTestCase = null;
    }
}
Also used : Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) TestScenarioResource(jp.ossc.nimbus.service.test.TestScenario.TestScenarioResource) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with TestScenarioResource

use of jp.ossc.nimbus.service.test.TestScenario.TestScenarioResource in project nimbus by nimbus-org.

the class TestControllerService method startScenario.

public synchronized void startScenario(String userId, String scenarioId) throws Exception {
    getLogger().write("TC___00009", new Object[] { currentTestScenarioGroup.getScenarioGroupId(), scenarioId, userId });
    try {
        setUserId(userId);
        if (currentTestScenarioGroup == null) {
            throw new TestException("ScenarioGroup is not start.");
        }
        if (currentTestScenario != null) {
            throw new TestException("Scenario is already started. ScenarioGroupId=" + currentTestScenario.getScenarioGroupId() + " ScenarioId=" + currentTestScenario.getScenarioId() + " UserId=" + currentTestScenario.getStatus().getUserId());
        }
        String[] scenarioIds = testResourceManager.getScenarioIds(currentTestScenarioGroup.getScenarioGroupId());
        if (!Arrays.asList(scenarioIds).contains(scenarioId)) {
            throw new TestException("This ScenarioId does not exist. ScenarioGroupId=" + currentTestScenarioGroup.getScenarioGroupId() + " ScenarioId=" + scenarioId);
        }
        TestScenarioGroupContext groupContext = (TestScenarioGroupContext) contextMap.get(currentTestScenarioGroup.getScenarioGroupId());
        TestScenarioContext context = groupContext.getTestScenarioContext(scenarioId);
        if (context != null) {
            ((TestScenarioImpl) context.getTestScenario()).clearResource();
            TestScenario.Status scenarioStatus = context.getStatus();
            if (scenarioStatus.getState() == TestScenario.Status.STARTED) {
                if (scenarioStatus.getUserId() != null && !scenarioStatus.getUserId().equals(userId)) {
                    throw new TestException("TestScenario is Stated by another user. testScenarioId=" + scenarioId + " userId=" + scenarioStatus.getUserId());
                }
                cancelScenario(scenarioId);
            }
        }
        context = new TestScenarioContext();
        RecurciveSearchFile resourceDir = new RecurciveSearchFile(testResourceFileBaseDirectory, currentTestScenarioGroup.getScenarioGroupId() + File.separator + scenarioId);
        downloadTestScenarioResource(resourceDir, currentTestScenarioGroup.getScenarioGroupId(), scenarioId);
        if (testStubs != null) {
            for (int i = 0; i < testStubs.length; i++) {
                String stubId = testStubs[i].getId();
                File tempDir = null;
                try {
                    tempDir = new File(internalTestResourceFileTempDirectory, new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
                    if (!tempDir.mkdirs()) {
                        throw new IOException("Directory can not make. path=" + tempDir);
                    }
                    File[] caseDirs = resourceDir.listFiles();
                    if (caseDirs != null && caseDirs.length > 0) {
                        for (int j = 0; j < caseDirs.length; j++) {
                            RecurciveSearchFile caseDir = new RecurciveSearchFile(caseDirs[j]);
                            File[] files = caseDir.listAllTreeFiles("**/" + stubId, RecurciveSearchFile.SEARCH_TYPE_DIR);
                            if (files != null && files.length > 0) {
                                for (int k = 0; k < files.length; k++) {
                                    File testcaseDir = new File(tempDir, files[k].getParentFile().getName());
                                    if (!testcaseDir.exists()) {
                                        if (!testcaseDir.mkdirs()) {
                                            throw new IOException("Directory can not make. path=" + testcaseDir);
                                        }
                                    }
                                    if (!RecurciveSearchFile.copyAllTree(files[k], testcaseDir)) {
                                        throw new IOException("Directory can not copy. From=" + files[k] + " To=" + testcaseDir);
                                    }
                                }
                            }
                        }
                    }
                    stubResourceManager.uploadScenarioResource(tempDir, currentTestScenarioGroup.getScenarioGroupId(), scenarioId, stubId);
                    testStubs[i].startScenario(userId, currentTestScenarioGroup.getScenarioGroupId(), scenarioId);
                } finally {
                    if (tempDir != null && tempDir.exists()) {
                        if (!RecurciveSearchFile.deleteAllTree(tempDir)) {
                            RecurciveSearchFile.deleteOnExitAllTree(tempDir);
                        }
                    }
                }
            }
        }
        if (testEventListeners != null) {
            for (int i = 0; i < testEventListeners.length; i++) {
                testEventListeners[i].startScenario(userId, scenarioId);
            }
        }
        TestScenarioImpl testScenario = new TestScenarioImpl(currentTestScenarioGroup.getScenarioGroupId(), scenarioId);
        testScenario.setController(this);
        context.setTestScenario(testScenario);
        groupContext.putTestScenarioContext(context);
        TestScenarioImpl.StatusImpl status = new TestScenarioImpl.StatusImpl(userId);
        context.setStatus(status);
        TestContextImpl scenarioTestContext = new TestContextImpl();
        scenarioTestContext.setCurrentDirectory(resourceDir);
        context.setScenarioTestContext(scenarioTestContext);
        TestContextImpl testCaseTestContext = new TestContextImpl();
        context.setTestCaseTestContext(testCaseTestContext);
        scenarioTestContext.setTestScenario(testScenario);
        TestScenarioResource testScenarioResource = testScenario.getTestScenarioResource();
        if (testPhase == null || testScenarioResource.isExecutable(testPhase)) {
            String[] beforeActionIds = testScenario.getTestScenarioResource().getBeforeActionIds();
            try {
                executeAction(context, scenarioTestContext, status, beforeActionIds, true, true, true);
            } catch (NotSupportActionException e) {
                throw new TestException("This action is not support at BeforeAction of Scenario. action=" + e.getAction().getClass().getName());
            }
            boolean result = context.isAllActionSuccess();
            status.setResult(result);
            status.setStartTime(new Date());
            if (result) {
                status.setState(TestScenario.Status.STARTED);
            } else {
                status.setState(TestScenario.Status.ERROR);
            }
        }
        currentTestScenario = testScenario;
    } catch (Exception e) {
        if (testStubs != null) {
            for (int i = 0; i < testStubs.length; i++) {
                testStubs[i].cancelScenario();
            }
        }
        currentTestScenario = null;
        currentTestCase = null;
        getLogger().write("TC___00010", new Object[] { currentTestScenarioGroup.getScenarioGroupId(), scenarioId }, e);
        throw e;
    }
}
Also used : RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) TestScenarioResource(jp.ossc.nimbus.service.test.TestScenario.TestScenarioResource) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with TestScenarioResource

use of jp.ossc.nimbus.service.test.TestScenario.TestScenarioResource in project nimbus by nimbus-org.

the class TestControllerService method endScenario.

public synchronized void endScenario(String scenarioId) throws Exception {
    if (currentTestScenarioGroup == null || currentTestScenario == null) {
        return;
    }
    getLogger().write("TC___00013", new Object[] { currentTestScenarioGroup.getScenarioGroupId(), scenarioId });
    try {
        if (testStubs != null) {
            for (int i = 0; i < testStubs.length; i++) {
                testStubs[i].endScenario();
            }
        }
        if (testEventListeners != null) {
            for (int i = 0; i < testEventListeners.length; i++) {
                testEventListeners[i].endScenario(scenarioId);
            }
        }
        TestScenarioGroupContext groupContext = (TestScenarioGroupContext) contextMap.get(currentTestScenarioGroup.getScenarioGroupId());
        TestScenarioContext context = groupContext.getTestScenarioContext(scenarioId);
        if (context != null) {
            TestCaseContext[] testCaseContexts = context.getTestCaseContexts();
            if (testCaseContexts != null) {
                for (int i = 0; i < testCaseContexts.length; i++) {
                    TestCase.Status caseStatus = testCaseContexts[i].getStatus();
                    if (caseStatus != null && caseStatus.getState() == TestCase.Status.STARTED) {
                        endTestCase(testCaseContexts[i].getTestCase().getScenarioId(), testCaseContexts[i].getTestCase().getTestCaseId());
                    }
                }
            }
        } else {
            currentTestScenario = null;
            currentTestCase = null;
            return;
        }
        TestScenarioResource testScenarioResource = context.getTestScenario().getTestScenarioResource();
        if (testPhase == null || testScenarioResource.isExecutable(testPhase)) {
            TestContextImpl scenarioTestContext = (TestContextImpl) context.getScenarioTestContext();
            String[] afterActionIds = testScenarioResource.getAfterActionIds();
            TestScenarioImpl.StatusImpl status = (TestScenarioImpl.StatusImpl) context.getStatus();
            try {
                executeAction(context, scenarioTestContext, status, afterActionIds, true, true, false);
            } catch (NotSupportActionException e) {
                throw new TestException("This action is not support at AfterAction of Scenario. action=" + e.getAction().getClass().getName());
            } finally {
                String[] finallyActionIds = testScenarioResource.getFinallyActionIds();
                try {
                    executeAction(context, scenarioTestContext, status, finallyActionIds, true, false, false);
                } catch (NotSupportActionException e) {
                    throw new TestException("This action is not support at FinallyAction of Scenario. action=" + e.getAction().getClass().getName());
                }
            }
            TestCaseContext[] testCaseContexts = context.getTestCaseContexts();
            boolean result = context.isAllActionSuccess();
            if (result) {
                for (int i = 0; i < testCaseContexts.length; i++) {
                    if (!testCaseContexts[i].isAllActionSuccess()) {
                        result = false;
                    }
                    List actionContextList = testCaseContexts[i].getActionContextList();
                    for (int j = 0; j < actionContextList.size(); j++) {
                        TestActionContext actionContext = (TestActionContext) actionContextList.get(j);
                        Reader[] readers = actionContext.getResources();
                        if (readers != null) {
                            for (int k = 0; k < readers.length; k++) {
                                if (readers[k] instanceof RetryReader) {
                                    ((RetryReader) readers[k]).closeInner();
                                } else {
                                    readers[k].close();
                                }
                            }
                        }
                    }
                }
            }
            status.setResult(result);
            status.setState(TestScenario.Status.END);
            status.setEndTime(new Date());
        }
    } catch (Exception e) {
        getLogger().write("TC___00014", new Object[] { currentTestScenarioGroup.getScenarioGroupId(), scenarioId }, e);
        throw e;
    } finally {
        currentTestScenario = null;
        currentTestCase = null;
    }
}
Also used : Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) TestScenarioResource(jp.ossc.nimbus.service.test.TestScenario.TestScenarioResource)

Aggregations

IOException (java.io.IOException)3 TestScenarioResource (jp.ossc.nimbus.service.test.TestScenario.TestScenarioResource)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3 FileReader (java.io.FileReader)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 File (java.io.File)1 SimpleDateFormat (java.text.SimpleDateFormat)1 RecurciveSearchFile (jp.ossc.nimbus.io.RecurciveSearchFile)1