/**
 * Copyright (c) 2015 Chiral Behaviors, LLC, all rights reserved.
 * 
 
 * This file is part of Ultrastructure.
 *
 *  Ultrastructure is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  ULtrastructure is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with Ultrastructure.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.chiralbehaviors.CoRE.workspace.dsl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;

import com.chiralbehaviors.CoRE.meta.models.AbstractModelTest;
import com.chiralbehaviors.CoRE.meta.workspace.DatabaseBackedWorkspace;
import com.chiralbehaviors.CoRE.meta.workspace.dsl.WorkspaceImporter;
import com.chiralbehaviors.CoRE.product.Product;

/**
 * @author hhildebrand
 *
 */
public class TestImport extends AbstractModelTest {

    @Test
    public void testExampleWorkspace() throws Exception {
        WorkspaceImporter importer = WorkspaceImporter.manifest(getClass().getResourceAsStream("/thing.wsp"),
                                                                model);
        em.flush();
        DatabaseBackedWorkspace workspace = new DatabaseBackedWorkspace(importer.getWorkspace()
                                                                                .getDefiningProduct(),
                                                                        model);
        assertNotNull(workspace);
        assertNotNull(workspace.getScope()
                               .lookup("kernel", "IsA"));
    }

    @Test
    public void testIncrementalVersionUpdate() throws Exception {
        // load version 1
        WorkspaceImporter importer = WorkspaceImporter.manifest(getClass().getResourceAsStream("/thing.wsp"),
                                                                model);
        em.flush();
        // load version 1

        importer = WorkspaceImporter.manifest(getClass().getResourceAsStream("/thing.2.wsp"),
                                              model);
        DatabaseBackedWorkspace workspace = new DatabaseBackedWorkspace(importer.getWorkspace()
                                                                                .getDefiningProduct(),
                                                                        model);
        assertNotNull(workspace);
        assertNotNull(workspace.getScope()
                               .lookup("TheDude"));
        Product definingProduct = workspace.getDefiningProduct();
        assertEquals(2, definingProduct.getVersion());
        assertEquals("Phantasm Demo V2", definingProduct.getName());
        assertEquals("Test of Workspace versioning",
                     definingProduct.getDescription());
    }
}

@@ -961,16 +961,48 @@
 it.Test;
+%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1130,16 +1130,82 @@
 kspace;%0A
+import com.chiralbehaviors.CoRE.meta.workspace.WorkspaceAccessor;%0A
 import c
@@ -1463,32 +1463,83 @@
 ows Exception %7B%0A
+        Product definingProduct;%0A        try %7B%0A    
         Workspac
@@ -1688,32 +1688,36 @@
                 
+    
 model);%0A        
@@ -1720,115 +1720,335 @@
     
-em.flush();%0A        DatabaseBackedWorkspace workspace = new DatabaseBackedWorkspace(importer.getWorkspac
+    definingProduct = importer.getWorkspace()%0A                                      .getDefiningProduct();%0A        %7D catch (IllegalStateException e) %7B%0A            LoggerFactory.getLogger(TestImport.class)%0A                         .info(%22Not loading thing ontology version 1: %7B%7D%22,%0A                               e.getMessag
 e()
+);
 %0A   
@@ -2060,57 +2060,133 @@
     
-                                         
+definingProduct = em.find(Product.class,%0A                                      WorkspaceAccessor.uuidOf(THING_URI));%0A
         
     
@@ -2177,32 +2177,35 @@
 ));%0A        
+%7D%0A%0A
         
            .
@@ -2192,32 +2192,101 @@
         
-           .getD
+em.flush();%0A        DatabaseBackedWorkspace workspace = new DatabaseBackedWorkspace(d
 efiningP
@@ -2283,34 +2283,32 @@
 (definingProduct
-()
 ,%0A              
@@ -2603,62 +2603,25 @@
     
-// load version 1%0A        WorkspaceImporter importer =
+try %7B%0A           
  Wor
@@ -2733,40 +2733,246 @@
     
-                         model);
+model);%0A        %7D catch (IllegalStateException e) %7B%0A            LoggerFactory.getLogger(TestImport.class)%0A                         .info(%22Not loading thing ontology version 1: %7B%7D%22,%0A                               e.getMessage());%0A        %7D
 %0A   
@@ -3012,17 +3012,17 @@
 version 
-1
+2
 %0A%0A      
@@ -3023,16 +3023,34 @@
         
+WorkspaceImporter 
 importer
@@ -3119,32 +3119,50 @@
 /thing.2.wsp%22),%0A
+                  
                 

fix transactionality around thing ontology.