package com.hida.controller;

import com.hida.model.Purl;
import com.hida.service.ResolverService;
import java.util.Map;
import org.json.JSONObject;
import org.mockito.InjectMocks;
import static org.mockito.Matchers.any;
import org.mockito.Mock;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.MockitoAnnotations;
import org.springframework.web.servlet.ModelAndView;
import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;

/**
 * Tests the functionality of ResolverController to see if it is capable of
 * performing REST calls.
 *
 * @author lruffin
 */
public class ResolverControllerTest {

    @Mock
    private ResolverService Service;

    @InjectMocks
    private ResolverController Controller;

    @BeforeClass
    public void setUpClass() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    /**
     * Test the retrieve REST call
     * @throws Exception
     */
    @Test
    public void testRetrieve() throws Exception{
        Purl entity = getSamplePurl();
        when(Service.retrieveModel(any(String.class))).thenReturn(entity);

        // test to see that the correct view is returned
        ModelAndView mav = Controller.retrieve("");
        Assert.assertEquals("retrieve", mav.getViewName());

        // test to see that Service at least makes a call to get a Purl object
        verify(Service, atLeastOnce()).retrieveModel(any(String.class));

        // test to see that Json is formated properly
        Map<String, Object> map = mav.getModel();
        String jsonObject = (String) map.get("purl");
        testJsonObject(jsonObject, entity);
    }

    /**
     * Test the edit REST call
     * @throws Exception
     */
    @Test
    public void testEdit() throws Exception{
        // create purl entites to reflect the change that happens after calling edit
        Purl entity = getSamplePurl();       
                
        // pretend the entity was edited and return new entity
        when(Service.editURL(any(String.class), any(String.class))).thenReturn(true);
        when(Service.retrieveModel(any(String.class))).thenReturn(entity);
        
        // test to see that the correct view is returned
        ModelAndView mav = Controller.retrieve("");
        Assert.assertEquals("retrieve", mav.getViewName());
        
        // test to see that Json is formated properly
        Map<String, Object> map = mav.getModel();
        String jsonObject = (String) map.get("purl");
        testJsonObject(jsonObject, entity);
    }

    @Test
    public void testInsert() {
        Assert.fail("unimplemented");
    }

    @Test
    public void testDelete() {
        Assert.fail("unimplemented");
    }

    @Test
    public void testHandleGeneralError() {
        Assert.fail("unimplemented");
    }

    /**
     * Tests the given jsonObject to ensure that it matches the given entity
     *
     * @param jsonObject Json object to test
     * @param entity Entity to check against
     */
    private void testJsonObject(String jsonObject, Purl entity) {
        JSONObject testObject = new JSONObject(jsonObject);

        String pid = testObject.getString("pid");
        String url = testObject.getString("url");
        String erc = testObject.getString("erc");
        String who = testObject.getString("who");
        String what = testObject.getString("what");
        String time = testObject.getString("date");

        Assert.assertEquals(entity.getIdentifier(), pid);
        Assert.assertEquals(entity.getURL(), url);
        Assert.assertEquals(entity.getERC(), erc);
        Assert.assertEquals(entity.getWho(), who);
        Assert.assertEquals(entity.getWhat(), what);
        Assert.assertEquals(entity.getDate(), time);

    }

    /**
     * Gets a sample Purl object for testing
     *
     * @return sample Purl object
     */
    private Purl getSamplePurl() {
        Purl purl = new Purl("samplePid",
                "sampleURL",
                "sampleERC",
                "sampleWho",
                "sampleWhat",
                "sampleTime");

        return purl;
    }
}

@@ -1042,32 +1042,39 @@
 rieve REST call%0A
+     *%0A
      * @throws E
@@ -1139,32 +1139,33 @@
 throws Exception
+ 
 %7B%0A        Purl e
@@ -1832,24 +1832,31 @@
 t REST call%0A
+     *%0A
      * @thro
@@ -1929,16 +1929,17 @@
 xception
+ 
 %7B%0A      
@@ -1960,18 +1960,17 @@
 rl entit
-es
+y
  to refl
@@ -2058,32 +2058,9 @@
 l();
-       %0A                
+%0A
 %0A   
@@ -2280,24 +2280,16 @@
 ntity);%0A
-        
 %0A       
@@ -2372,33 +2372,33 @@
  Controller.
-retrieve(
+edit(%22%22, 
 %22%22);%0A       
@@ -2415,32 +2415,28 @@
 sertEquals(%22
-retrieve
+edit
 %22, mav.getVi
@@ -2446,24 +2446,16 @@
 ame());%0A
-        
 %0A       
@@ -2664,80 +2664,993 @@
     
-@Test%0A    public void testInsert() %7B%0A        Assert.fail(%22unimplemented%22
+/**%0A     * Tests the insert REST call%0A     *%0A     * @throws Exception%0A     */%0A    @Test%0A    public void testInsert() throws Exception %7B%0A        // create purl entites to reflect the change that happens after calling edit%0A        Purl entity = getSamplePurl();%0A%0A        // pretend the entity was edited and return new entity%0A        when(Service.insertPURL(any(Purl.class))).thenReturn(true);%0A        %0A        // call insert%0A        ModelAndView mav = Controller.insert(entity.getIdentifier(),%0A                entity.getURL(),%0A                entity.getERC(),%0A                entity.getWho(),%0A                entity.getWhat(),%0A                entity.getDate());%0A        %0A        // test to see that the correct view is returned%0A        Assert.assertEquals(%22insert%22, mav.getViewName());%0A%0A        // test to see that Json is formated properly%0A        Map%3CString, Object%3E map = mav.getModel();%0A        String jsonObject = (String) map.get(%22purl%22);%0A        testJsonObject(jsonObject, entity
 );%0A 

Complete testInsert