You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/05/30 15:21:19 UTC

[GitHub] [ignite-3] ptupitsyn commented on a diff in pull request #816: IGNITE-16943: Implement Micronaut REST

ptupitsyn commented on code in PR #816:
URL: https://github.com/apache/ignite-3/pull/816#discussion_r884931683


##########
modules/configuration/src/test/java/org/apache/ignite/internal/rest/configuration/ConfigurationControllerBaseTest.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.internal.rest.configuration;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import io.micronaut.context.ApplicationContext;
+import io.micronaut.http.HttpRequest;
+import io.micronaut.http.HttpStatus;
+import io.micronaut.http.MediaType;
+import io.micronaut.http.client.HttpClient;
+import io.micronaut.http.client.exceptions.HttpClientResponseException;
+import io.micronaut.runtime.server.EmbeddedServer;
+import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
+import jakarta.inject.Inject;
+import org.apache.ignite.internal.configuration.ConfigurationRegistry;
+import org.apache.ignite.internal.configuration.rest.presentation.ConfigurationPresentation;
+import org.apache.ignite.internal.configuration.rest.presentation.TestRootConfiguration;
+import org.apache.ignite.internal.rest.api.ErrorResult;
+import org.jetbrains.annotations.NotNull;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The base test for configuration controllers.
+ */
+@MicronautTest
+public abstract class ConfigurationControllerBaseTest {
+    @Inject
+    EmbeddedServer server;
+
+    @Inject
+    ConfigurationPresentation<String> cfgPresentation;
+
+    @Inject
+    ConfigurationRegistry configurationRegistry;
+
+    @Inject
+    ApplicationContext context;
+
+    abstract HttpClient client();
+
+    @BeforeEach
+    void beforeEach() throws Exception {
+        var cfg = configurationRegistry.getConfiguration(TestRootConfiguration.KEY);
+        cfg.change(c -> c.changeFoo("foo").changeSubCfg(subCfg -> subCfg.changeBar("bar"))).get(1, SECONDS);
+    }
+
+    @Test
+    void testGetConfig() {
+        var response = client().toBlocking().exchange("", String.class);
+
+        assertEquals(HttpStatus.OK, response.status());
+        assertEquals(cfgPresentation.represent(), response.body());
+    }
+
+    @Test
+    void testGetConfigByPath() {
+        var response = client().toBlocking().exchange("/root.subCfg", String.class);
+
+        assertEquals(HttpStatus.OK, response.status());
+        assertEquals(cfgPresentation.representByPath("root.subCfg"), response.body());
+    }
+
+    @Test
+    void testUpdateConfig() {
+        String givenChangedConfig = "{root:{foo:foo,subCfg:{bar:changed}}}";
+
+        var response = client().toBlocking().exchange(
+                HttpRequest.PATCH("", givenChangedConfig).contentType(MediaType.TEXT_PLAIN)
+        );
+        assertEquals(response.status(), HttpStatus.OK);
+
+        String changedConfigValue = client().toBlocking().exchange("/root.subCfg.bar", String.class).body();
+        assertEquals("\"changed\"", changedConfigValue);
+    }
+
+    @Test
+    void testUnrecognizedConfigPath() {
+        try {
+            client().toBlocking().exchange("/no-such-root.some-value");
+            fail("Expected exception to be thrown");
+        } catch (HttpClientResponseException exception) {
+            exception.printStackTrace();
+            assertEquals(HttpStatus.BAD_REQUEST, exception.getResponse().status());
+
+            var errorResult = getErrorResult(exception);
+            assertEquals("CONFIG_PATH_UNRECOGNIZED", errorResult.type());
+            assertTrue(errorResult.message().contains("no-such-root"));
+        }
+    }
+
+    @Test
+    void testUnrecognizedConfigPathForUpdate() {
+        String givenBrokenConfig = "{root:{foo:foo,subCfg:{no-such-bar:bar}}}";
+        try {
+            client().toBlocking().exchange(
+                    HttpRequest.PATCH("", givenBrokenConfig).contentType(MediaType.TEXT_PLAIN)
+            );
+            fail("Expected exception to be thrown");
+        } catch (HttpClientResponseException exception) {
+            assertEquals(HttpStatus.BAD_REQUEST, exception.getResponse().status());
+
+            var errorResult = getErrorResult(exception);
+            assertEquals("INVALID_CONFIG_FORMAT", errorResult.type());
+            assertTrue(errorResult.message().contains("no-such-bar"));
+        }
+    }
+
+    @Test
+    void testValidationForUpdate() {
+        String givenConfigWithError = "{root:{foo:error,subCfg:{bar:bar}}}";
+        try {
+            client().toBlocking().exchange(
+                    HttpRequest.PATCH("", givenConfigWithError).contentType(MediaType.TEXT_PLAIN)
+            );
+            fail("Expected exception to be thrown");

Review Comment:
   Here and other tests: prefer `assertThrows` instead of catching exceptions.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org