View Javadoc
1   package io.apicurio.registry.maven;
2   
3   import org.junit.jupiter.api.Test;
4   
5   import java.lang.reflect.Field;
6   import java.lang.reflect.Method;
7   import java.util.Optional;
8   
9   import static org.junit.jupiter.api.Assertions.assertEquals;
10  import static org.junit.jupiter.api.Assertions.assertFalse;
11  import static org.junit.jupiter.api.Assertions.assertTrue;
12  
13  public class RegisterRegistryMojoRegistryReferenceTest {
14  
15      @Test
16      void testParseRegistryReferenceLocationAcceptsSameRegistryAndDecodesSegments() throws Exception {
17          RegisterRegistryMojo mojo = new RegisterRegistryMojo();
18          mojo.setRegistryUrl("http://localhost/apis/registry/v3/");
19  
20          Optional<?> location = parseLocation(mojo,
21                  "http://localhost:80/apis/registry/v3/groups/master%20group/artifacts/kafka-bindings.yml/"
22                          + "versions/branch%3Dlatest/content");
23  
24          assertTrue(location.isPresent());
25          assertEquals("master group", readField(location.get(), "groupId"));
26          assertEquals("kafka-bindings.yml", readField(location.get(), "artifactId"));
27          assertEquals("branch=latest", readField(location.get(), "versionExpression"));
28      }
29  
30      @Test
31      void testParseRegistryReferenceLocationRejectsDifferentRegistry() throws Exception {
32          RegisterRegistryMojo mojo = new RegisterRegistryMojo();
33          mojo.setRegistryUrl("http://localhost/apis/registry/v3/");
34  
35          Optional<?> location = parseLocation(mojo,
36                  "http://otherhost/apis/registry/v3/groups/master/artifacts/kafka-bindings.yml/versions/1");
37  
38          assertFalse(location.isPresent());
39      }
40  
41      private static Optional<?> parseLocation(RegisterRegistryMojo mojo, String resource) throws Exception {
42          Method method = RegisterRegistryMojo.class
43                  .getDeclaredMethod("parseRegistryReferenceLocation", String.class);
44          method.setAccessible(true);
45          return (Optional<?>) method.invoke(mojo, resource);
46      }
47  
48      private static Object readField(Object target, String fieldName) throws Exception {
49          Field field = target.getClass().getDeclaredField(fieldName);
50          field.setAccessible(true);
51          return field.get(target);
52      }
53  }