package be.ac.ulb.code.wit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.text.ParseException;

import org.junit.Test;

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

public class MediaRangeMatcherTest {
	RangeMatcher<MediaRange> matcher = 
		new RangeMatcher<MediaRange>(new MediaRangeParser());
	RangeMatcher<MediaRange> emptyMatcher = 
		new RangeMatcher<MediaRange>(new MediaRangeParser());
	
	public MediaRangeMatcherTest() throws ParseException {
		matcher.registerKnownType("text/html;level=2");
		matcher.registerKnownType("text/html");
		matcher.registerKnownType("text/plain");
	}
	
	@Test
	public void testUnmatchables() throws ParseException {
		assertNull(matcher.getBestMatch("application/json"));
		assertNull(matcher.getBestMatch("text/html;level=3"));
		assertNull(matcher.getBestMatch("text/foo"));
	}
	
	@Test
	public void testUnmatchablesStar() throws ParseException {
		assertNull(matcher.getBestMatch("application/*"));
		assertNull(emptyMatcher.getBestMatch("*/*"));
	}
	
	@Test
	public void testMatchableStar() throws ParseException {
		assertNotNull(matcher.getBestMatch("text/*"));
		assertNotNull(matcher.getBestMatch("*/*"));
	}
	
	@Test
	public void testMatchables() throws ParseException {
		assertEquals("text/html", matcher.getBestMatch("text/html").toString());
		assertEquals("text/html;level=2", matcher.getBestMatch("text/html;level=2").toString());
		assertEquals("text/plain", matcher.getBestMatch("text/plain").toString());
	}
	
	@Test
	public void testQuality() throws ParseException {
		assertEquals("text/html",
			matcher.getBestMatch("text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5").toString());
		assertEquals("text/html",
			matcher.getBestMatch("text/*;q=0.9, text/plain;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.8").toString());
		assertEquals("text/plain","" +
			matcher.getBestMatch("text/*;q=0.9, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.8").toString());
	}
}
