dsmiley commented on code in PR #4703:
URL: https://github.com/apache/solr/pull/4703#discussion_r3717529551


##########
solr/core/src/test/org/apache/solr/handler/admin/api/NodeHealthTest.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * 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.solr.handler.admin.api;
+
+import static 
org.apache.solr.client.api.model.NodeHealthResponse.NodeStatus.OK;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import org.apache.solr.SolrTestCase;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.cloud.CloudDescriptor;
+import org.apache.solr.cloud.ClusterStateMockUtil;
+import org.apache.solr.cloud.ZkController;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.SolrException.ErrorCode;
+import org.apache.solr.common.cloud.ClusterState;
+import org.apache.solr.common.cloud.Replica;
+import org.apache.solr.common.cloud.SolrZkClient;
+import org.apache.solr.common.cloud.ZkStateReader;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.core.CoreDescriptor;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Unit test for the cloud-mode logic of {@link NodeHealth}, using mocks 
instead of a real cluster.
+ *
+ * @see NodeHealthSolrCloudTest
+ * @see NodeHealthStandaloneTest
+ */
+public class NodeHealthTest extends SolrTestCase {
+
+  private static final String NODE_NAME = "baseUrl1:8983_";
+
+  private CoreContainer mockCoreContainer;
+  private ZkStateReader mockZkStateReader;
+  private SolrZkClient mockZkClient;
+  private NodeHealth nodeHealth;
+
+  @BeforeClass
+  public static void ensureWorkingMockito() {
+    SolrTestCaseJ4.assumeWorkingMockito();
+  }
+
+  @Before
+  public void setupMocks() {
+    mockCoreContainer = mock(CoreContainer.class);
+    ZkController mockZkController = mock(ZkController.class);
+    mockZkStateReader = mock(ZkStateReader.class);
+    mockZkClient = mock(SolrZkClient.class);
+
+    when(mockCoreContainer.isZooKeeperAware()).thenReturn(true);
+    when(mockCoreContainer.getZkController()).thenReturn(mockZkController);
+    when(mockZkController.getZkStateReader()).thenReturn(mockZkStateReader);
+    when(mockZkController.getNodeName()).thenReturn(NODE_NAME);
+    when(mockZkStateReader.getZkClient()).thenReturn(mockZkClient);
+    when(mockZkClient.isConnected()).thenReturn(true);
+    when(mockZkClient.isClosed()).thenReturn(false);
+    when(mockZkStateReader.getClusterState())
+        .thenReturn(new ClusterState(Set.of(NODE_NAME), Map.of()));
+
+    nodeHealth = new NodeHealth(mockCoreContainer);
+  }
+
+  @Test
+  public void testUninitializedCoreContainerReturns500() {
+    SolrException e =
+        expectThrows(SolrException.class, () -> new 
NodeHealth(null).healthcheck(null, null));
+    assertEquals(ErrorCode.SERVER_ERROR.code, e.code());
+
+    when(mockCoreContainer.isShutDown()).thenReturn(true);
+    e = expectThrows(SolrException.class, () -> nodeHealth.healthcheck(null, 
null));
+    assertEquals(ErrorCode.SERVER_ERROR.code, e.code());
+  }
+
+  @Test
+  public void testHealthyNodeReturnsOkStatus() {
+    final var response = nodeHealth.healthcheck(null, null);
+
+    assertEquals(OK, response.status);
+    assertNull("Expected no error on a healthy node", response.error);
+  }
+
+  @Test
+  public void testZkClientClosedReturns503() {
+    when(mockZkClient.isClosed()).thenReturn(true);
+
+    SolrException e = expectThrows(SolrException.class, () -> 
nodeHealth.healthcheck(null, null));
+    assertEquals(ErrorCode.SERVICE_UNAVAILABLE.code, e.code());
+    assertThat(e.getMessage(), containsString("Not connected to zk"));
+  }
+
+  @Test
+  public void testZkClientDisconnectedReturns503() {
+    when(mockZkClient.isConnected()).thenReturn(false);
+
+    SolrException e = expectThrows(SolrException.class, () -> 
nodeHealth.healthcheck(null, null));
+    assertEquals(ErrorCode.SERVICE_UNAVAILABLE.code, e.code());
+    assertThat(e.getMessage(), containsString("Not connected to zk"));
+  }
+
+  @Test
+  public void testNotInLiveNodesReturns503() {
+    when(mockZkStateReader.getClusterState())
+        .thenReturn(new ClusterState(Set.of("someOtherNode:8983_"), Map.of()));
+
+    SolrException e = expectThrows(SolrException.class, () -> 
nodeHealth.healthcheck(null, null));
+    assertEquals(ErrorCode.SERVICE_UNAVAILABLE.code, e.code());
+    assertThat(e.getMessage(), containsString("Not in live nodes"));
+  }
+
+  @Test
+  public void testCoreLoadingNotCompleteReturns503() {
+    when(mockCoreContainer.isStatusLoadComplete()).thenReturn(false);
+
+    SolrException e = expectThrows(SolrException.class, () -> 
nodeHealth.healthcheck(true, null));
+    assertEquals(ErrorCode.SERVICE_UNAVAILABLE.code, e.code());
+    assertThat(e.getMessage(), containsString("Core Loading not complete"));
+  }
+
+  @Test
+  public void testRequireHealthyCoresReturnsOkWhenAllCoresHealthy() {
+    when(mockCoreContainer.isStatusLoadComplete()).thenReturn(true);
+    CoreDescriptor activeCore = mockCoreDescriptor(Replica.State.ACTIVE);
+    
when(mockCoreContainer.getCoreDescriptors()).thenReturn(List.of(activeCore));
+
+    final var response = nodeHealth.healthcheck(true, null);
+
+    assertEquals(OK, response.status);
+    assertEquals("All cores are healthy", response.message);
+  }
+
+  @Test
+  public void testUnhealthyCoresReturns503() {
+    when(mockCoreContainer.isStatusLoadComplete()).thenReturn(true);
+    CoreDescriptor recoveringCore = 
mockCoreDescriptor(Replica.State.RECOVERING);
+    
when(mockCoreContainer.getCoreDescriptors()).thenReturn(List.of(recoveringCore));
+    when(mockCoreContainer.getNumAllCores()).thenReturn(1);
+
+    SolrException e = expectThrows(SolrException.class, () -> 
nodeHealth.healthcheck(true, null));
+    assertEquals(ErrorCode.SERVICE_UNAVAILABLE.code, e.code());
+    assertThat(
+        e.getMessage(),
+        containsString("1 out of 1 replicas are currently initializing or 
recovering"));
+  }
+
+  /**
+   * Creates a core descriptor for a core of collection1/slice1 in the given 
state, and points the
+   * mocked cluster state at a matching collection.
+   */
+  private CoreDescriptor mockCoreDescriptor(Replica.State state) {
+    Properties props = new Properties();
+    props.put(CoreDescriptor.CORE_SHARD, "slice1");
+    props.put(CoreDescriptor.CORE_COLLECTION, "collection1");
+    props.put(CoreDescriptor.CORE_NODE_NAME, "slice1_replica1");
+    CloudDescriptor cloudDescriptor = new CloudDescriptor(null, 
"slice1_replica1", props);
+    cloudDescriptor.setHasRegistered(true);
+    cloudDescriptor.setLastPublished(state);
+
+    CoreDescriptor coreDescriptor = mock(CoreDescriptor.class);

Review Comment:
   coredescriptor is pretty simple; just a pojo.  Why mock it when you could 
use the real thing?



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to