/*
 * Copyright 2011-2021 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.snomed.core.issue;

import static com.google.common.collect.Lists.newArrayList;
import static org.junit.Assert.assertNull;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

import com.b2international.snowowl.core.events.AsyncRequest;
import com.b2international.snowowl.core.events.util.Promise;
import com.b2international.snowowl.snomed.common.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.core.domain.SnomedConcepts;
import com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest;
import com.b2international.snowowl.snomed.datastore.request.SnomedRequests;
import com.b2international.snowowl.test.commons.SnomedContentRule;
import com.google.common.base.Throwables;

/**
 * 
 * @since 6.4
 */
public class EclSerializerTest extends AbstractSnomedApiTest {
	
	@Test
	public void verify() throws Exception {
		for (int rounds = 0; rounds < 100; rounds++) {
		
			AsyncRequest<SnomedConcepts> eclRequest = SnomedRequests.prepareSearchConcept()
					.all()
					.filterByEcl("<<" + Concepts.ROOT_CONCEPT + ":" + Concepts.MORPHOLOGY + " = " + Concepts.ROOT_CONCEPT)
					.build(SnomedContentRule.SNOMEDCT);
			
			List<Promise<SnomedConcepts>> promises = newArrayList();
			
			for (int i = 0; i < 4; i++) {
				promises.add(eclRequest.execute(getBus()));
			}
			
			final String error = Promise.all(promises)
					.then(concepts -> {
						return (String) null;
					})
					.fail(throwable -> {
						return (throwable.getMessage() != null) 
								? throwable.getMessage() 
								: Throwables.getRootCause(throwable).getClass().getSimpleName();
					})
					.getSync(1, TimeUnit.MINUTES);
			
			assertNull(error, error);
		}
	}
}

@@ -871,69 +871,8 @@
 t;%0A%0A
-import com.b2international.snowowl.core.events.AsyncRequest;%0A
 impo
@@ -1510,10 +1510,8 @@
  %3C 1
-00
 ; ro
@@ -1530,20 +1530,20 @@
 %0A%09%09%09
-AsyncRequest
+List%3CPromise
 %3CSno
@@ -1558,22 +1558,97 @@
 pts%3E
- eclRequest = 
+%3E promises = newArrayList();%0A%09%09%09%0A%09%09%09for (int i = 0; i %3C 4; i++) %7B%0A%09%09%09%09promises.add(%0A%09%09%09%09%09
 Snom
@@ -1690,15 +1690,17 @@
 %09%09%09%09
+%09
 .all()%0A
+%09
 %09%09%09%09
@@ -1808,16 +1808,17 @@
 T)%0A%09%09%09%09%09
+%09
 .build(S
@@ -1847,138 +1847,15 @@
 DCT)
-;
 %0A%09%09%09
-%0A
 %09%09%09
-List%3CPromise%3CSnomedConcepts%3E%3E promises = newArrayList();%0A%09%09%09%0A%09%09%09for (int i = 0; i %3C 4; i++) %7B%0A%09%09%09%09promises.add(eclRequest
 .exe
@@ -1868,16 +1868,21 @@
 etBus())
+%0A%09%09%09%09
 );%0A%09%09%09%7D%0A

create different request instances when performing ECL...