|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.oss.driver.internal.core.metadata; |
| 17 | + |
| 18 | +import static com.datastax.oss.driver.Assertions.assertThat; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +import com.datastax.oss.driver.api.core.metadata.EndPoint; |
| 22 | +import com.datastax.oss.driver.api.core.metadata.Node; |
| 23 | +import com.datastax.oss.driver.internal.core.channel.ChannelFactory; |
| 24 | +import com.datastax.oss.driver.internal.core.context.InternalDriverContext; |
| 25 | +import com.datastax.oss.driver.internal.core.metrics.MetricsFactory; |
| 26 | +import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList; |
| 27 | +import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.UUID; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.mockito.Mock; |
| 34 | +import org.mockito.junit.MockitoJUnitRunner; |
| 35 | + |
| 36 | +@RunWith(MockitoJUnitRunner.class) |
| 37 | +public class InitialNodeListRefreshTest { |
| 38 | + |
| 39 | + @Mock private InternalDriverContext context; |
| 40 | + @Mock protected MetricsFactory metricsFactory; |
| 41 | + @Mock private ChannelFactory channelFactory; |
| 42 | + |
| 43 | + private DefaultNode contactPoint1; |
| 44 | + private DefaultNode contactPoint2; |
| 45 | + private EndPoint endPoint3; |
| 46 | + private UUID hostId1; |
| 47 | + private UUID hostId2; |
| 48 | + private UUID hostId3; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setup() { |
| 52 | + when(context.getMetricsFactory()).thenReturn(metricsFactory); |
| 53 | + when(context.getChannelFactory()).thenReturn(channelFactory); |
| 54 | + |
| 55 | + contactPoint1 = TestNodeFactory.newContactPoint(1, context); |
| 56 | + contactPoint2 = TestNodeFactory.newContactPoint(2, context); |
| 57 | + |
| 58 | + endPoint3 = TestNodeFactory.newEndPoint(3); |
| 59 | + hostId1 = UUID.randomUUID(); |
| 60 | + hostId2 = UUID.randomUUID(); |
| 61 | + hostId3 = UUID.randomUUID(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void should_copy_contact_points() { |
| 66 | + // Given |
| 67 | + Iterable<NodeInfo> newInfos = |
| 68 | + ImmutableList.of( |
| 69 | + DefaultNodeInfo.builder() |
| 70 | + .withEndPoint(contactPoint1.getEndPoint()) |
| 71 | + // in practice there are more fields, but hostId is enough to validate the logic |
| 72 | + .withHostId(hostId1) |
| 73 | + .build(), |
| 74 | + DefaultNodeInfo.builder() |
| 75 | + .withEndPoint(contactPoint2.getEndPoint()) |
| 76 | + .withHostId(hostId2) |
| 77 | + .build()); |
| 78 | + InitialNodeListRefresh refresh = |
| 79 | + new InitialNodeListRefresh(newInfos, ImmutableSet.of(contactPoint1, contactPoint2)); |
| 80 | + |
| 81 | + // When |
| 82 | + MetadataRefresh.Result result = refresh.compute(DefaultMetadata.EMPTY, false, context); |
| 83 | + |
| 84 | + // Then |
| 85 | + // contact points have been copied to the metadata, and completed with missing information |
| 86 | + Map<UUID, Node> newNodes = result.newMetadata.getNodes(); |
| 87 | + assertThat(newNodes).containsOnlyKeys(hostId1, hostId2); |
| 88 | + assertThat(newNodes.get(hostId1)).isEqualTo(contactPoint1); |
| 89 | + assertThat(contactPoint1.getHostId()).isEqualTo(hostId1); |
| 90 | + assertThat(newNodes.get(hostId2)).isEqualTo(contactPoint2); |
| 91 | + assertThat(contactPoint2.getHostId()).isEqualTo(hostId2); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void should_add_other_nodes() { |
| 96 | + // Given |
| 97 | + Iterable<NodeInfo> newInfos = |
| 98 | + ImmutableList.of( |
| 99 | + DefaultNodeInfo.builder() |
| 100 | + .withEndPoint(contactPoint1.getEndPoint()) |
| 101 | + // in practice there are more fields, but hostId is enough to validate the logic |
| 102 | + .withHostId(hostId1) |
| 103 | + .build(), |
| 104 | + DefaultNodeInfo.builder() |
| 105 | + .withEndPoint(contactPoint2.getEndPoint()) |
| 106 | + .withHostId(hostId2) |
| 107 | + .build(), |
| 108 | + DefaultNodeInfo.builder().withEndPoint(endPoint3).withHostId(hostId3).build()); |
| 109 | + InitialNodeListRefresh refresh = |
| 110 | + new InitialNodeListRefresh(newInfos, ImmutableSet.of(contactPoint1, contactPoint2)); |
| 111 | + |
| 112 | + // When |
| 113 | + MetadataRefresh.Result result = refresh.compute(DefaultMetadata.EMPTY, false, context); |
| 114 | + |
| 115 | + // Then |
| 116 | + // new node created in addition to the contact points |
| 117 | + Map<UUID, Node> newNodes = result.newMetadata.getNodes(); |
| 118 | + assertThat(newNodes).containsOnlyKeys(hostId1, hostId2, hostId3); |
| 119 | + Node node3 = newNodes.get(hostId3); |
| 120 | + assertThat(node3.getEndPoint()).isEqualTo(endPoint3); |
| 121 | + assertThat(node3.getHostId()).isEqualTo(hostId3); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void should_ignore_duplicate_host_ids() { |
| 126 | + // Given |
| 127 | + Iterable<NodeInfo> newInfos = |
| 128 | + ImmutableList.of( |
| 129 | + DefaultNodeInfo.builder() |
| 130 | + .withEndPoint(contactPoint1.getEndPoint()) |
| 131 | + // in practice there are more fields, but hostId is enough to validate the logic |
| 132 | + .withHostId(hostId1) |
| 133 | + .withDatacenter("dc1") |
| 134 | + .build(), |
| 135 | + DefaultNodeInfo.builder() |
| 136 | + .withEndPoint(contactPoint1.getEndPoint()) |
| 137 | + .withDatacenter("dc2") |
| 138 | + .withHostId(hostId1) |
| 139 | + .build()); |
| 140 | + InitialNodeListRefresh refresh = |
| 141 | + new InitialNodeListRefresh(newInfos, ImmutableSet.of(contactPoint1)); |
| 142 | + |
| 143 | + // When |
| 144 | + MetadataRefresh.Result result = refresh.compute(DefaultMetadata.EMPTY, false, context); |
| 145 | + |
| 146 | + // Then |
| 147 | + // only the first nodeInfo should have been copied |
| 148 | + Map<UUID, Node> newNodes = result.newMetadata.getNodes(); |
| 149 | + assertThat(newNodes).containsOnlyKeys(hostId1); |
| 150 | + assertThat(newNodes.get(hostId1)).isEqualTo(contactPoint1); |
| 151 | + assertThat(contactPoint1.getHostId()).isEqualTo(hostId1); |
| 152 | + assertThat(contactPoint1.getDatacenter()).isEqualTo("dc1"); |
| 153 | + } |
| 154 | +} |
0 commit comments