package be.ac.ulb.code.wit;

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

import java.text.ParseException;

import org.junit.Test;

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

public class MediaRangeTest {
	@Test
	public void testNoParamMediaRange() throws ParseException {
		MediaRange range = (new MediaRangeParser()).parse("text/html");
		assertEquals("text", range.getType());
		assertEquals("html", range.getSubType());
		assertTrue(range.getAcceptParameters().isEmpty());
		assertTrue(range.getMediaParameters().isEmpty());
	}
	@Test(expected=ParseException.class)
	public void testBadRange() throws ParseException {
		(new MediaRangeParser()).parse("text/html;");
	}
	@Test(expected=ParseException.class)
	public void testBadRange2() throws ParseException {
		(new MediaRangeParser()).parse("text-html");
	}
	@Test(expected=ParseException.class)
	public void testBadRange3() throws ParseException {
		(new MediaRangeParser()).parse("text/html= ;level=2");
	}
	@Test(expected=NumberFormatException.class)
	public void testQualityValueError() throws ParseException {
		(new MediaRangeParser()).parse("text/html;q=abc");
	}
	@Test(expected=NumberFormatException.class)
	public void testQualityValueError2() throws NumberFormatException, ParseException {
		(new MediaRangeParser()).parse("text/html;q=6");
	}
	
	@Test
	public void testParamParsing() throws ParseException {
		MediaRange range = (new MediaRangeParser()).parse("text/html;level=3;charset=UTF-8;q=0.8;ext=3");
		assertEquals("text", range.getType());
		assertEquals("html", range.getSubType());
		assertTrue(range.getMediaParameters().size() == 2);
		assertTrue(range.getAcceptParameters().size() == 2);
		
		assertTrue(range.getMediaParameters().contains(new Parameter("level", "3")));
		assertTrue(range.getMediaParameters().contains(new Parameter("charset", "UTF-8")));
		assertTrue(range.getAcceptParameters().contains(new Parameter("q", "0.8")));
		assertTrue(range.getAcceptParameters().contains(new Parameter("ext", "3")));
	}
	@Test
	public void testMediaRangeParsingQuotedString() throws ParseException {
		MediaRange range = (new MediaRangeParser()).parse("text/html;level=1;my=\"hello \\\"world\\\";\";q=0.3");
		assertTrue(range.getMediaParameters().contains(new Parameter("my", "hello \"world\";")));
	}
}
