-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add namespace filter option in config file (#2688)
* feat: add namespace filter option in config Signed-off-by: Yan <yannick.libert@gmail.com> Signed-off-by: yanlibert <yannick.libert@gmail.com> * doc: better comment Signed-off-by: Yan <yannick.libert@gmail.com> Signed-off-by: yanlibert <yannick.libert@gmail.com> * feat: implement excludes for namespaces Signed-off-by: yanlibert <yannick.libert@gmail.com> * feat: relocate test to proper location Signed-off-by: yanlibert <yannick.libert@gmail.com> * feat: increased test coverage Signed-off-by: yanlibert <yannick.libert@gmail.com> * doc: add FAQ entry for filtering namespaces Signed-off-by: yanlibert <yannick.libert@gmail.com> * refacto: cleaner Exclusion interface and better yml usage Signed-off-by: yanlibert <yannick.libert@gmail.com> * feat: add Exclusion interface Signed-off-by: yanlibert <yannick.libert@gmail.com> --------- Signed-off-by: Yan <yannick.libert@gmail.com> Signed-off-by: yanlibert <yannick.libert@gmail.com>
- Loading branch information
Showing
9 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
api/src/main/java/marquez/api/filter/exclusions/Exclusions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package marquez.api.filter.exclusions; | ||
|
||
import com.google.common.collect.ClassToInstanceMap; | ||
import com.google.common.collect.MutableClassToInstanceMap; | ||
import lombok.NonNull; | ||
import marquez.api.filter.exclusions.ExclusionsConfig.NamespaceExclusions; | ||
|
||
public final class Exclusions { | ||
private Exclusions() {} | ||
|
||
private static final ClassToInstanceMap<Object> EXCLUSIONS = MutableClassToInstanceMap.create(); | ||
|
||
public static void use(@NonNull ExclusionsConfig config) { | ||
EXCLUSIONS.put(ExclusionsConfig.NamespaceExclusions.class, config.getNamespaces()); | ||
} | ||
|
||
public static NamespaceExclusions namespaces() { | ||
return EXCLUSIONS.getInstance(NamespaceExclusions.class); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
api/src/main/java/marquez/api/filter/exclusions/ExclusionsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package marquez.api.filter.exclusions; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
|
||
public class ExclusionsConfig { | ||
@Getter @JsonProperty public NamespaceExclusions namespaces; | ||
|
||
public static class NamespaceExclusions { | ||
@Getter | ||
@JsonProperty("onRead") | ||
public OnRead onRead; | ||
|
||
@Getter | ||
@JsonProperty("onWrite") | ||
public OnWrite onWrite; | ||
} | ||
|
||
public static class OnRead { | ||
@Getter | ||
@JsonProperty("enabled") | ||
public boolean enabled; | ||
|
||
@Getter | ||
@JsonProperty("pattern") | ||
public String pattern; | ||
} | ||
|
||
public static class OnWrite { | ||
@Getter | ||
@JsonProperty("enabled") | ||
public boolean enabled; | ||
|
||
@Getter | ||
@JsonProperty("pattern") | ||
public String pattern; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
api/src/test/java/marquez/api/NamespaceResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package marquez.api; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.doCallRealMethod; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
import javax.ws.rs.core.Response; | ||
import marquez.api.NamespaceResource.Namespaces; | ||
import marquez.api.filter.exclusions.Exclusions; | ||
import marquez.api.filter.exclusions.ExclusionsConfig; | ||
import marquez.common.models.NamespaceName; | ||
import marquez.common.models.OwnerName; | ||
import marquez.db.BaseDao; | ||
import marquez.db.NamespaceDao; | ||
import marquez.jdbi.MarquezJdbiExternalPostgresExtension; | ||
import marquez.service.NamespaceService; | ||
import marquez.service.ServiceFactory; | ||
import marquez.service.models.Namespace; | ||
import marquez.service.models.NamespaceMeta; | ||
import org.jdbi.v3.core.Jdbi; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
@ExtendWith(MarquezJdbiExternalPostgresExtension.class) | ||
public class NamespaceResourceTest { | ||
|
||
@Mock private ServiceFactory serviceFactory; | ||
|
||
@Mock private BaseDao baseDao; | ||
|
||
private static NamespaceDao namespaceDao; | ||
private NamespaceService namespaceService; | ||
private NamespaceResource namespaceResource; | ||
|
||
@BeforeEach | ||
public void setUp(Jdbi jdbi) { | ||
MockitoAnnotations.openMocks(this); | ||
|
||
namespaceDao = jdbi.onDemand(NamespaceDao.class); | ||
when(baseDao.createNamespaceDao()).thenReturn(namespaceDao); | ||
namespaceService = new NamespaceService(baseDao); | ||
|
||
when(serviceFactory.getNamespaceService()).thenReturn(namespaceService); | ||
namespaceResource = new NamespaceResource(serviceFactory); | ||
} | ||
|
||
@Test | ||
void testFindAllFilter() { | ||
var namespaceName1 = NamespaceName.of("postgres://localhost:5432"); | ||
var namespaceMeta1 = new NamespaceMeta(new OwnerName("marquez"), null); | ||
namespaceDao.upsertNamespaceMeta(namespaceName1, namespaceMeta1); | ||
|
||
var namespaceName2 = NamespaceName.of("excluded_namespace"); | ||
var namespaceMeta2 = new NamespaceMeta(new OwnerName("yannick"), null); | ||
namespaceDao.upsertNamespaceMeta(namespaceName2, namespaceMeta2); | ||
|
||
List<Namespace> namespaces = namespaceDao.findAllWithExclusion("excluded.*", 10, 0); | ||
|
||
// Assert that the namespaces list does not contain the excluded namespace | ||
assertFalse( | ||
namespaces.stream().anyMatch(namespace -> namespace.getName().equals(namespaceName2))); | ||
} | ||
|
||
@Test | ||
public void testListWithFilter() { | ||
String filter = "excluded_.*"; | ||
ExclusionsConfig exclusionsConfig = new ExclusionsConfig(); | ||
ExclusionsConfig.NamespaceExclusions namespaceExclusions = | ||
new ExclusionsConfig.NamespaceExclusions(); | ||
ExclusionsConfig.OnRead onRead = new ExclusionsConfig.OnRead(); | ||
onRead.enabled = true; | ||
onRead.pattern = filter; | ||
namespaceExclusions.onRead = onRead; | ||
|
||
exclusionsConfig.namespaces = namespaceExclusions; | ||
Exclusions.use(exclusionsConfig); | ||
|
||
NamespaceName namespaceName = NamespaceName.of("excluded_namespace"); | ||
OwnerName owner = new OwnerName("yannick"); | ||
NamespaceMeta namespaceMeta = new NamespaceMeta(owner, "description"); | ||
|
||
namespaceDao.upsertNamespaceMeta(namespaceName, namespaceMeta); | ||
|
||
NamespaceService namespaceServiceSpy = spy(namespaceService); | ||
doCallRealMethod() | ||
.when(namespaceServiceSpy) | ||
.findAllWithExclusion(eq(filter), anyInt(), anyInt()); | ||
|
||
Response response = namespaceResource.list(10, 0); | ||
Namespaces namespaces = (Namespaces) response.getEntity(); | ||
|
||
// Check if the returned namespaces contain a namespace with the name | ||
// "excluded_namespace" | ||
boolean containsExcludedNamespace = | ||
namespaces.getValue().stream() | ||
.anyMatch(namespace -> namespace.getName().getValue().equals("excluded_namespace")); | ||
|
||
// Assert that the returned namespaces do not contain a namespace with the name | ||
// "excluded_namespace" | ||
assertFalse(containsExcludedNamespace); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
api/src/test/java/marquez/api/filter/exclusions/ExclusionsConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package marquez.api.filter.exclusions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class ExclusionsConfigTest { | ||
@Test | ||
public void testNamespaceExclusionsOnRead() { | ||
ExclusionsConfig exclusionsConfig = new ExclusionsConfig(); | ||
ExclusionsConfig.NamespaceExclusions namespaceExclusions = | ||
new ExclusionsConfig.NamespaceExclusions(); | ||
ExclusionsConfig.OnRead onRead = new ExclusionsConfig.OnRead(); | ||
onRead.enabled = true; | ||
onRead.pattern = "readPattern"; | ||
namespaceExclusions.onRead = onRead; | ||
|
||
exclusionsConfig.namespaces = namespaceExclusions; | ||
|
||
assertEquals(true, exclusionsConfig.namespaces.onRead.enabled); | ||
assertEquals("readPattern", exclusionsConfig.namespaces.onRead.pattern); | ||
} | ||
|
||
@Test | ||
public void testNamespaceExclusionsOnWrite() { | ||
ExclusionsConfig exclusionsConfig = new ExclusionsConfig(); | ||
ExclusionsConfig.NamespaceExclusions namespaceExclusions = | ||
new ExclusionsConfig.NamespaceExclusions(); | ||
ExclusionsConfig.OnWrite onWrite = new ExclusionsConfig.OnWrite(); | ||
onWrite.enabled = false; | ||
onWrite.pattern = "writePattern"; | ||
namespaceExclusions.onWrite = onWrite; | ||
|
||
exclusionsConfig.namespaces = namespaceExclusions; | ||
|
||
assertEquals(false, exclusionsConfig.namespaces.onWrite.enabled); | ||
assertEquals("writePattern", exclusionsConfig.namespaces.onWrite.pattern); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters