Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OKAPI-1189: Add "extensions" field to module descriptor #1357

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ModuleDescriptor implements Comparable<ModuleDescriptor> {
private UiModuleDescriptor uiDescriptor;
private LaunchDescriptor launchDescriptor;
private ModuleId[] replaces;
private AnyDescriptor extensions;

public ModuleDescriptor() {
}
Expand Down Expand Up @@ -393,6 +394,14 @@ public void setMetadata(AnyDescriptor metadata) {
this.metadata = metadata;
}

public AnyDescriptor getExtensions() {
return extensions;
}

public void setExtensions(AnyDescriptor extensions) {
this.extensions = extensions;
}

public RoutingEntry[] getFilters() {
return filters;
}
Expand Down
4 changes: 4 additions & 0 deletions okapi-core/src/main/raml/ModuleDescriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
"launchDescriptor": {
"description": "Default deployment for this module",
"$ref": "LaunchDescriptor.json"
},
"extensions" : {
"description": "Sets extensions for ModuleDescriptor that can store custom or meta information.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting redundant information allows to reduce the description to "Custom or meta information."

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://folio-org.atlassian.net/browse/OKAPI-732 has already added such a field to ModuleDescriptor, it is called “metadata”.

I don’t see a need to duplicate it. What is the difference between the “metadata” field and the “extensions” field?

"type": "object"
}
},
"required": ["id"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.folio.okapi.bean;

import static org.junit.Assert.*;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import java.util.LinkedHashMap;
import java.util.List;
import org.junit.Test;

public class ModuleDescriptorTest {

Expand All @@ -28,7 +35,7 @@ public void testGetExpandedPermissionsSets() {
InterfaceDescriptor id = new InterfaceDescriptor();
id.setId("test");
id.setVersion("1.0");
md.setProvides(new InterfaceDescriptor[] { id });
md.setProvides(new InterfaceDescriptor[] {id});
assertNull(md.getExpandedPermissionSets());

id.setHandlers(new RoutingEntry[] {});
Expand All @@ -37,29 +44,29 @@ public void testGetExpandedPermissionsSets() {
// handler without module permission
RoutingEntry handler = new RoutingEntry();
handler.setPathPattern("/abc");
handler.setMethods(new String[] { "GET", "POST" });
id.setHandlers(new RoutingEntry[] { handler });
handler.setMethods(new String[] {"GET", "POST"});
id.setHandlers(new RoutingEntry[] {handler});
assertNull(md.getExpandedPermissionSets());

// filter without permission
RoutingEntry filter = new RoutingEntry();
filter.setPathPattern("/*");
filter.setMethods(new String[] { "*" });
md.setFilters(new RoutingEntry[] { filter });
filter.setMethods(new String[] {"*"});
md.setFilters(new RoutingEntry[] {filter});
assertNull(md.getExpandedPermissionSets());

// empty module permissions
handler.setModulePermissions(new String[] { });
id.setHandlers(new RoutingEntry[] { handler });
filter.setModulePermissions(new String[] { });
md.setFilters(new RoutingEntry[] { filter });
handler.setModulePermissions(new String[] {});
id.setHandlers(new RoutingEntry[] {handler});
filter.setModulePermissions(new String[] {});
md.setFilters(new RoutingEntry[] {filter});
assertNull(md.getExpandedPermissionSets());

// add module permissions
handler.setModulePermissions(new String[] { "handler.read", "handler.write" });
id.setHandlers(new RoutingEntry[] { handler });
filter.setModulePermissions(new String[] { "auth.check" });
md.setFilters(new RoutingEntry[] { filter });
handler.setModulePermissions(new String[] {"handler.read", "handler.write"});
id.setHandlers(new RoutingEntry[] {handler});
filter.setModulePermissions(new String[] {"auth.check"});
md.setFilters(new RoutingEntry[] {filter});

Permission[] perms = md.getExpandedPermissionSets();
String handlerPermName = handler.generateSystemId(modId);
Expand All @@ -85,7 +92,7 @@ public void testGetExpandedPermissionsSets() {
// add regular permission sets
Permission perm = new Permission();
perm.setPermissionName("regular");
md.setPermissionSets(new Permission[] { perm });
md.setPermissionSets(new Permission[] {perm});
perms = md.getExpandedPermissionSets();
assertEquals(3, perms.length);
assertTrue(Json.encode(perms).contains("regular"));
Expand All @@ -97,4 +104,30 @@ public void testConstructorWithId() {
assertEquals("foo-1.2.3", md.getId());
assertEquals("foo", md.getProduct());
}

@Test
public void testExtensionField() {
var md = new ModuleDescriptor("foo-1.2.3");
var mdJson = JsonObject.mapFrom(md);

mdJson.put("extensions", systemUserObject());
md = mdJson.mapTo(ModuleDescriptor.class);
Object user = md.getExtensions().properties().get("user");

assertEquals("foo-1.2.3", md.getId());
assertNotNull(user);

JsonObject userJson = JsonObject.mapFrom(user);

assertEquals("system", userJson.getString("type"));
assertEquals("test.permission", userJson.getJsonArray("permissions").getString(0));
assertEquals("test2.permission", userJson.getJsonArray("permissions").getString(1));
}

public static AnyDescriptor systemUserObject() {
var userMap = new LinkedHashMap<String, Object>();
userMap.put("type", "system");
userMap.put("permissions", List.of("test.permission", "test2.permission"));
return new AnyDescriptor().set("user", userMap);
}
}