2222import org .apache .nifi .components .PropertyValue ;
2323import org .apache .nifi .logging .ComponentLog ;
2424import org .apache .nifi .registry .flow .FlowRegistryClientConfigurationContext ;
25+ import org .apache .nifi .registry .flow .FlowRegistryClientInitializationContext ;
2526import org .apache .nifi .registry .flow .FlowRegistryException ;
27+ import org .apache .nifi .registry .flow .FlowVersionLocation ;
2628import org .apache .nifi .registry .flow .git .client .GitCommit ;
2729import org .apache .nifi .registry .flow .git .client .GitCreateContentRequest ;
2830import org .apache .nifi .registry .flow .git .client .GitRepositoryClient ;
2931import org .apache .nifi .util .MockComponentLog ;
3032import org .apache .nifi .util .MockPropertyValue ;
3133import org .junit .jupiter .api .Test ;
3234
35+ import javax .net .ssl .SSLContext ;
36+
3337import java .io .IOException ;
3438import java .io .InputStream ;
3539import java .util .List ;
4044import java .util .concurrent .atomic .AtomicReference ;
4145
4246import static org .junit .jupiter .api .Assertions .assertEquals ;
47+ import static org .junit .jupiter .api .Assertions .assertThrows ;
4348import static org .junit .jupiter .api .Assertions .assertTrue ;
4449
4550class AbstractGitFlowRegistryClientTest {
@@ -49,6 +54,7 @@ void verifySuccessful() throws Exception {
4954 final TestGitRepositoryClient repositoryClient = new TestGitRepositoryClient (true , true , Set .of ("bucket-a" , ".git" ));
5055 final AtomicReference <TestGitRepositoryClient > suppliedClient = new AtomicReference <>(repositoryClient );
5156 final TestGitFlowRegistryClient flowRegistryClient =
new TestGitFlowRegistryClient (() ->
suppliedClient .
getAndSet (
null ),
"[email protected] " );
57+ flowRegistryClient .initialize (createInitializationContext ());
5258 final FlowRegistryClientConfigurationContext context = createContext ("main" , "[.].*" );
5359 final ComponentLog verificationLogger = new MockComponentLog ("test-component" , this );
5460
@@ -71,6 +77,7 @@ void verifyAuthenticationFailure() {
7177 final TestGitFlowRegistryClient flowRegistryClient = new TestGitFlowRegistryClient (() -> {
7278 throw new FlowRegistryException ("Authentication failed" );
737980+ flowRegistryClient .initialize (createInitializationContext ());
7481 final FlowRegistryClientConfigurationContext context = createContext ("main" , "[.].*" );
7582 final ComponentLog verificationLogger = new MockComponentLog ("test-component" , this );
7683
@@ -85,6 +92,7 @@ void verifyAuthenticationFailure() {
8592 void verifyReadFailureSkipsBucketListing () throws Exception {
8693 final TestGitRepositoryClient repositoryClient = new TestGitRepositoryClient (false , false , Set .of ());
8794 final TestGitFlowRegistryClient flowRegistryClient =
new TestGitFlowRegistryClient (() ->
repositoryClient ,
"[email protected] " );
95+ flowRegistryClient .initialize (createInitializationContext ());
8896 final FlowRegistryClientConfigurationContext context = createContext ("main" , "[.].*" );
8997 final ComponentLog verificationLogger = new MockComponentLog ("test-component" , this );
9098
@@ -100,10 +108,11 @@ void verifyReadFailureSkipsBucketListing() throws Exception {
100108
101109 @ Test
102110 void verifyBucketListingFailureReported () throws Exception {
103- final TestGitRepositoryClient repositoryClient = new TestGitRepositoryClient (true , true , Set .of ());
111+ final TestGitRepositoryClient repositoryClient = new TestGitRepositoryClient (true , true , Set .of ("bucket-a" ));
104112 repositoryClient .setGetTopLevelDirectoryNamesException (new FlowRegistryException ("listing error" ));
105113
106114 final TestGitFlowRegistryClient flowRegistryClient =
new TestGitFlowRegistryClient (() ->
repositoryClient ,
"[email protected] " );
115+ flowRegistryClient .initialize (createInitializationContext ());
107116 final FlowRegistryClientConfigurationContext context = createContext ("main" , "[.].*" );
108117 final ComponentLog verificationLogger = new MockComponentLog ("test-component" , this );
109118
@@ -118,6 +127,41 @@ void verifyBucketListingFailureReported() throws Exception {
118127 assertTrue (repositoryClient .isClosed ());
119128 }
120129
130+ @ Test
131+ void createBranchDelegatesToRepositoryClient () throws Exception {
132+ final TestGitRepositoryClient repositoryClient = new TestGitRepositoryClient (true , true , Set .of ("bucket-a" ));
133+ final TestGitFlowRegistryClient flowRegistryClient =
new TestGitFlowRegistryClient (() ->
repositoryClient ,
"[email protected] " );
134+ flowRegistryClient .initialize (createInitializationContext ());
135+ final FlowRegistryClientConfigurationContext context = createContext ("main" , "[.].*" );
136+
137+ final FlowVersionLocation sourceLocation = new FlowVersionLocation ("source-branch" , "bucket-a" , "flow-x" , "commit-1" );
138+
139+ flowRegistryClient .createBranch (context , sourceLocation , " new-branch " );
140+
141+ assertEquals ("new-branch" , repositoryClient .getCreatedBranch ());
142+ assertEquals ("source-branch" , repositoryClient .getCreatedBranchSource ());
143+ assertEquals (Optional .of ("commit-1" ), repositoryClient .getCreatedBranchCommit ());
144+ }
145+
146+ @ Test
147+ void createBranchUnsupportedThrowsFlowRegistryException () throws Exception {
148+ final TestGitRepositoryClient repositoryClient = new TestGitRepositoryClient (true , true , Set .of ("bucket-a" ));
149+ repositoryClient .setBranchCreationUnsupported (true );
150+
151+ final TestGitFlowRegistryClient flowRegistryClient =
new TestGitFlowRegistryClient (() ->
repositoryClient ,
"[email protected] " );
152+ flowRegistryClient .initialize (createInitializationContext ());
153+ final FlowRegistryClientConfigurationContext context = createContext ("main" , "[.].*" );
154+
155+ final FlowVersionLocation sourceLocation = new FlowVersionLocation ();
156+ sourceLocation .setBranch ("source" );
157+
158+ final FlowRegistryException exception = assertThrows (FlowRegistryException .class ,
159+ () -> flowRegistryClient .createBranch (context , sourceLocation , "new-branch" ));
160+
161+ assertEquals ("Configured repository client does not support branch creation" , exception .getMessage ());
162+ assertTrue (repositoryClient .getCreatedBranchCommit ().isEmpty ());
163+ }
164+
121165 private FlowRegistryClientConfigurationContext createContext (final String branch , final String exclusionPattern ) {
122166 final Map <PropertyDescriptor , PropertyValue > properties = Map .of (
123167 AbstractGitFlowRegistryClient .REPOSITORY_BRANCH , new MockPropertyValue (branch ),
@@ -145,6 +189,25 @@ public Optional<String> getNiFiUserIdentity() {
145189 };
146190 }
147191
192+ private FlowRegistryClientInitializationContext createInitializationContext () {
193+ return new FlowRegistryClientInitializationContext () {
194+ @ Override
195+ public String getIdentifier () {
196+ return "test-git-client" ;
197+ }
198+
199+ @ Override
200+ public ComponentLog getLogger () {
201+ return new MockComponentLog ("test-git-client" , AbstractGitFlowRegistryClientTest .this );
202+ }
203+
204+ @ Override
205+ public Optional <SSLContext > getSystemSslContext () {
206+ return Optional .empty ();
207+ }
208+ };
209+ }
210+
148211 private static class TestGitFlowRegistryClient extends AbstractGitFlowRegistryClient {
149212 private final RepositoryClientSupplier repositoryClientSupplier ;
150213 private final String storageLocation ;
@@ -186,6 +249,11 @@ private static class TestGitRepositoryClient implements GitRepositoryClient {
186249 private FlowRegistryException topLevelDirectoryNamesException ;
187250 private IOException topLevelDirectoryNamesIOException ;
188251 private boolean closed ;
252+ private boolean branchCreationUnsupported ;
253+ private FlowRegistryException createBranchException ;
254+ private String createdBranch ;
255+ private String createdBranchSource ;
256+ private Optional <String > createdBranchCommit = Optional .empty ();
189257
190258 TestGitRepositoryClient (final boolean canRead , final boolean canWrite , final Set <String > bucketNames ) {
191259 this .canRead = canRead ;
@@ -198,11 +266,31 @@ void setGetTopLevelDirectoryNamesException(final FlowRegistryException exception
198266 this .topLevelDirectoryNamesIOException = null ;
199267 }
200268
201- void setGetTopLevelDirectoryNamesException (final IOException exception ) {
269+ void setGetTopLevelDirectoryNamesIOException (final IOException exception ) {
202270 this .topLevelDirectoryNamesIOException = exception ;
203271 this .topLevelDirectoryNamesException = null ;
204272 }
205273
274+ void setBranchCreationUnsupported (final boolean unsupported ) {
275+ this .branchCreationUnsupported = unsupported ;
276+ }
277+
278+ void setCreateBranchException (final FlowRegistryException exception ) {
279+ this .createBranchException = exception ;
280+ }
281+
282+ String getCreatedBranch () {
283+ return createdBranch ;
284+ }
285+
286+ String getCreatedBranchSource () {
287+ return createdBranchSource ;
288+ }
289+
290+ Optional <String > getCreatedBranchCommit () {
291+ return createdBranchCommit ;
292+ }
293+
206294 boolean isClosed () {
207295 return closed ;
208296 }
@@ -228,6 +316,21 @@ public Set<String> getTopLevelDirectoryNames(final String branch) throws IOExcep
228316 return bucketNames ;
229317 }
230318
319+ @ Override
320+ public void createBranch (final String newBranchName , final String sourceBranch , final Optional <String > sourceCommitSha )
321+ throws IOException , FlowRegistryException {
322+ if (branchCreationUnsupported ) {
323+ throw new UnsupportedOperationException ("Branch creation not supported" );
324+ }
325+ if (createBranchException != null ) {
326+ throw createBranchException ;
327+ }
328+
329+ createdBranch = newBranchName ;
330+ createdBranchSource = sourceBranch ;
331+ createdBranchCommit = sourceCommitSha ;
332+ }
333+
231334 @ Override
232335 public void close () {
233336 closed = true ;
@@ -265,7 +368,7 @@ public Optional<String> getContentSha(final String path, final String branch) {
265368
266369 @ Override
267370 public String createContent (final GitCreateContentRequest request ) {
268- throw new UnsupportedOperationException ( "Not required for test" ) ;
371+ return " test-commit" ;
269372 }
270373
271374 @ Override
0 commit comments