org.opentestsystem.ap.ivs.service.ValidationUtilityTest.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.ap.ivs.service.ValidationUtilityTest.java

Source

/*
 * Copyright 2018 Regents of the University of California. Licensed under the Educational Community 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
 *
 * https://opensource.org/licenses/ECL-2.0
 *
 * Unless required under applicable law or agreed to in writing, software distributed under the License is distributed
 * in an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
 * specific language governing permissions and limitations under the license.
 */

package org.opentestsystem.ap.ivs.service;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.opentestsystem.ap.common.model.ItemContext;
import org.opentestsystem.ap.common.model.ValidationResult;
import org.opentestsystem.ap.ivs.TestHelper;
import org.opentestsystem.ap.ivs.models.ErrorReport;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.opentestsystem.ap.ivs.TestHelper.ITEM_ID;
import static org.opentestsystem.ap.ivs.TestHelper.ITEM_ZIP_FILE;
import static org.opentestsystem.ap.ivs.TestHelper.STIM_ID;
import static org.opentestsystem.ap.ivs.TestHelper.STIM_ZIP_FILE;

public class ValidationUtilityTest {

    private static final TestHelper testHelper = new TestHelper();

    private static final String itemRootFolderStr = "cloned-from-gitlab";

    private ValidationUtility utility;

    private ValidationService service;

    private Path baseDir;

    private Path clonedItemPath;

    private Path clonedStimPath;

    @Before
    public void setup() throws IOException {
        baseDir = Paths.get(testHelper.getItemBankProperties().getLocalBaseDir());

        clonedItemPath = baseDir.resolve(itemRootFolderStr + "/" + ITEM_ID);

        clonedStimPath = baseDir.resolve(itemRootFolderStr + "/" + STIM_ID);

        utility = new ValidationUtility(testHelper.getItemBankProperties(), testHelper.getIvsProperties());

        service = new ValidationService(null, testHelper.getIvsProperties(), null, utility,
                testHelper.getItemBankProperties());

        cleanup();
        unzipTestItem(ITEM_ZIP_FILE);
        unzipTestItem(STIM_ZIP_FILE);
    }

    @After
    public void tearDown() throws IOException {
        cleanup();
    }

    @Test
    public void itShouldParseValidationErrorReport() {
        final String errorReportFileName = testHelper.getIvsProperties().getErrorReportFileName();
        final URL errorFileUrl = Thread.currentThread().getContextClassLoader().getResource(errorReportFileName);
        final Path errorFilePath = Paths.get(errorFileUrl.getPath());
        final List<ErrorReport> errorReportList = utility.parseErrorReport(errorFilePath.getParent());

        assertThat(errorReportList).hasSize(23);

        final List<ValidationResult> validationResults = service.generateResults(errorFilePath.getParent());
        assertThat(validationResults).hasSize(21);
    }

    @Ignore
    @Test
    public void itShouldCreateValidationStructure() {
        final Path itemValidationRootPath = baseDir.resolve(TestHelper.ID_DIGIT_GENERATOR.generateTransactionId());
        final ItemContext itemContext = new ItemContext(ITEM_ID, testHelper.getAssembler(), clonedItemPath);
        final ItemContext stimContext = new ItemContext(STIM_ID, testHelper.getAssembler(), clonedStimPath);
        execute(itemValidationRootPath, itemContext, stimContext);
    }

    @Ignore
    @Test
    public void itShouldCreateValidationStructureNoStimulus() {
        final Path itemValidationRootPath = baseDir.resolve(TestHelper.ID_DIGIT_GENERATOR.generateTransactionId());
        final ItemContext itemContext = new ItemContext(ITEM_ID, testHelper.getAssembler(), clonedItemPath);
        execute(itemValidationRootPath, itemContext, null);
    }

    // ------------------------------------------------------------------------

    private void execute(final Path itemValidationRootPath, final ItemContext itemContext,
            final ItemContext stimContext) {
        final Path validationRootChild = utility.initializeValidationStructure(itemValidationRootPath);
        utility.mapItemToValidationStructure(itemContext, validationRootChild);
        utility.mapStimulusToValidationStructure(stimContext, validationRootChild);
        utility.mapGlossaryToValidationStructure(itemContext, validationRootChild);
        service.runValidation(ITEM_ID, validationRootChild);
    }

    private void unzipTestItem(final String zipFileName) throws IOException {
        final Path unzipFolder = baseDir.resolve(itemRootFolderStr);
        final URL resource = Thread.currentThread().getContextClassLoader().getResource(zipFileName);
        final Path zipPath = Paths.get(resource.getPath());
        testHelper.unzipFile(zipPath.toFile(), unzipFolder);
    }

    private void cleanup() throws IOException {
        if (baseDir.toFile().exists()) {
            FileUtils.deleteDirectory(baseDir.toFile());
        }
    }
}