|
| 1 | +// Copyright 2024 JanusGraph Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package org.janusgraph.core.inmemory; |
| 16 | + |
| 17 | +import org.apache.commons.configuration2.MapConfiguration; |
| 18 | +import org.apache.tinkerpop.gremlin.server.Settings; |
| 19 | +import org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration; |
| 20 | +import org.janusgraph.graphdb.configuration.builder.GraphDatabaseConfigurationBuilder; |
| 21 | +import org.janusgraph.graphdb.database.StandardJanusGraph; |
| 22 | +import org.janusgraph.graphdb.management.ConfigurationManagementGraph; |
| 23 | +import org.janusgraph.graphdb.management.JanusGraphManager; |
| 24 | +import org.janusgraph.graphdb.management.utils.ConfigurationManagementGraphSettingException; |
| 25 | +import org.janusgraph.util.system.ConfigurationUtil; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ALLOW_CUSTOM_VERTEX_ID_TYPES; |
| 32 | +import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.ALLOW_SETTING_VERTEX_ID; |
| 33 | +import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.STORAGE_BACKEND; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 35 | + |
| 36 | +/** |
| 37 | + * This class is dedicated to test a common configuration error people consistently make |
| 38 | + * when they aim to use ConfiguredGraphFactory: their management graph config contains |
| 39 | + * `graph.set-vertex-id=true` which leads to runtime error because ConfigurationManagementGraph |
| 40 | + * is managed by JanusGraph internally and doesn't support custom ID |
| 41 | + */ |
| 42 | +public class MisconfiguredGraphFactoryTest { |
| 43 | + |
| 44 | + private static JanusGraphManager gm; |
| 45 | + |
| 46 | + protected MapConfiguration getManagementConfig() { |
| 47 | + final Map<String, Object> map = new HashMap<>(); |
| 48 | + map.put(STORAGE_BACKEND.toStringWithoutRoot(), "inmemory"); |
| 49 | + |
| 50 | + // management graph config is not allowed to use custom vertex id |
| 51 | + map.put(ALLOW_SETTING_VERTEX_ID.toStringWithoutRoot(), "true"); |
| 52 | + map.put(ALLOW_CUSTOM_VERTEX_ID_TYPES.toStringWithoutRoot(), "true"); |
| 53 | + return ConfigurationUtil.loadMapConfiguration(map); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void shouldRejectInvalidConfig() throws Exception { |
| 58 | + gm = new JanusGraphManager(new Settings()); |
| 59 | + final MapConfiguration config = getManagementConfig(); |
| 60 | + final StandardJanusGraph graph = new StandardJanusGraph(new GraphDatabaseConfigurationBuilder().build(new CommonsConfiguration(config))); |
| 61 | + |
| 62 | + // Cannot instantiate the ConfigurationManagementGraph Singleton because custom vertex ID is not allowed |
| 63 | + assertThrows(ConfigurationManagementGraphSettingException.class, () -> new ConfigurationManagementGraph(graph)); |
| 64 | + } |
| 65 | +} |
| 66 | + |
0 commit comments