/*
 * Copyright 2020 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.request;

import static org.junit.Assert.*;

import org.junit.Test;

import com.b2international.commons.options.Options;

/**
 * @since 7.5.1
 */
public class SearchResourceRequestTest {

	public enum OptionKey {
		
		SPECIAL
		
	}
	
	@Test
	public void nullSpecialOptionKey() throws Exception {
		final Options options = Options.builder().build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, null);
		assertTrue(options == actual);
	}
	
	@Test
	public void specialOptionKeyWithNoValue() throws Exception {
		final Options options = Options.builder().build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, OptionKey.SPECIAL);
		assertTrue(options == actual);
	}
	
	@Test
	public void specialOptionKeyWithNonExpressionValue() throws Exception {
		final Options options = Options.builder()
				.put(OptionKey.SPECIAL, TermFilter.defaultTermMatch("not an expression"))
				.build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, OptionKey.SPECIAL);
		assertTrue(options == actual);
	}
	
	@Test
	public void specialOptionKeyWithExpressionValueIncorrectSyntaxOnlyLeadingChar() throws Exception {
		final Options options = Options.builder()
				.put(OptionKey.SPECIAL, TermFilter.defaultTermMatch("@"))
				.build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, OptionKey.SPECIAL);
		assertTrue(options == actual);
	}
	
	@Test
	public void specialOptionKeyWithExpressionValueIncorrectSyntaxNoValueBracket() throws Exception {
		final Options options = Options.builder()
				.put(OptionKey.SPECIAL, TermFilter.defaultTermMatch("@field"))
				.build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, OptionKey.SPECIAL);
		assertTrue(options == actual);
	}
	
	@Test
	public void specialOptionKeyWithExpressionValueIncorrectSyntaxNoValue() throws Exception {
		final Options options = Options.builder()
				.put(OptionKey.SPECIAL, TermFilter.defaultTermMatch("@field()"))
				.build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, OptionKey.SPECIAL);
		assertTrue(options == actual);
	}
	
	@Test
	public void specialOptionKeyWithExpressionValue() throws Exception {
		final Options options = Options.builder()
				.put(OptionKey.SPECIAL, TermFilter.defaultTermMatch("@field(value)"))
				.build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, OptionKey.SPECIAL);
		assertEquals(Options.builder()
				.put("FIELD", "value")
				.build(), actual);
	}
	
	@Test
	public void specialOptionKeyWithExpressionValueOtherOptionKeys() throws Exception {
		final Options options = Options.builder()
				.put(OptionKey.SPECIAL, TermFilter.defaultTermMatch("@field(value)"))
				.put("OTHER", "filter")
				.build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, OptionKey.SPECIAL);
		assertEquals(Options.builder()
				.put("FIELD", "value")
				.put("OTHER", "filter")
				.build(), actual);
	}
	
	@Test
	public void specialOptionKeyWithExtraParenthesis() throws Exception {
		final Options options = Options.builder()
				.put(OptionKey.SPECIAL, TermFilter.defaultTermMatch("@field(value (extra))"))
				.build();
		final Options actual = SearchResourceRequest.processSpecialOptionKey(options, OptionKey.SPECIAL);
		assertEquals(Options.builder()
				.put("FIELD", "value (extra)")
				.build(), actual);
	}
	
}

@@ -1547,36 +1547,8 @@
 AL, 
-TermFilter.defaultTermMatch(
 %22not
@@ -1563,17 +1563,16 @@
 ession%22)
-)
 %0A%09%09%09%09.bu
@@ -1899,41 +1899,12 @@
 AL, 
-TermFilter.defaultTermMatch(
 %22@%22)
-)
 %0A%09%09%09
@@ -2234,36 +2234,8 @@
 AL, 
-TermFilter.defaultTermMatch(
 %22@fi
@@ -2239,17 +2239,16 @@
 @field%22)
-)
 %0A%09%09%09%09.bu
@@ -2567,36 +2567,8 @@
 AL, 
-TermFilter.defaultTermMatch(
 %22@fi
@@ -2574,17 +2574,16 @@
 ield()%22)
-)
 %0A%09%09%09%09.bu
@@ -2880,36 +2880,8 @@
 AL, 
-TermFilter.defaultTermMatch(
 %22@fi
@@ -2888,25 +2888,24 @@
 eld(value)%22)
-)
 %0A%09%09%09%09.build(
@@ -3263,36 +3263,8 @@
 AL, 
-TermFilter.defaultTermMatch(
 %22@fi
@@ -3275,17 +3275,16 @@
 value)%22)
-)
 %0A%09%09%09%09.pu
@@ -3688,36 +3688,8 @@
 AL, 
-TermFilter.defaultTermMatch(
 %22@fi
@@ -3708,17 +3708,16 @@
 xtra))%22)
-)
 %0A%09%09%09%09.bu

Revert "SO-4243: wrap special options in TermFilter in test-cases"