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

import static com.b2international.snowowl.test.commons.rest.RestExtensions.givenAuthenticatedRequest;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import com.b2international.snowowl.core.api.IBranchPath;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;

import io.restassured.response.Response;

/**
 * @since 5.4
 */
public abstract class SnomedExportRestRequests {

	private SnomedExportRestRequests() {
	}
	
	public static Response export(IBranchPath branchPath, Map<String, ?> exportConfiguration) {
		return givenAuthenticatedRequest(SnomedApiTestConstants.SCT_API)
			.queryParams(exportConfiguration)
			.pathParam("path", branchPath.toString())
			.get("/{path}/export");
	}
	
	public static File doExport(final IBranchPath branchPath, final Map<String, ?> exportConfiguration) throws Exception {
		File tmpDir = null;
		File exportArchive = null;

		try {

			tmpDir = Files.createTempDir();
			exportArchive = new File(tmpDir, "export.zip");
			new ByteSource() {
				@Override
				public InputStream openStream() throws IOException {
					return export(branchPath, exportConfiguration)
							.thenReturn()
							.asInputStream();
				}
			}.copyTo(Files.asByteSink(exportArchive));

		} catch (final Exception e) {
			throw e;
		} finally {
			if (tmpDir != null) {
				tmpDir.deleteOnExit();
			}

			if (exportArchive != null) {
				exportArchive.deleteOnExit();
			}
		}

		assertNotNull(exportArchive);
		return exportArchive;
	}

}

@@ -17,17 +17,17 @@
 2011-202
-0
+1
  B2i Hea
@@ -1246,35 +1246,24 @@
  export(
-IBranchPath branchP
+String p
 ath, Map
@@ -1427,29 +1427,12 @@
 h%22, 
-branchPath.toString()
+path
 )%0A%09%09
@@ -1581,16 +1581,198 @@
 ption %7B%0A
+%09%09return doExport(branchPath.getPath(), exportConfiguration);%0A%09%7D%0A%09%0A%09public static File doExport(final String branchPath, final Map%3CString, ?%3E exportConfiguration) throws Exception %7B%0A
 %09%09File t

allow string as branch path as well