package com.altamiracorp.lumify.web.routes.vertex;

import com.altamiracorp.lumify.core.model.audit.AuditRepository;
import com.altamiracorp.lumify.core.model.graph.GraphRepository;
import com.altamiracorp.lumify.core.model.graph.GraphVertex;
import com.altamiracorp.lumify.core.model.graph.InMemoryGraphVertex;
import com.altamiracorp.lumify.core.model.ontology.OntologyRepository;
import com.altamiracorp.lumify.core.model.ontology.Property;
import com.altamiracorp.lumify.core.model.ontology.PropertyName;
import com.altamiracorp.lumify.core.model.ontology.VertexType;
import com.altamiracorp.lumify.web.routes.RouteTestBase;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class VertexDeletePropertyTest extends RouteTestBase {
    private final String AUTHOR = "foo";
    private final String TYPE = VertexType.ENTITY.toString();
    private final String SUBTYPE = "28";
    private VertexDeleteProperty vertexDeleteProperty;

    @Mock
    private OntologyRepository mockOntologyRepository;
    @Mock
    private GraphRepository mockGraphRepository;
    @Mock
    private AuditRepository mockAuditRepository;
    @Mock
    private Property mockProperty;

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
        vertexDeleteProperty = new VertexDeleteProperty(mockOntologyRepository, mockGraphRepository, mockAuditRepository);
    }

    @Test(expected = RuntimeException.class)
    public void testHandleWithNullProperty() throws Exception {
        when(mockRequest.getAttribute("graphVertexId")).thenReturn("");
        when(mockRequest.getParameter("propertyName")).thenReturn("");
        when(mockOntologyRepository.getProperty(PropertyName.TITLE.toString(), mockUser)).thenReturn(null);
        vertexDeleteProperty.handle(mockRequest, mockResponse, mockHandlerChain);
    }

    @Test
    public void testHandle() throws Exception {
        when(mockRequest.getParameter("propertyName")).thenReturn(PropertyName.TITLE.toString());
        when(mockOntologyRepository.getProperty(PropertyName.TITLE.toString(), mockUser)).thenReturn(mockProperty);
        when(mockRequest.getAttribute("graphVertexId")).thenReturn("40004");

        GraphVertex vertex = new InMemoryGraphVertex("40004");
        vertex.setProperty(PropertyName.TITLE.toString(), "testVertex");

        Map<String, String> properties = new HashMap<String, String>();
        properties.put(PropertyName.AUTHOR.toString(), AUTHOR);
        properties.put(PropertyName.SUBTYPE.toString(), SUBTYPE);
        properties.put(PropertyName.TYPE.toString(), TYPE);

        when(mockGraphRepository.findVertex(vertex.getId(), mockUser)).thenReturn(vertex);
        when(mockGraphRepository.getVertexProperties(vertex.getId(), mockUser)).thenReturn(properties);

        vertexDeleteProperty.handle(mockRequest, mockResponse, mockHandlerChain);

        JSONObject response = new JSONObject(responseStringWriter.getBuffer().toString());
        JSONObject responseProperties = response.getJSONObject("properties");
        assertEquals(PropertyName.TITLE.toString(), response.getString("deletedProperty"));
        assertEquals(vertex.getId(), response.getJSONObject("vertex").getString("graphVertexId"));
        assertTrue(responseProperties.length() > 0);
        assertEquals(AUTHOR, responseProperties.getString(PropertyName.AUTHOR.toString()));
        assertEquals(SUBTYPE, responseProperties.getString(PropertyName.SUBTYPE.toString()));
        assertEquals(TYPE, responseProperties.getString(PropertyName.TYPE.toString()));
    }
}

@@ -45,16 +45,77 @@
 ertex;%0A%0A
+import com.altamiracorp.lumify.core.model.audit.AuditAction;%0A
 import c
@@ -1036,20 +1036,103 @@
 Mockito.
-when
+times;%0Aimport static org.mockito.Mockito.when;%0Aimport static org.mockito.Mockito.verify
 ;%0A%0A@RunW
@@ -4034,21 +4034,179 @@
 oString()));
+%0A%0A        verify(mockAuditRepository, times(1)).auditEntityProperties(AuditAction.DELETE.toString(), vertex, PropertyName.TITLE.toString(), %22%22, %22%22, mockUser);
 %0A    %7D%0A%7D%0A

add audit test