CASSSIDECAR-264: Settings endpoint should return Service Unavailable when Cassandra instance is down (#228)

Patch by Francisco Guerrero; reviewed by Bernardo Botella for CASSSIDECAR-264
diff --git a/integration-tests/src/integrationTest/org/apache/cassandra/sidecar/routes/NodeSettingsIntegrationTest.java b/integration-tests/src/integrationTest/org/apache/cassandra/sidecar/routes/NodeSettingsIntegrationTest.java
new file mode 100644
index 0000000..4a142ad
--- /dev/null
+++ b/integration-tests/src/integrationTest/org/apache/cassandra/sidecar/routes/NodeSettingsIntegrationTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import org.junit.jupiter.api.Test;
+
+import io.vertx.core.buffer.Buffer;
+import io.vertx.core.http.HttpResponseExpectation;
+import io.vertx.ext.web.client.HttpResponse;
+import org.apache.cassandra.sidecar.common.response.NodeSettings;
+import org.apache.cassandra.sidecar.testing.SharedClusterSidecarIntegrationTestBase;
+
+import static io.netty.handler.codec.http.HttpResponseStatus.SERVICE_UNAVAILABLE;
+import static org.apache.cassandra.testing.utils.AssertionUtils.getBlocking;
+import static org.apache.cassandra.testing.utils.AssertionUtils.loopAssert;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Integration tests for the {@link  org.apache.cassandra.sidecar.handlers.cassandra.NodeSettingsHandler}
+ */
+class NodeSettingsIntegrationTest extends SharedClusterSidecarIntegrationTestBase
+{
+    @Test
+    void testNodeSettings()
+    {
+        HttpResponse<Buffer> response = getBlocking(trustedClient().get(serverWrapper.serverPort, "localhost", "/api/v1/cassandra/settings")
+                                                                   .send()
+                                                                   .expecting(HttpResponseExpectation.SC_OK));
+        NodeSettings nodeSettings = response.bodyAsJson(NodeSettings.class);
+        assertThat(nodeSettings).isNotNull();
+        assertThat(nodeSettings.partitioner()).isEqualTo("org.apache.cassandra.dht.Murmur3Partitioner");
+        assertThat(nodeSettings.datacenter()).isEqualTo("datacenter1");
+
+        cluster.stopUnchecked(cluster.getFirstRunningInstance());
+
+        loopAssert(60, () -> {
+            HttpResponse<Buffer> responseAfterStop = getBlocking(trustedClient().get(serverWrapper.serverPort, "localhost", "/api/v1/cassandra/settings")
+                                                                                .send()
+                                                                                .expecting(HttpResponseExpectation.SC_SERVICE_UNAVAILABLE));
+            assertThat(responseAfterStop).isNotNull();
+            assertThat(responseAfterStop.statusCode()).isEqualTo(SERVICE_UNAVAILABLE.code());
+            assertThat(responseAfterStop.bodyAsJsonObject().getString("message")).contains("NodeSettings unavailable");
+        });
+    }
+
+    @Override
+    protected void initializeSchemaForTest()
+    {
+        // Do nothing
+    }
+}
diff --git a/server/src/main/java/org/apache/cassandra/sidecar/cluster/CassandraAdapterDelegate.java b/server/src/main/java/org/apache/cassandra/sidecar/cluster/CassandraAdapterDelegate.java
index 9a1d0be..a70992c 100644
--- a/server/src/main/java/org/apache/cassandra/sidecar/cluster/CassandraAdapterDelegate.java
+++ b/server/src/main/java/org/apache/cassandra/sidecar/cluster/CassandraAdapterDelegate.java
@@ -64,6 +64,7 @@
 import static org.apache.cassandra.sidecar.adapters.base.jmx.EndpointSnitchJmxOperations.ENDPOINT_SNITCH_INFO_OBJ_NAME;
 import static org.apache.cassandra.sidecar.adapters.base.jmx.StorageJmxOperations.STORAGE_SERVICE_OBJ_NAME;
 import static org.apache.cassandra.sidecar.exceptions.CassandraUnavailableException.Service.CQL_AND_JMX;
+import static org.apache.cassandra.sidecar.exceptions.CassandraUnavailableException.Service.JMX;
 import static org.apache.cassandra.sidecar.server.SidecarServerEvents.ON_CASSANDRA_CQL_DISCONNECTED;
 import static org.apache.cassandra.sidecar.server.SidecarServerEvents.ON_CASSANDRA_CQL_READY;
 import static org.apache.cassandra.sidecar.server.SidecarServerEvents.ON_CASSANDRA_JMX_DISCONNECTED;
@@ -370,6 +371,10 @@
     @NotNull
     public NodeSettings nodeSettings() throws CassandraUnavailableException
     {
+        if (nodeSettingsFromJmx == null)
+        {
+            throw new CassandraUnavailableException(JMX, "NodeSettings unavailable");
+        }
         return nodeSettingsFromJmx;
     }