/*
 * 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.versioning;

import static com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants.SNOMED_SHORT_NAME;
import static com.b2international.snowowl.snomed.core.rest.CodeSystemVersionRestRequests.createVersion;
import static com.b2international.snowowl.snomed.core.rest.CodeSystemVersionRestRequests.getNextAvailableEffectiveDateAsString;
import static com.b2international.snowowl.snomed.core.rest.CodeSystemVersionRestRequests.getVersion;

import org.junit.Test;

import com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest;

/**
 * @since 2.0
 */
public class SnomedVersioningApiTest extends AbstractSnomedApiTest {

	@Test
	public void getNonExistentVersion() {
		getVersion(SNOMED_SHORT_NAME, "nonexistent-version-id").statusCode(404);
	}

	@Test
	public void createVersionWithoutDescription() {
		createVersion(SNOMED_SHORT_NAME, "", getNextAvailableEffectiveDateAsString(SNOMED_SHORT_NAME)).statusCode(400);
	}

	@Test
	public void createVersionWithNonLatestEffectiveDate() {
		createVersion(SNOMED_SHORT_NAME, "not-latest-effective-time", "20020101").statusCode(400);
	}

	@Test
	public void createRegularVersion() {
		createVersion(SNOMED_SHORT_NAME, "regular-version", getNextAvailableEffectiveDateAsString(SNOMED_SHORT_NAME)).statusCode(201);
		getVersion(SNOMED_SHORT_NAME, "regular-version").statusCode(200);
	}

	@Test
	public void createVersionWithSameNameAsBranch() {
		createVersion(SNOMED_SHORT_NAME, "SnomedVersioningApiTest", getNextAvailableEffectiveDateAsString(SNOMED_SHORT_NAME)).statusCode(409);
	}
	
	@Test
	public void createForcedVersionWithSameNameAsBranch() {
		createVersion(SNOMED_SHORT_NAME, "VersionToRecreate", getNextAvailableEffectiveDateAsString(SNOMED_SHORT_NAME)).statusCode(201);
		
		//Should fail to recreate version without the force flag
		createVersion(SNOMED_SHORT_NAME, "VersionToRecreate", getNextAvailableEffectiveDateAsString(SNOMED_SHORT_NAME)).statusCode(409);
		
		//Should succeed to recreate version with the force flag set
		createVersion(SNOMED_SHORT_NAME, "VersionToRecreate", getNextAvailableEffectiveDateAsString(SNOMED_SHORT_NAME), true).statusCode(201);
	}
	
	

}

@@ -1195,46 +1195,307 @@
 owl.
-snomed.core.rest.AbstractSnomedApiTest
+core.branch.Branch;%0Aimport com.b2international.snowowl.core.uri.CodeSystemURI;%0Aimport com.b2international.snowowl.snomed.common.SnomedConstants.Concepts;%0Aimport com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest;%0Aimport com.b2international.snowowl.snomed.core.rest.SnomedRestFixtures
 ;%0A%0A/
@@ -2553,38 +2553,161 @@
 ranch() %7B%0A%09%09
-createVersion(
+final String versionName = %22VersionToRecreate%22;%09%09%0A%09%09CodeSystemURI codeSystemVersionURI = new CodeSystemURI(String.join(Branch.SEPARATOR, 
 SNOMED_SHORT
@@ -2713,35 +2713,80 @@
 T_NAME, 
-%22V
+v
 ersion
-ToRecreate%22
+Name));%0A%09%09%0A%09%09createVersion(SNOMED_SHORT_NAME, versionName
 , getNex
@@ -2854,16 +2854,142 @@
 e(201);%0A
+%09%09String conceptId = createConcept(codeSystemVersionURI, SnomedRestFixtures.createConceptRequestBody(Concepts.ROOT_CONCEPT));%0A
 %09%09%0A%09%09//S
@@ -3077,35 +3077,27 @@
 T_NAME, 
-%22V
+v
 ersion
-ToRecreate%22
+Name
 , getNex
@@ -3165,16 +3165,100 @@
 e(409);%0A
+%09%09assertGetConcept(codeSystemVersionURI, conceptId, new String%5B0%5D).statusCode(200);%0A
 %09%09%0A%09%09//S
@@ -3350,35 +3350,27 @@
 T_NAME, 
-%22V
+v
 ersion
-ToRecreate%22
+Name
 , getNex
@@ -3449,13 +3449,94 @@
 );%0A%09
-%7D
+%09assertGetConcept(codeSystemVersionURI, conceptId, new String%5B0%5D).statusCode(404);
 %0A%09
+%7D
 %0A%09%0A
-%0A
 %7D%0A

Add component change checks to force recreate version test