package com.altamiracorp.lumify.core.ontology;

import com.altamiracorp.lumify.core.model.ontology.Concept;
import com.altamiracorp.lumify.core.model.ontology.OntologyRepository;
import com.altamiracorp.lumify.core.model.ontology.PropertyType;
import com.altamiracorp.lumify.core.user.User;
import com.altamiracorp.securegraph.Graph;
import com.altamiracorp.securegraph.Vertex;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(MockitoJUnitRunner.class)
public class BaseOntologyTest {
    @Mock
    User user;
    @Mock
    Concept rootConcept;
    @Mock
    Concept artifactConcept;
    @Mock
    Concept imageConcept;
    @Mock
    Concept entityConcept;
    @Mock
    Vertex entityVertex;
    @Mock
    OntologyRepository ontologyRepository;
    @Mock
    Graph graph;

    private BaseOntology baseOntology;

    @Before
    public void setUp() {
        baseOntology = new BaseOntology(ontologyRepository, graph);
        when(entityConcept.getVertex()).thenReturn(entityVertex);
    }

    @Test
    public void testDefineOntology() {
        when(ontologyRepository.getOrCreateConcept((Concept) isNull(), eq("rootConcept"), eq("rootConcept"))).thenReturn(rootConcept);
        when(ontologyRepository.getOrCreateConcept(eq(rootConcept), anyString(), eq("Entity"))).thenReturn(entityConcept);

        baseOntology.defineOntology(user);
    }

    @Test
    public void testIsOntologyDefinedTrue() {
        when(ontologyRepository.getConceptById(ontologyRepository.ROOT_CONCEPT_IRI.toString())).thenReturn(rootConcept);
        boolean result = baseOntology.isOntologyDefined(user);
        assertEquals(true, result);
    }

    @Test
    public void testIsOntologyDefinedFalse() {
        when(ontologyRepository.getConceptById(ontologyRepository.ROOT_CONCEPT_IRI.toString())).thenReturn(null);
        boolean result = baseOntology.isOntologyDefined(user);
        assertEquals(false, result);
    }

    @Test(expected = RuntimeException.class)
    public void testIsOntologyDefinedException() {
        when(ontologyRepository.getConceptById(ontologyRepository.ROOT_CONCEPT_IRI.toString())).thenThrow(new RuntimeException("test", new Throwable("testing exception")));
        baseOntology.isOntologyDefined(user);
    }

    @Test
    public void testIsOntologyDefinedExceptionWithFalse() {
        when(ontologyRepository.getConceptById(OntologyRepository.ROOT_CONCEPT_IRI)).thenThrow(new RuntimeException("ontologyTitle", new Throwable("testing exception")));
        assertFalse(baseOntology.isOntologyDefined(user));
    }

    @Test
    public void testInitializeWhenUndefined() {
        when(ontologyRepository.getConceptById(OntologyRepository.ROOT_CONCEPT_IRI)).thenReturn(null);
        when(ontologyRepository.getOrCreateConcept((Concept) isNull(), eq("rootConcept"), eq("rootConcept"))).thenReturn(rootConcept);
        when(ontologyRepository.getOrCreateConcept(eq(rootConcept), anyString(), eq("Entity"))).thenReturn(entityConcept);

        baseOntology.initialize(user);
    }

    @Test
    public void testInitializeWhenDefined() {
        when(ontologyRepository.getConceptById(OntologyRepository.ROOT_CONCEPT_IRI)).thenReturn(rootConcept);
        baseOntology.initialize(user);
    }
}

@@ -160,26 +160,32 @@
 Ontology
-Repository
+LumifyProperties
 ;%0Aimport
@@ -233,20 +233,26 @@
 ogy.
-PropertyType
+OntologyRepository
 ;%0Aim
@@ -668,91 +668,8 @@
 .*;%0A
-import static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A
 impo
@@ -1417,37 +1417,59 @@
 sNull(), eq(
-%22rootConcept%22
+OntologyRepository.ROOT_CONCEPT_IRI
 ), eq(%22rootC
@@ -1459,39 +1459,32 @@
 T_IRI), eq(%22root
-Concept
 %22))).thenReturn(
@@ -1565,39 +1565,68 @@
 ncept), 
-anyString(
+eq(OntologyRepository.ENTITY_CONCEPT_IRI
 ), eq(%22
-Entity
+thing
 %22))).the
@@ -2773,23 +2773,56 @@
 ion(
-%22o
+O
 ntology
-Title%22
+LumifyProperties.ONTOLOGY_TITLE.getKey()
 , ne
@@ -3158,29 +3158,51 @@
 l(), eq(
-%22rootConcept%22
+OntologyRepository.ROOT_CONCEPT_IRI
 ), eq(%22r
@@ -3204,23 +3204,16 @@
 eq(%22root
-Concept
 %22))).the
@@ -3306,31 +3306,60 @@
 t), 
-anyString(
+eq(OntologyRepository.ENTITY_CONCEPT_IRI
 ), eq(%22
-Entity
+thing
 %22)))

fix BaseOntologyTest