Skip to content

Commit

Permalink
feat: add partition
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Dec 11, 2023
1 parent 8b47501 commit 167595e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
Empty file removed cdi/src/test/java/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.qwlabs.cdi.partition;


import jakarta.interceptor.InvocationContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collection;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class PartitionInterceptorTest {
private PartitionInterceptor interceptor;

@BeforeEach
void setUp() {
interceptor = new PartitionInterceptor();
}

@Test
void should_call_twice(@Mock InvocationContext context) throws Exception {
TestClass target = new TestClass();
var method = target.getClass().getMethod("testMethod", String.class, Collection.class);
var parameters1 = "org";
var parameters2 = List.of("1", "2", "3");
when(context.getTarget()).thenReturn(target);
when(context.getMethod()).thenReturn(method);
when(context.getParameters()).thenReturn(new Object[]{parameters1, parameters2});
when(context.proceed()).thenReturn(List.of("r1", "r2"), List.of("r3", "r4"));

var result = interceptor.intercept(context);

assertThat(result, is(List.of("r1", "r2", "r3", "r4")));
verify(context, times(2)).proceed();
}

private static class TestClass {
@Partition(size = 2)
public List<String> testMethod(String orgId, @PartitionBy Collection<String> ids) {
return List.of("1");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.qwlabs.cdi.partition;

import com.qwlabs.exceptions.CodeException;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;


class PartitionResultTest {
@Test
void should_return_stream() {
Stream<Stream> data = Stream.of(Stream.of("1"), Stream.of("2"));

var result = PartitionResult.get(data, Stream.class);

assertThat(result.toList(), is(List.of("1", "2")));
}

@Test
void should_return_set() {
Stream<Set> data = Stream.of(Set.of("1"), Set.of("2"));

var result = PartitionResult.get(data, Set.class);

assertThat(result, is(Set.of("1", "2")));
}

@Test
void should_return_list() {
Stream<List> data = Stream.of(List.of("1"), List.of("2"));

var result = PartitionResult.get(data, List.class);

assertThat(result, is(List.of("1", "2")));
}

@Test
void should_return_integer() {
Stream<Integer> data = Stream.of(1, 2);

var result = PartitionResult.get(data, Integer.class);

assertThat(result, is(3));
}

@Test
void should_return_long() {
Stream<Long> data = Stream.of(1L, 2L);

var result = PartitionResult.get(data, Long.class);

assertThat(result, is(3L));
}

@Test
void should_exception() {
Stream<Map> data = Stream.of(Map.of("a", "b"));

var exception = assertThrows(CodeException.class, () -> PartitionResult.get(data, Map.class));

assertThat(exception.getMessage(), is("代码错误: PartitionResult can not support type java.util.Map"));
}
}

0 comments on commit 167595e

Please sign in to comment.