package com.unidev;

import com.unidev.polycms.web.WebPolyCore;
import com.unidev.polydata.SQLiteStorage;
import com.unidev.polydata.SQLiteStorageException;
import com.unidev.polydata.domain.BasicPoly;
import com.unidev.polyembeddedcms.PolyConstants;
import com.unidev.polyembeddedcms.PolyCore;
import com.unidev.polyembeddedcms.PolyRecord;
import com.unidev.polyembeddedcms.TagsPolyMigrator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProjectnameApplicationTests {

	@Autowired
	private WebPolyCore webPolyCore;

	@Autowired
	private PolyCore polyCore;

	private File root;
	private String domain = "test.com";
	private File dbFile;

	@Before
	public void init() {
		root = polyCore.fetchStorageRoot(domain);
		root.mkdirs();

		dbFile = new File(root, PolyConstants.DB_FILE);
		dbFile.deleteOnExit();
	}

	@Test
	public void contextLoads() {

	}

	@Test
	public void testTagsListing() throws MalformedURLException, SQLiteStorageException {
		SQLiteStorage sqLiteStorage = new SQLiteStorage(dbFile.getAbsolutePath());
		sqLiteStorage.setPolyMigrators(Arrays.asList(new TagsPolyMigrator()));

		PolyRecord tagTomato = new PolyRecord()._id("tomato").label("Tomato").count(100);
		sqLiteStorage.save(PolyConstants.TAGS_POLY, tagTomato);

		PolyRecord tagPotato = new PolyRecord()._id("potato").label("Potato").count(10);
		sqLiteStorage.save(PolyConstants.TAGS_POLY, tagPotato);


		HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
		Mockito.doReturn(new StringBuffer("http://" + domain)).when(httpServletRequest).getRequestURL();

		List<BasicPoly> list = webPolyCore.fetchTags(httpServletRequest);
		assertThat(list, is(not(nullValue())));
		assertThat(list.size(), is(2));

		BasicPoly first = list.get(0);

		assertThat(first.get(PolyConstants.COUNT_KEY), is(100));
		assertThat(first._id(), is("tomato"));
	}

	@Test
	public void testPolyLoading() {

	}

}

@@ -221,123 +221,24 @@
 poly
-embeddedcms.PolyConstants;%0Aimport com.unidev.polyembeddedcms.PolyCore;%0Aimport com.unidev.polyembeddedcms.PolyRecord
+data.domain.Poly
 ;%0Aim
@@ -269,32 +269,17 @@
 ddedcms.
-TagsPolyMigrator
+*
 ;%0Aimport
@@ -747,16 +747,39 @@
 Arrays;%0A
+import java.util.Date;%0A
 import j
@@ -2368,16 +2368,22 @@
 oid test
+Single
 PolyLoad
@@ -2388,17 +2388,1022 @@
 ading() 
-%7B
+throws SQLiteStorageException %7B%0A%09%09SQLiteStorage sqLiteStorage = new SQLiteStorage(dbFile.getAbsolutePath());%0A%09%09sqLiteStorage.setPolyMigrators(Arrays.asList(new DataPolyMigrator()));%0A%0A%09%09String postId = %22post_1%22;%0A%0A%09%09PolyRecord data = new PolyRecord()._id(postId).label(%22Tomato%22).category(%22Test%22).tags(%22tag1, tag2%22).date(new Date());%0A%09%09sqLiteStorage.save(PolyConstants.DATA_POLY, data);%0A%0A%09%09HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);%0A%09%09Mockito.doReturn(new StringBuffer(%22http://%22 + domain)).when(httpServletRequest).getRequestURL();%0A%0A%09%09PolyRecord poly = webPolyCore.fetchPoly(postId, httpServletRequest);%0A%0A%09%09assertThat(poly, is(not(nullValue())));%0A%09%09assertThat(poly._id(), is(postId));%0A%09%09assertThat(poly.date(), is(notNullValue()));%0A%0A%09%09Mockito.doReturn(new StringBuffer(%22http://%22 + domain)).when(httpServletRequest).getRequestURL();%0A%09%09PolyRecord notExistingPoly = webPolyCore.fetchPoly(%22not-existing-id%22, httpServletRequest);%0A%09%09assertThat(notExistingPoly, is(nullValue()));
 %0A%0A%09%7D%0A%0A%7D%0A

Test case for single poly fetching