|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.eventmesh.registry.etcd.service; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import org.apache.eventmesh.api.meta.bo.EventMeshAppSubTopicInfo; |
| 26 | +import org.apache.eventmesh.api.meta.bo.EventMeshServicePubTopicInfo; |
| 27 | +import org.apache.eventmesh.common.utils.JsonUtils; |
| 28 | +import org.apache.eventmesh.meta.etcd.service.EtcdCustomService; |
| 29 | + |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.HashSet; |
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.CompletableFuture; |
| 36 | + |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 40 | +import org.mockito.InjectMocks; |
| 41 | +import org.mockito.Mock; |
| 42 | +import org.mockito.MockitoAnnotations; |
| 43 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 44 | + |
| 45 | +import io.etcd.jetcd.ByteSequence; |
| 46 | +import io.etcd.jetcd.Client; |
| 47 | +import io.etcd.jetcd.KV; |
| 48 | +import io.etcd.jetcd.KeyValue; |
| 49 | +import io.etcd.jetcd.kv.GetResponse; |
| 50 | +import io.etcd.jetcd.options.GetOption; |
| 51 | + |
| 52 | +@ExtendWith(MockitoExtension.class) |
| 53 | +public class EtcdCustomServiceTest { |
| 54 | + |
| 55 | + @Mock |
| 56 | + private Client etcdClient; |
| 57 | + |
| 58 | + @Mock |
| 59 | + private KV kvClient; |
| 60 | + |
| 61 | + @Mock |
| 62 | + private KeyValue keyValue; |
| 63 | + |
| 64 | + @Mock |
| 65 | + private GetResponse getResponse; |
| 66 | + |
| 67 | + @Mock |
| 68 | + private CompletableFuture<GetResponse> futureResponse; |
| 69 | + |
| 70 | + @InjectMocks |
| 71 | + private EtcdCustomService etcdCustomService; |
| 72 | + |
| 73 | + @BeforeEach |
| 74 | + void setUp() { |
| 75 | + MockitoAnnotations.openMocks(this); |
| 76 | + when(etcdClient.getKVClient()).thenReturn(kvClient); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testFindEventMeshServicePubTopicInfos() throws Exception { |
| 81 | + |
| 82 | + EventMeshServicePubTopicInfo mockInfo = new EventMeshServicePubTopicInfo(); |
| 83 | + mockInfo.setService("testService"); |
| 84 | + mockInfo.setTopics(Collections.unmodifiableSet(new HashSet<>(Arrays.asList("topic1", "topic2")))); |
| 85 | + |
| 86 | + String mockValue = JsonUtils.toJSONString(mockInfo); |
| 87 | + ByteSequence mockByteSequence = ByteSequence.from(mockValue, StandardCharsets.UTF_8); |
| 88 | + |
| 89 | + when(keyValue.getValue()).thenReturn(mockByteSequence); |
| 90 | + when(getResponse.getKvs()).thenReturn(Arrays.asList(keyValue)); |
| 91 | + when(futureResponse.get()).thenReturn(getResponse); |
| 92 | + when(kvClient.get(any(ByteSequence.class), any(GetOption.class))).thenReturn(futureResponse); |
| 93 | + |
| 94 | + List<EventMeshServicePubTopicInfo> result = etcdCustomService.findEventMeshServicePubTopicInfos(); |
| 95 | + assertNotNull(result); |
| 96 | + assertEquals(1, result.size()); |
| 97 | + EventMeshServicePubTopicInfo resultInfo = result.get(0); |
| 98 | + assertEquals("testService", resultInfo.getService()); |
| 99 | + assertEquals(new HashSet<>(Arrays.asList("topic1", "topic2")), resultInfo.getTopics()); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testFindEventMeshAppSubTopicInfoByGroup() throws Exception { |
| 105 | + |
| 106 | + String group = "testGroup"; |
| 107 | + EventMeshAppSubTopicInfo mockInfo = new EventMeshAppSubTopicInfo(); |
| 108 | + |
| 109 | + String mockValue = JsonUtils.toJSONString(mockInfo); |
| 110 | + ByteSequence mockByteSequence = ByteSequence.from(mockValue, StandardCharsets.UTF_8); |
| 111 | + |
| 112 | + when(keyValue.getValue()).thenReturn(mockByteSequence); |
| 113 | + when(kvClient.get(any(ByteSequence.class), any(GetOption.class))).thenReturn(futureResponse); |
| 114 | + when(futureResponse.get()).thenReturn(getResponse); |
| 115 | + when(getResponse.getKvs()).thenReturn(Collections.singletonList(keyValue)); |
| 116 | + |
| 117 | + EventMeshAppSubTopicInfo result = etcdCustomService.findEventMeshAppSubTopicInfoByGroup(group); |
| 118 | + |
| 119 | + assertNotNull(result); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments