@@ -41,6 +41,7 @@ import kotlinx.coroutines.flow.flow
41
41
import kotlinx.coroutines.flow.onEmpty
42
42
import kotlinx.coroutines.test.runTest
43
43
import kotlinx.coroutines.withTimeout
44
+ import org.modelix.model.IVersion
44
45
import org.modelix.model.ObjectDeltaFilter
45
46
import org.modelix.model.api.IConceptReference
46
47
import org.modelix.model.client2.ModelClientV2
@@ -49,6 +50,7 @@ import org.modelix.model.client2.runWrite
49
50
import org.modelix.model.client2.useVersionStreamFormat
50
51
import org.modelix.model.lazy.RepositoryId
51
52
import org.modelix.model.server.api.BranchInfo
53
+ import org.modelix.model.server.api.v2.VersionDelta
52
54
import org.modelix.model.server.api.v2.VersionDeltaStream
53
55
import org.modelix.model.server.api.v2.VersionDeltaStreamV2
54
56
import org.modelix.model.server.installDefaultServerPlugins
@@ -603,6 +605,67 @@ class ModelReplicationServerTest {
603
605
response shouldHaveStatus HttpStatusCode .NotFound
604
606
problem.type shouldBe " /problems/object-value-not-found"
605
607
}
608
+
609
+ @Test
610
+ fun `postRepositoryBranch with failIfExists true returns 409 if branch exists` () = runWithTestModelServer { _, fixture ->
611
+ // Arrange
612
+ val repositoryId = RepositoryId (" repo1" )
613
+ val branch = " existingBranch"
614
+ var version: IVersion ? = null
615
+ @OptIn(RequiresTransaction ::class )
616
+ fixture.repositoriesManager.getTransactionManager().runWrite {
617
+ version = fixture.repositoriesManager.createRepository(repositoryId, null )
618
+ fixture.repositoriesManager.forcePush(
619
+ repositoryId.getBranchReference(branch),
620
+ version.getContentHash(),
621
+ )
622
+ }
623
+
624
+ // Act
625
+ val client = createClient { install(ContentNegotiation ) { json() } }
626
+ val response = client.post {
627
+ url {
628
+ appendPathSegments(" v2" , " repositories" , repositoryId.id, " branches" , branch)
629
+ parameters.append(" failIfExists" , " true" )
630
+ }
631
+ useVersionStreamFormat()
632
+ contentType(ContentType .Application .Json )
633
+ setBody(VersionDelta (version!! .getContentHash(), null , objectsMap = mapOf<String , String >()))
634
+ }
635
+
636
+ // Assert
637
+ assertEquals(HttpStatusCode .Conflict , response.status)
638
+ assertContains(response.bodyAsText(), " already exists" )
639
+ }
640
+
641
+ @Test
642
+ fun `postRepositoryBranch with failIfExists true creates branch if not exists` () = runWithTestModelServer { _, fixture ->
643
+ // Arrange
644
+ val repositoryId = RepositoryId (" repo1" )
645
+ val branch = " newBranch"
646
+
647
+ var version: IVersion ? = null
648
+ @OptIn(RequiresTransaction ::class )
649
+ fixture.repositoriesManager.getTransactionManager().runWrite {
650
+ version = fixture.repositoriesManager.createRepository(repositoryId, null )
651
+ }
652
+
653
+ // Act
654
+ val client = createClient { install(ContentNegotiation ) { json() } }
655
+ val response = client.post {
656
+ url {
657
+ appendPathSegments(" v2" , " repositories" , repositoryId.id, " branches" , branch)
658
+ parameters.append(" failIfExists" , " true" )
659
+ }
660
+ useVersionStreamFormat()
661
+ contentType(ContentType .Application .Json )
662
+ setBody(VersionDelta (version!! .getContentHash(), null , objectsMap = mapOf<String , String >()))
663
+ }
664
+
665
+ // Assert
666
+ assertEquals(HttpStatusCode .OK , response.status)
667
+ assertContains(response.bodyAsText(), version!! .getContentHash())
668
+ }
606
669
}
607
670
608
671
private fun ApplicationTestBuilder.jsonClient (): HttpClient = createClient {
0 commit comments