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

ConfigurationManagementGraph to throw Exception with custom ID setting #4708

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -30,6 +30,7 @@
import org.janusgraph.graphdb.database.management.ManagementSystem;
import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphAlreadyInstantiatedException;
import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphNotEnabledException;
import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphSettingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -77,6 +78,11 @@
// but for now, leaving open for testing purposes
public ConfigurationManagementGraph(StandardJanusGraph graph) {
initialize();
if (graph.getConfiguration().allowVertexIdSetting()) {
final String errMsg = "ConfigurationManagementGraph prohibits usage of custom vertex ID config. " +

Check warning on line 82 in janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java

View check run for this annotation

Codecov / codecov/patch

janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java#L82

Added line #L82 was not covered by tests
"Note: ConfigurationManagementGraph and actual graphs can have different configs.";
throw new ConfigurationManagementGraphSettingException(errMsg);

Check warning on line 84 in janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java

View check run for this annotation

Codecov / codecov/patch

janusgraph-core/src/main/java/org/janusgraph/graphdb/management/ConfigurationManagementGraph.java#L84

Added line #L84 was not covered by tests
}
this.graph = graph;
createIndexIfDoesNotExist(GRAPH_NAME_INDEX, PROPERTY_GRAPH_NAME, String.class, true);
createIndexIfDoesNotExist(TEMPLATE_INDEX, PROPERTY_TEMPLATE, Boolean.class, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 JanusGraph Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package org.janusgraph.graphdb.management.utils;

public class ConfigurationManagementGraphSettingException extends RuntimeException {
public ConfigurationManagementGraphSettingException(String message) {
super(message);
}

Check warning on line 21 in janusgraph-core/src/main/java/org/janusgraph/graphdb/management/utils/ConfigurationManagementGraphSettingException.java

View check run for this annotation

Codecov / codecov/patch

janusgraph-core/src/main/java/org/janusgraph/graphdb/management/utils/ConfigurationManagementGraphSettingException.java#L20-L21

Added lines #L20 - L21 were not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 JanusGraph Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.janusgraph.core.inmemory;

import org.apache.commons.configuration2.MapConfiguration;
import org.apache.tinkerpop.gremlin.server.Settings;
import org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration;
import org.janusgraph.graphdb.configuration.builder.GraphDatabaseConfigurationBuilder;
import org.janusgraph.graphdb.database.StandardJanusGraph;
import org.janusgraph.graphdb.management.ConfigurationManagementGraph;
import org.janusgraph.graphdb.management.JanusGraphManager;
import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphSettingException;
import org.janusgraph.util.system.ConfigurationUtil;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ALLOW_CUSTOM_VERTEX_ID_TYPES;
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ALLOW_SETTING_VERTEX_ID;
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.STORAGE_BACKEND;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* This class is dedicated to test a common configuration error people consistently make
* when they aim to use ConfiguredGraphFactory: their management graph config contains
* `graph.set-vertex-id=true` which leads to runtime error because ConfigurationManagementGraph
* is managed by JanusGraph internally and doesn't support custom ID
*/
public class MisconfiguredGraphFactoryTest {

private static JanusGraphManager gm;

Check warning on line 44 in janusgraph-inmemory/src/test/java/org/janusgraph/core/inmemory/MisconfiguredGraphFactoryTest.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

janusgraph-inmemory/src/test/java/org/janusgraph/core/inmemory/MisconfiguredGraphFactoryTest.java#L44

Avoid unused private fields such as 'gm'.

protected MapConfiguration getManagementConfig() {
final Map<String, Object> map = new HashMap<>();
map.put(STORAGE_BACKEND.toStringWithoutRoot(), "inmemory");

// management graph config is not allowed to use custom vertex id
map.put(ALLOW_SETTING_VERTEX_ID.toStringWithoutRoot(), "true");
map.put(ALLOW_CUSTOM_VERTEX_ID_TYPES.toStringWithoutRoot(), "true");
return ConfigurationUtil.loadMapConfiguration(map);
}

@Test
public void shouldRejectInvalidConfig() throws Exception {
gm = new JanusGraphManager(new Settings());
final MapConfiguration config = getManagementConfig();
final StandardJanusGraph graph = new StandardJanusGraph(new GraphDatabaseConfigurationBuilder().build(new CommonsConfiguration(config)));

// Cannot instantiate the ConfigurationManagementGraph Singleton because custom vertex ID is not allowed
assertThrows(ConfigurationManagementGraphSettingException.class, () -> new ConfigurationManagementGraph(graph));
}
}

Loading