package org.sagebionetworks.web.unitclient.widget.entity.registration;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

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

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.widget.entity.registration.WidgetRegistrarImpl;
import org.sagebionetworks.web.shared.WidgetConstants;

public class WidgetRegistrarImplTest {
		
	WidgetRegistrarImpl widgetRegistrar;
	PortalGinInjector mockGinInjector;
	Map<String, String> testImageWidgetDescriptor;
	String testFileName = "testfile.png";
	@Before
	public void setup(){	
		mockGinInjector = mock(PortalGinInjector.class);
		widgetRegistrar= new WidgetRegistrarImpl(mockGinInjector, new JSONObjectAdapterImpl());
		testImageWidgetDescriptor = new HashMap<String, String>();
	}
	
	@Test
	public void testCreateWidgets() {
		widgetRegistrar.getWidgetRendererForWidgetDescriptor(null, WidgetConstants.YOUTUBE_CONTENT_TYPE, null, null, null);
		verify(mockGinInjector).getYouTubeRenderer();
		widgetRegistrar.getWidgetRendererForWidgetDescriptor(null, WidgetConstants.IMAGE_CONTENT_TYPE, null, null, null);
		verify(mockGinInjector).getImageRenderer();
		widgetRegistrar.getWidgetRendererForWidgetDescriptor(null, WidgetConstants.PROVENANCE_CONTENT_TYPE, null, null, null);
		verify(mockGinInjector).getProvenanceRenderer();
	}
	@Test
	public void testCreateWidgetEditors() {
		widgetRegistrar.getWidgetEditorForWidgetDescriptor(null, WidgetConstants.YOUTUBE_CONTENT_TYPE, null, null);
		verify(mockGinInjector).getYouTubeConfigEditor();
		widgetRegistrar.getWidgetEditorForWidgetDescriptor(null, WidgetConstants.IMAGE_CONTENT_TYPE, null, null);
		verify(mockGinInjector).getImageConfigEditor();
		widgetRegistrar.getWidgetEditorForWidgetDescriptor(null, WidgetConstants.EXTERNAL_IMAGE_CONTENT_TYPE, null, null);
		verify(mockGinInjector).getExternalImageConfigEditor();
		widgetRegistrar.getWidgetEditorForWidgetDescriptor(null, WidgetConstants.PROVENANCE_CONTENT_TYPE, null, null);
		verify(mockGinInjector).getProvenanceConfigEditor();
	}
	
	@Test
	public void testGetWidgetDescriptor() throws JSONObjectAdapterException {
		//from MD representation, verify that various widget descriptors can be constructed
		Map<String, String> actualWidgetDescriptor = widgetRegistrar.getWidgetDescriptor(WidgetConstants.IMAGE_CONTENT_TYPE+"?fileName="+testFileName);
		Assert.assertEquals(testFileName, actualWidgetDescriptor.get(WidgetConstants.IMAGE_WIDGET_FILE_NAME_KEY));
	}
	
	@Test (expected=IllegalArgumentException.class)
	public void testGetWidgetDescriptorWithNull () throws JSONObjectAdapterException {
		//from MD representation, verify that various widget descriptors can be constructed
		widgetRegistrar.getWidgetDescriptor(null);
	}
	
	@Test (expected=IllegalArgumentException.class)
	public void testGetWidgetDescriptorWithEmpty() throws JSONObjectAdapterException {
		//from MD representation, verify that various widget descriptors can be constructed
		widgetRegistrar.getWidgetDescriptor("");
	}
	
	@Test
	public void testGetWidgetDescriptorNoParams() throws JSONObjectAdapterException {
		//from MD representation, verify that various widget descriptors can be constructed
		widgetRegistrar.getWidgetDescriptor(WidgetConstants.TOC_CONTENT_TYPE);
	}
	
	@Test
	public void testGetMDRepresentationDecoded() throws JSONObjectAdapterException {
		String aTestFilename = "getMDRepresentationDecodedTest.png";
		testImageWidgetDescriptor.put(WidgetConstants.IMAGE_WIDGET_FILE_NAME_KEY, aTestFilename);
		String actualResult = widgetRegistrar.getMDRepresentation(WidgetConstants.IMAGE_CONTENT_TYPE, testImageWidgetDescriptor);
		//there's a single key/value pair, so there isn't an ordering problem in this test.  If another key/value were added to the Image WidgetDescriptor, 
		//then this equality test would be fragile (since the keys are not necessarily in order)
		Assert.assertEquals(WidgetConstants.IMAGE_CONTENT_TYPE+"?fileName=getMDRepresentationDecodedTest%2Epng", actualResult);
	}	
	
}

@@ -108,16 +108,58 @@
 .mock;%0D%0A
+import static org.mockito.Mockito.times;%0D%0A
 import s
@@ -251,42 +251,8 @@
 %0D%0A%0D%0A
-import junit.framework.Assert;%0D%0A%0D%0A
 impo
@@ -646,16 +646,50 @@
 nts;%0D%0A%0D%0A
+import junit.framework.Assert;%0D%0A%0D%0A
 public c
@@ -1681,24 +1681,457 @@
 enderer();%0D%0A
+%09%09widgetRegistrar.getWidgetRendererForWidgetDescriptor(null, WidgetConstants.API_TABLE_CONTENT_TYPE, null, null, null);%0D%0A%09%09widgetRegistrar.getWidgetRendererForWidgetDescriptor(null, WidgetConstants.QUERY_TABLE_CONTENT_TYPE, null, null, null);%0D%0A%09%09widgetRegistrar.getWidgetRendererForWidgetDescriptor(null, WidgetConstants.LEADERBOARD_CONTENT_TYPE, null, null, null);%0D%0A%09%09verify(mockGinInjector, times(3)).getSynapseAPICallRenderer();%0D%0A
 %09%7D%0D%0A%09@Test%0D%0A

verify that supertable, query table, and leaderboard widgets all use the same renderer widget