|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | +import static org.mockito.ArgumentMatchers.any; |
| 13 | +import static org.mockito.Mockito.mock; |
| 14 | +import static org.mockito.Mockito.verify; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | + |
| 17 | +import java.io.InputStream; |
| 18 | +import java.lang.reflect.Type; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.nio.file.Paths; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.DisplayName; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import io.jooby.exception.MissingValueException; |
| 30 | +import io.jooby.value.Value; |
| 31 | + |
| 32 | +public class BodyTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + @DisplayName("Verify value(Charset) decoding branches") |
| 36 | + void testValueCharset() { |
| 37 | + Body body = mock(Body.class); |
| 38 | + when(body.value(any(java.nio.charset.Charset.class))).thenCallRealMethod(); |
| 39 | + |
| 40 | + // Branch 1: Missing value (empty byte array) |
| 41 | + when(body.bytes()).thenReturn(new byte[0]); |
| 42 | + assertThrows(MissingValueException.class, () -> body.value(StandardCharsets.UTF_8)); |
| 43 | + |
| 44 | + // Branch 2: Successful decode |
| 45 | + String testContent = "jooby-body"; |
| 46 | + when(body.bytes()).thenReturn(testContent.getBytes(StandardCharsets.UTF_8)); |
| 47 | + assertEquals(testContent, body.value(StandardCharsets.UTF_8)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @DisplayName("Verify size() returns 1 as defined by default method") |
| 52 | + void testSize() { |
| 53 | + Body body = mock(Body.class); |
| 54 | + when(body.size()).thenCallRealMethod(); |
| 55 | + assertEquals(1, body.size()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @DisplayName("Verify get(int) delegates to get(String)") |
| 60 | + void testGetInt() { |
| 61 | + Body body = mock(Body.class); |
| 62 | + Value expectedValue = mock(Value.class); |
| 63 | + |
| 64 | + when(body.get("5")).thenReturn(expectedValue); |
| 65 | + when(body.get(5)).thenCallRealMethod(); |
| 66 | + |
| 67 | + assertEquals(expectedValue, body.get(5)); |
| 68 | + verify(body).get("5"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @DisplayName("Verify iterator wraps the body instance") |
| 73 | + void testIterator() { |
| 74 | + Body body = mock(Body.class); |
| 75 | + when(body.iterator()).thenCallRealMethod(); |
| 76 | + |
| 77 | + Iterator<Value> iterator = body.iterator(); |
| 78 | + |
| 79 | + assertTrue(iterator.hasNext()); |
| 80 | + assertEquals(body, iterator.next()); |
| 81 | + assertFalse(iterator.hasNext()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + @DisplayName("Verify toList(Class) delegates to to(Type) via Reified") |
| 86 | + void testToListClass() { |
| 87 | + Body body = mock(Body.class); |
| 88 | + List<String> expectedList = List.of("a", "b"); |
| 89 | + |
| 90 | + // We capture the Reified Type delegation |
| 91 | + when(body.to(any(Type.class))).thenReturn(expectedList); |
| 92 | + when(body.toList(String.class)).thenCallRealMethod(); |
| 93 | + |
| 94 | + assertEquals(expectedList, body.toList(String.class)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + @DisplayName("Verify string-based toList() and toSet() collections") |
| 99 | + void testStringCollections() { |
| 100 | + Body body = mock(Body.class); |
| 101 | + when(body.value()).thenReturn("test-value"); |
| 102 | + when(body.toList()).thenCallRealMethod(); |
| 103 | + when(body.toSet()).thenCallRealMethod(); |
| 104 | + |
| 105 | + assertEquals(List.of("test-value"), body.toList()); |
| 106 | + assertEquals(Set.of("test-value"), body.toSet()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @DisplayName("Verify class-based conversions delegate to type-based conversions") |
| 111 | + void testClassConversions() { |
| 112 | + Body body = mock(Body.class); |
| 113 | + |
| 114 | + when(body.to((Type) Integer.class)).thenReturn(100); |
| 115 | + when(body.to(Integer.class)).thenCallRealMethod(); |
| 116 | + assertEquals(100, body.to(Integer.class)); |
| 117 | + |
| 118 | + when(body.toNullable((Type) Long.class)).thenReturn(200L); |
| 119 | + when(body.toNullable(Long.class)).thenCallRealMethod(); |
| 120 | + assertEquals(200L, body.toNullable(Long.class)); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + @DisplayName("Verify static factory methods route to correct internal implementations") |
| 125 | + void testStaticFactories() { |
| 126 | + Context ctx = mock(Context.class); |
| 127 | + |
| 128 | + // Using getClass().getSimpleName() avoids restricted visibility issues |
| 129 | + // if internal classes are package-private while ensuring the correct factory was invoked. |
| 130 | + |
| 131 | + Body emptyBody = Body.empty(ctx); |
| 132 | + assertEquals("ByteArrayBody", emptyBody.getClass().getSimpleName()); |
| 133 | + |
| 134 | + Body bytesBody = Body.of(ctx, new byte[] {1, 2, 3}); |
| 135 | + assertEquals("ByteArrayBody", bytesBody.getClass().getSimpleName()); |
| 136 | + |
| 137 | + InputStream stream = mock(InputStream.class); |
| 138 | + Body streamBody = Body.of(ctx, stream, 1024L); |
| 139 | + assertEquals("InputStreamBody", streamBody.getClass().getSimpleName()); |
| 140 | + |
| 141 | + Path file = Paths.get("dummy.txt"); |
| 142 | + Body fileBody = Body.of(ctx, file); |
| 143 | + assertEquals("FileBody", fileBody.getClass().getSimpleName()); |
| 144 | + } |
| 145 | +} |
0 commit comments