ibessonov commented on a change in pull request #267: URL: https://github.com/apache/ignite-3/pull/267#discussion_r686550144
########## File path: modules/rest/src/main/java/org/apache/ignite/rest/presentation/hocon/HoconPresentation.java ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.rest.presentation.hocon; + +import com.typesafe.config.ConfigException; +import java.util.List; +import org.apache.ignite.configuration.validation.ConfigurationValidationException; +import org.apache.ignite.internal.configuration.ConfigurationRegistry; +import org.apache.ignite.internal.configuration.hocon.HoconConverter; +import org.apache.ignite.rest.presentation.ConfigurationPresentation; +import org.jetbrains.annotations.Nullable; + +import static com.typesafe.config.ConfigFactory.parseString; +import static com.typesafe.config.ConfigRenderOptions.concise; +import static org.apache.ignite.internal.configuration.util.ConfigurationUtil.split; + +/** + * Representing the configuration as a string using HOCON as a converter to JSON. + */ +public class HoconPresentation implements ConfigurationPresentation<String> { + /** Configuration registry. */ + private final ConfigurationRegistry registry; + + /** + * Constructor. + * + * @param registry Configuration registry. + */ + public HoconPresentation(ConfigurationRegistry registry) { + this.registry = registry; + } + + /** {@inheritDoc} */ + @Override public String represent() { + return representByPath(null); + } + + /** {@inheritDoc} */ + @Override public String representByPath(@Nullable String path) { + return HoconConverter.represent(registry, path == null ? List.of() : split(path)).render(concise()); + } + + /** {@inheritDoc} */ + @Override public void update(String cfgUpdate) { + if (cfgUpdate.isBlank()) + throw new IllegalArgumentException("Empty configuration"); + + try { + registry.change(HoconConverter.hoconSource(parseString(cfgUpdate).root()), null).get(); + } + catch (IllegalArgumentException | ConfigurationValidationException e) { + throw e; + } + catch (ConfigException.Parse e) { + throw new IllegalArgumentException(e); + } + catch (Throwable t) { + RuntimeException e; + + if (t.getCause() instanceof IllegalArgumentException) + e = (RuntimeException)t.getCause(); + else if (t.getCause() instanceof ConfigurationValidationException) + e = (RuntimeException)t.getCause(); + else + e = new RuntimeException(t); Review comment: IgniteException would fit better ########## File path: modules/rest/src/main/java/org/apache/ignite/rest/presentation/hocon/HoconPresentation.java ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.rest.presentation.hocon; + +import com.typesafe.config.ConfigException; Review comment: Imports have wrong order here ########## File path: modules/rest/src/test/java/org/apache/ignite/rest/presentation/ConfigurationPresentationTest.java ########## @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.rest.presentation; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.UUID; +import org.apache.ignite.configuration.annotation.Config; +import org.apache.ignite.configuration.annotation.ConfigValue; +import org.apache.ignite.configuration.annotation.ConfigurationRoot; +import org.apache.ignite.configuration.annotation.Value; +import org.apache.ignite.configuration.validation.ConfigurationValidationException; +import org.apache.ignite.configuration.validation.ValidationContext; +import org.apache.ignite.configuration.validation.ValidationIssue; +import org.apache.ignite.configuration.validation.Validator; +import org.apache.ignite.internal.configuration.ConfigurationRegistry; +import org.apache.ignite.internal.configuration.storage.TestConfigurationStorage; +import org.apache.ignite.rest.presentation.hocon.HoconPresentation; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Testing the {@link ConfigurationPresentation}. + */ +public class ConfigurationPresentationTest { + /** Configuration registry. */ + private static ConfigurationRegistry cfgRegistry; + + /** Configuration presentation. */ + private static ConfigurationPresentation<String> cfgPresentation; + + /** Test root configuration. */ + private static TestRootConfiguration cfg; + + /** */ + @BeforeAll + static void beforeAll() { + Validator<Value, Object> validator = new Validator<>() { + /** {@inheritDoc} */ + @Override public void validate(Value annotation, ValidationContext<Object> ctx) { + if (Objects.equals("error", ctx.getNewValue())) + ctx.addIssue(new ValidationIssue("Error word")); + } + }; + + cfgRegistry = new ConfigurationRegistry( + List.of(TestRootConfiguration.KEY), + Map.of(Value.class, Set.of(validator)), + List.of(new TestConfigurationStorage(LOCAL)) + ); + + cfgRegistry.start(); + + cfgPresentation = new HoconPresentation(cfgRegistry); + + cfg = cfgRegistry.getConfiguration(TestRootConfiguration.KEY); + } + + /** */ + @AfterAll + static void afterAll() { + cfgRegistry.stop(); + cfgRegistry = null; + + cfgPresentation = null; + + cfg = null; + } + + /** */ + @BeforeEach + void beforeEach() throws Exception { + cfg.change(cfg -> cfg.changeFoo("foo").changeSubCfg(subCfg -> subCfg.changeBar("bar"))).get(1, SECONDS); + } + + /** */ + @Test + void testRepresentWholeCfg() { + String s = "{\"root\":{\"foo\":\"foo\",\"subCfg\":{\"bar\":\"bar\"}}}"; + + assertEquals(s, cfgPresentation.represent()); + assertEquals(s, cfgPresentation.representByPath(null)); + } + + /** */ + @Test + void testCorrectRepresentCfgByPath() { + Map<String, String> paths = Map.of( Review comment: Test would look less confusing if you just had 4 asserts one after the other ########## File path: modules/rest/src/main/java/org/apache/ignite/rest/RestModule.java ########## @@ -116,28 +115,11 @@ public ChannelFuture start() throws InterruptedException { .readCharSequence(req.request().content().readableBytes(), StandardCharsets.UTF_8) .toString()); } - catch (IllegalArgumentException argE) { - ErrorResult eRes = new ErrorResult("CONFIG_PATH_UNRECOGNIZED", argE.getMessage()); + catch (RuntimeException e) { Review comment: Can't find "CONFIG_PATH_UNRECOGNIZED" ########## File path: modules/rest/src/test/java/org/apache/ignite/rest/presentation/ConfigurationPresentationTest.java ########## @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.ignite.rest.presentation; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.UUID; +import org.apache.ignite.configuration.annotation.Config; +import org.apache.ignite.configuration.annotation.ConfigValue; +import org.apache.ignite.configuration.annotation.ConfigurationRoot; +import org.apache.ignite.configuration.annotation.Value; +import org.apache.ignite.configuration.validation.ConfigurationValidationException; +import org.apache.ignite.configuration.validation.ValidationContext; +import org.apache.ignite.configuration.validation.ValidationIssue; +import org.apache.ignite.configuration.validation.Validator; +import org.apache.ignite.internal.configuration.ConfigurationRegistry; +import org.apache.ignite.internal.configuration.storage.TestConfigurationStorage; +import org.apache.ignite.rest.presentation.hocon.HoconPresentation; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Testing the {@link ConfigurationPresentation}. + */ +public class ConfigurationPresentationTest { + /** Configuration registry. */ + private static ConfigurationRegistry cfgRegistry; + + /** Configuration presentation. */ + private static ConfigurationPresentation<String> cfgPresentation; + + /** Test root configuration. */ + private static TestRootConfiguration cfg; + + /** */ + @BeforeAll + static void beforeAll() { + Validator<Value, Object> validator = new Validator<>() { + /** {@inheritDoc} */ + @Override public void validate(Value annotation, ValidationContext<Object> ctx) { + if (Objects.equals("error", ctx.getNewValue())) + ctx.addIssue(new ValidationIssue("Error word")); + } + }; + + cfgRegistry = new ConfigurationRegistry( + List.of(TestRootConfiguration.KEY), + Map.of(Value.class, Set.of(validator)), + List.of(new TestConfigurationStorage(LOCAL)) + ); + + cfgRegistry.start(); + + cfgPresentation = new HoconPresentation(cfgRegistry); + + cfg = cfgRegistry.getConfiguration(TestRootConfiguration.KEY); + } + + /** */ + @AfterAll + static void afterAll() { + cfgRegistry.stop(); + cfgRegistry = null; + + cfgPresentation = null; + + cfg = null; + } + + /** */ + @BeforeEach + void beforeEach() throws Exception { + cfg.change(cfg -> cfg.changeFoo("foo").changeSubCfg(subCfg -> subCfg.changeBar("bar"))).get(1, SECONDS); + } + + /** */ + @Test + void testRepresentWholeCfg() { + String s = "{\"root\":{\"foo\":\"foo\",\"subCfg\":{\"bar\":\"bar\"}}}"; + + assertEquals(s, cfgPresentation.represent()); + assertEquals(s, cfgPresentation.representByPath(null)); + } + + /** */ + @Test + void testCorrectRepresentCfgByPath() { + Map<String, String> paths = Map.of( + "root", "{\"foo\":\"foo\",\"subCfg\":{\"bar\":\"bar\"}}", + "root.foo", "\"foo\"", + "root.subCfg", "{\"bar\":\"bar\"}", + "root.subCfg.bar", "\"bar\"" + ); + + for (Map.Entry<String, String> e : paths.entrySet()) + assertEquals(e.getValue(), cfgPresentation.representByPath(e.getKey())); + } + + /** */ + @Test + void testErrorRepresentCfgByPath() { + assertThrows( + IllegalArgumentException.class, + () -> cfgPresentation.representByPath(UUID.randomUUID().toString()) + ); + } + + /** */ + @Test + void testCorrectUpdateFullCfg() { + String updateVal = "{\"root\":{\"foo\":\"bar\",\"subCfg\":{\"bar\":\"foo\"}}}"; + + cfgPresentation.update(updateVal); + + assertEquals("bar", cfg.foo().value()); + assertEquals("foo", cfg.subCfg().bar().value()); + assertEquals(updateVal, cfgPresentation.represent()); + } + + /** */ + @Test + void testCorrectUpdateSubCfg() { + cfgPresentation.update("{\"root\":{\"subCfg\":{\"bar\":\"foo\"}}}"); + + assertEquals("foo", cfg.foo().value()); + assertEquals("foo", cfg.subCfg().bar().value()); + assertEquals("{\"root\":{\"foo\":\"foo\",\"subCfg\":{\"bar\":\"foo\"}}}", cfgPresentation.represent()); + } + + /** */ + @Test + void testErrorUpdateCfg() { + Map<String, Class<? extends RuntimeException>> errors = Map.of( + "{\"root\":{\"foo\":100,\"subCfg\":{\"bar\":\"foo\"}}}", IllegalArgumentException.class, + "{\"root0\":{\"foo\":\"foo\",\"subCfg\":{\"bar\":\"foo\"}}}", IllegalArgumentException.class, + "{", IllegalArgumentException.class, + "", IllegalArgumentException.class, + "{\"root\":{\"foo\":\"error\",\"subCfg\":{\"bar\":\"foo\"}}}", ConfigurationValidationException.class + ); + + for (Map.Entry<String, Class<? extends RuntimeException>> e : errors.entrySet()) Review comment: Same comment applies to this test, why do we need maps? It makes tests code less linear -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
