package com.phoenixnap.oss.ramlapisync.generation.rules.pojogen;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.phoenixnap.oss.ramlapisync.data.ApiBodyMetadata;
import com.phoenixnap.oss.ramlapisync.naming.SchemaHelper;
import com.sun.codemodel.writer.SingleStreamCodeWriter;

/**
 * Tests relating to POJO Generation
 * 
 * @author kurtpa
 * @since 0.5.2
 */
public class JsonSchema2PojoTest {
	
	protected Logger logger = Logger.getLogger(this.getClass());
	
	@Before
    public void before() {
    }
	
	@After
    public void after() {
    }
	
	@Test
    public void schemaHelper_ExtractsName_fromId() throws Exception {
		URL url = Resources.getResource("pojogen\\B.json");
		String text = Resources.toString(url, Charsets.UTF_8);
        String nameFromSchema = SchemaHelper.extractNameFromSchema(text, "Not", "Even");
        
        assertEquals("B",nameFromSchema);
        
    }
	
	@Test
    public void schemaHelper_ExtractsName_schemaName_noId() throws Exception {
		URL url = Resources.getResource("pojogen\\B-noid.json");
		String text = Resources.toString(url, Charsets.UTF_8);
        String nameFromSchema = SchemaHelper.extractNameFromSchema(text, "Not", "Even");
        
        assertEquals("Not",nameFromSchema);
        
    }
	
	@Test
    public void schemaHelper_ExtractsName_schemaName_badId() throws Exception {
		URL url = Resources.getResource("pojogen\\B-badid.json");
		String text = Resources.toString(url, Charsets.UTF_8);
        String nameFromSchema = SchemaHelper.extractNameFromSchema(text, "Not", "Even");
        
        assertEquals("Not",nameFromSchema);
        
    }
	
	@Test
    public void schemaHelper_ExtractsName_fallback() throws Exception {
		URL url = Resources.getResource("pojogen\\B-noid.json");
		String text = Resources.toString(url, Charsets.UTF_8);
        String nameFromSchema = SchemaHelper.extractNameFromSchema(text, "1Not", "Even");
        
        assertEquals("Even",nameFromSchema);
        
    }
	
	@Test
    public void schemaHelper_ExtractsPojo() throws Exception {
		URL url = Resources.getResource("pojogen\\B.json");
		String text = Resources.toString(url, Charsets.UTF_8);
        ApiBodyMetadata mapSchemaToPojo = SchemaHelper.mapSchemaToPojo(null, text, "com.test", "Fallback", null);
        
        assertNotNull(mapSchemaToPojo);
        
    }
	
	@Test
    public void schemaHelper_ExtractsPojo_NestedUsingClasspath() throws Exception {
		URL url = Resources.getResource("pojogen\\A.json");
		String text = Resources.toString(url, Charsets.UTF_8);
        ApiBodyMetadata mapSchemaToPojo = SchemaHelper.mapSchemaToPojo(null, text, "com.test", "Fallback", null);
        assertNotNull(mapSchemaToPojo);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        
        try {
        	 mapSchemaToPojo.getCodeModel().build(new SingleStreamCodeWriter(bos));
        	 logger.debug(bos.toString());
        } catch (IOException e) {
            assertThat(e.getMessage(), is(nullValue()));
        }
    }
	
	@Test
    public void schemaHelper_ExtractsPojo_NestedUsingFile() throws Exception {
		String resourceName = "pojogen"+File.separator+"A.json";
		URL url = Resources.getResource(resourceName);
		String text = Resources.toString(url, Charsets.UTF_8);
		String path = "file:" + URLDecoder.decode(url.getPath(), Charsets.UTF_8.name()).replace(resourceName, "");
        ApiBodyMetadata mapSchemaToPojo = SchemaHelper.mapSchemaToPojo(null, text, "com.test", "Fallback", path);
        assertNotNull(mapSchemaToPojo);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        
        try {
        	 mapSchemaToPojo.getCodeModel().build(new SingleStreamCodeWriter(bos));
        	 logger.debug(bos.toString());
        } catch (IOException e) {
            assertThat(e.getMessage(), is(nullValue()));
        }
    }
    

}

@@ -1048,39 +1048,98 @@
 ter() %7B%0A    %7D%0A%09%0A
+%09private static String path = %22pojogen%22+ File.separator;%0A%09%0A
 %09@Test%0A
-
     public void 
@@ -1222,34 +1222,32 @@
 getResource(
-%22pojogen%5C%5C
+path + %22
 B.json%22);%0A%09%09
@@ -1574,34 +1574,32 @@
 getResource(
-%22pojogen%5C%5C
+path + %22
 B-noid.json%22
@@ -1938,26 +1938,24 @@
 esource(
-%22pojogen%5C%5C
+path + %22
 B-badid.
@@ -2291,26 +2291,24 @@
 esource(
-%22pojogen%5C%5C
+path + %22
 B-noid.j
@@ -2636,26 +2636,24 @@
 esource(
-%22pojogen%5C%5C
+path + %22
 B.json%22)
@@ -3020,18 +3020,16 @@
 rce(
-%22pojogen%5C%5C
+path + %22
 A.js
@@ -3677,33 +3677,15 @@
 e = 
-%22pojogen%22+File.separator+
+path + 
 %22A.j

Fix for Unix Paths