/*
 * Copyright 2011-2018 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.test.commons.snomed;

import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

import org.eclipse.emf.common.util.WrappedException;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b2international.commons.ReflectionUtils;
import com.b2international.index.es.client.EsIndexStatus;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.branch.Branch;
import com.b2international.snowowl.core.config.SnowOwlConfiguration;
import com.b2international.snowowl.core.domain.BranchContext;
import com.b2international.snowowl.core.domain.DelegatingContext;
import com.b2international.snowowl.core.domain.RepositoryContext;
import com.b2international.snowowl.core.domain.RepositoryContextProvider;
import com.b2international.snowowl.core.events.DelegatingRequest;
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.datastore.request.BranchRequest;
import com.b2international.snowowl.datastore.request.IndexReadRequest;
import com.b2international.snowowl.datastore.request.RepositoryRequest;
import com.b2international.snowowl.eventbus.EventBusUtil;
import com.b2international.snowowl.eventbus.IEventBus;

/**
 * @since 6.4
 */
public final class TestBranchContext extends DelegatingContext implements BranchContext, RepositoryContextProvider {

	private final String repositoryId;
	private final Branch branch;

	private TestBranchContext(String repositoryId, Branch branch) {
		super(ServiceProvider.EMPTY);
		this.repositoryId = repositoryId;
		this.branch = branch;
	}
	
	@Override
	public RepositoryContext getContext(String repositoryId) {
		return this;
	}
	
	@Override
	public Logger log() {
		return LoggerFactory.getLogger(repositoryId);
	}
	
	@Override
	public Branch branch() {
		return branch;
	}
	
	@Override
	public String branchPath() {
		return branch.path();
	}
	
	@Override
	public SnowOwlConfiguration config() {
		return service(SnowOwlConfiguration.class);
	}
	
	@Override
	public String id() {
		return repositoryId;
	}
	
	@Override
	public Health health() {
		return Health.GREEN;
	}
	
	@Override
	public List<EsIndexStatus> indices() {
		return Collections.emptyList();
	}
	
	public static TestBranchContext.Builder on(String branch) {
		return new TestBranchContext.Builder(branch);
	}
	
	public static class Builder {
		
		private TestBranchContext context;
		
		Builder(String branch) {
			final String repositoryId = UUID.randomUUID().toString();
			final Branch mockBranch = Mockito.mock(Branch.class);
			when(mockBranch.path()).thenReturn(branch);
			context = new TestBranchContext(repositoryId, mockBranch);
			final IEventBus bus = EventBusUtil.getDirectBus(repositoryId);
			bus.registerHandler(Request.ADDRESS, message -> {
				try {
					final RepositoryRequest<?> repoReq = message.body(RepositoryRequest.class);
					final IndexReadRequest<?> indexReadReq = ReflectionUtils.getField(DelegatingRequest.class, repoReq, "next");
					final BranchRequest<?> branchReq = ReflectionUtils.getField(DelegatingRequest.class, indexReadReq, "next");
					final Request<BranchContext, ?> innerReq = ReflectionUtils.getField(DelegatingRequest.class, branchReq, "next");
					message.reply(innerReq.execute(context));
				} catch (WrappedException e1) {
					message.fail(e1.getCause());
				} catch (Throwable e2) {
					message.fail(e2);
				}
			});
			with(IEventBus.class, bus);
			with(RepositoryContextProvider.class, context);
		}
		
		public <T> Builder with(Class<T> type, T object) {
			context.bind(type, object);
			return this;
		}
		
		public BranchContext build() {
			return context;
		}
		
	}
	
}
@@ -21,9 +21,9 @@
 -201
-8
+9
  B2i
@@ -683,49 +683,8 @@
 d;%0A%0A
-import static org.mockito.Mockito.when;%0A%0A
 impo
@@ -813,36 +813,8 @@
 on;%0A
-import org.mockito.Mockito;%0A
 impo
@@ -919,16 +919,73 @@
 nUtils;%0A
+import com.b2international.commons.options.MetadataImpl;%0A
 import c
@@ -1065,36 +1065,163 @@
 nal.
-snowowl.core.ServiceProvider
+index.revision.RevisionBranch.BranchState;%0Aimport com.b2international.snowowl.core.ServiceProvider;%0Aimport com.b2international.snowowl.core.api.IBranchPath
 ;%0Aim
@@ -1724,32 +1724,94 @@
 events.Request;%0A
+import com.b2international.snowowl.datastore.BranchPathUtils;%0A
 import com.b2int
@@ -3396,106 +3396,258 @@
 %0A%09%09%09
-final Branch mockBranch = Mockito.mock(Branch.class);%0A%09%09%09when(mockB
+IBranchPath branchPath = BranchPathUtils.createPath(branch);%0A%09%09%09final Branch mockBranch = new Branch(1L, branchPath.lastSegment(), b
 ranch
-.path()).thenReturn(branch
+Path.getParentPath(), 0L, 1L, false, new MetadataImpl(), BranchState.FORWARD, branchPath, Collections.emptyList()
 );%0A%09

fix failed test cases due to incorrect mock in TestBranchContext