package be.ac.ulb.code.wit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.text.ParseException;
import java.util.List;

import org.junit.Test;

import be.ac.ulb.code.wit.rangeparser.MediaRange;
import be.ac.ulb.code.wit.rangeparser.RangeParser.MediaRangeParser;

public class MediaQueryParsingTest {
	private MediaRangeParser parser = new MediaRangeParser();
	
	@Test
	public void testSimplequery() throws ParseException {
		List<MediaRange> parsed = 
			parser.parseMediaQuery("text/html");
		
		assertEquals(1, parsed.size());
		assertEquals("text", parsed.get(0).getType());
		assertEquals("html", parsed.get(0).getSubType());
		assertEquals((Double)1.0, parsed.get(0).getQuality());
		assertTrue(parsed.get(0).getMediaParameters().isEmpty());
		assertTrue(parsed.get(0).getAcceptParameters().isEmpty());
	}

	@Test
	public void testManySimpleQueries() throws ParseException {
		List<MediaRange> parsed = parser.parseMediaQuery(
			"text/html,text/plain,application/json");
		
		assertEquals(3, parsed.size());
		assertEquals("text", parsed.get(0).getType());
		assertEquals("html", parsed.get(0).getSubType());
		assertEquals("text", parsed.get(1).getType());
		assertEquals("plain", parsed.get(1).getSubType());
		assertEquals("application", parsed.get(2).getType());
		assertEquals("json", parsed.get(2).getSubType());
	}
	
	@Test
	public void testNull() throws ParseException {
		List<MediaRange> parsed = parser.parseMediaQuery(
			"text/html,,application/json");
		
		assertEquals(2, parsed.size());
		assertEquals("text", parsed.get(0).getType());
		assertEquals("html", parsed.get(0).getSubType());
		assertEquals("application", parsed.get(1).getType());
		assertEquals("json", parsed.get(1).getSubType());
	}
	
	@Test(expected=ParseException.class)
	public void testParseError() throws ParseException {
		parser.parseMediaQuery(
			"text/html;,,application/json");
	}
	
	@Test(expected=ParseException.class)
	public void testParseError2() throws ParseException {
		parser.parseMediaQuery(
			"text html;,,application/json");
	}
	
	@Test
	public void testParameters() throws ParseException {
		List<MediaRange> parsed = parser.parseMediaQuery(
			"text/html;level=1;q=0.1, application/json;param=value");
		
		assertEquals(2, parsed.size());
		assertEquals("text", parsed.get(0).getType());
		assertEquals("html", parsed.get(0).getSubType());
		assertEquals("application", parsed.get(1).getType());
		assertEquals("json", parsed.get(1).getSubType());
		assertEquals(1, parsed.get(0).getAcceptParameters().size());
		assertEquals(1, parsed.get(0).getMediaParameters().size());
		assertEquals(1, parsed.get(1).getMediaParameters().size());
	}
}
