<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.commonwl.view.cwl;

import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.commonwl.view.git.GitDetails;
import org.commonwl.view.workflow.Workflow;
import org.commonwl.view.workflow.WorkflowOverview;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.apache.commons.io.FileUtils.readFileToString;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CWLServiceTest {

    /**
     * RDFService for testing
     */
    private RDFService rdfService;

    @Before
    public void setUp() throws Exception {
        File packedWorkflowRdf = new File("src/test/resources/cwl/make_to_cwl/dna.ttl");
        Model workflowModel = ModelFactory.createDefaultModel();
        workflowModel.read(new ByteArrayInputStream(readFileToString(packedWorkflowRdf).getBytes()), null, "TURTLE");
        Dataset workflowDataset = DatasetFactory.create();
        workflowDataset.addNamedModel("http://localhost:3030/cwlviewer/github.com/common-workflow-language/workflows/blob/549c973ccc01781595ce562dea4cedc6c9540fe0/workflows/make-to-cwl/dna.cwl#main", workflowModel);

        Answer queryRdf = new Answer<ResultSet>() {
            @Override
            public ResultSet answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                Query query = QueryFactory.create(args[0].toString());
                try (QueryExecution qexec = QueryExecutionFactory.create(query, workflowDataset)) {
                    return ResultSetFactory.copyResults(qexec.execSelect());
                }
            }
        };

        this.rdfService = Mockito.spy(new RDFService("http://localhost:3030/cwlviewer/"));
        Mockito.doAnswer(queryRdf).when(rdfService).runQuery(anyObject());
        Mockito.doReturn(true).when(rdfService).graphExists(anyString());
    }

    /**
     * Used for expected IOExceptions for filesize limits
     */
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    /**
     * Test native loading parsing of a the LobSTR workflow CWL version draft-3
     */
    @Test
    public void parseLobSTRDraft3WorkflowNative() throws Exception {
        CWLService cwlService = new CWLService(rdfService, Mockito.mock(CWLTool.class), 5242880);
        Workflow lobSTRDraft3 = cwlService.parseWorkflowNative(
                new File("src/test/resources/cwl/lobstr-draft3/lobSTR-workflow.cwl"), null);
        testLobSTRWorkflow(lobSTRDraft3, true);
    }

    /**
     * Test native loading parsing of a the LobSTR workflow CWL version 1.0
     */
    @Test
    public void parseLobSTRv1WorkflowNative() throws Exception {
        CWLService cwlService = new CWLService(rdfService, new CWLTool(), 5242880);
        Workflow lobSTRv1 = cwlService.parseWorkflowNative(
                new File("src/test/resources/cwl/lobstr-v1/lobSTR-workflow.cwl"), null);
        testLobSTRWorkflow(lobSTRv1, true);
    }<commit_msg>Add test for parsing packed workflow natively<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.commonwl.view.cwl;

import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.commonwl.view.git.GitDetails;
import org.commonwl.view.workflow.Workflow;
import org.commonwl.view.workflow.WorkflowOverview;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.apache.commons.io.FileUtils.readFileToString;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CWLServiceTest {

    /**
     * RDFService for testing
     */
    private RDFService rdfService;

    @Before
    public void setUp() throws Exception {
        File packedWorkflowRdf = new File("src/test/resources/cwl/make_to_cwl/dna.ttl");
        Model workflowModel = ModelFactory.createDefaultModel();
        workflowModel.read(new ByteArrayInputStream(readFileToString(packedWorkflowRdf).getBytes()), null, "TURTLE");
        Dataset workflowDataset = DatasetFactory.create();
        workflowDataset.addNamedModel("http://localhost:3030/cwlviewer/github.com/common-workflow-language/workflows/blob/549c973ccc01781595ce562dea4cedc6c9540fe0/workflows/make-to-cwl/dna.cwl#main", workflowModel);

        Answer queryRdf = new Answer<ResultSet>() {
            @Override
            public ResultSet answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                Query query = QueryFactory.create(args[0].toString());
                try (QueryExecution qexec = QueryExecutionFactory.create(query, workflowDataset)) {
                    return ResultSetFactory.copyResults(qexec.execSelect());
                }
            }
        };

        this.rdfService = Mockito.spy(new RDFService("http://localhost:3030/cwlviewer/"));
        Mockito.doAnswer(queryRdf).when(rdfService).runQuery(anyObject());
        Mockito.doReturn(true).when(rdfService).graphExists(anyString());
    }

    /**
     * Used for expected IOExceptions for filesize limits
     */
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void parsePackedWorkflowNative() throws Exception {
        CWLService cwlService = new CWLService(rdfService, Mockito.mock(CWLTool.class), 5242880);
        Workflow dna = cwlService.parseWorkflowNative(
                new File("src/test/resources/cwl/make_to_cwl/dna.cwl"), "main");
        assertNotNull(dna);
        assertEquals("dna.cwl", dna.getLabel());
        assertEquals(1, dna.getInputs().size());
        assertEquals(1, dna.getOutputs().size());
        assertEquals(3, dna.getSteps().size());

    }

    /**
     * Test native loading parsing of a the LobSTR workflow CWL version draft-3
     */
    @Test
    public void parseLobSTRDraft3WorkflowNative() throws Exception {
        CWLService cwlService = new CWLService(rdfService, Mockito.mock(CWLTool.class), 5242880);
        Workflow lobSTRDraft3 = cwlService.parseWorkflowNative(
                new File("src/test/resources/cwl/lobstr-draft3/lobSTR-workflow.cwl"), null);
        testLobSTRWorkflow(lobSTRDraft3, true);
    }

    /**
     * Test native loading parsing of a the LobSTR workflow CWL version 1.0
     */
    @Test
    public void parseLobSTRv1WorkflowNative() throws Exception {
        CWLService cwlService = new CWLService(rdfService, new CWLTool(), 5242880);
        Workflow lobSTRv1 = cwlService.parseWorkflowNative(
                new File("src/test/resources/cwl/lobstr-v1/lobSTR-workflow.cwl"), null);
        testLobSTRWorkflow(lobSTRv1, true);
    }
{'fraction_non_alphanumeric': 0.09887451351635637, 'fraction_numerical': 0.01430524876406858, 'mean_word_length': 3.595456742387627, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6957179', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2761, 'n_words': 655}