|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 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 | + * https://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 org.springframework.batch.infrastructure.item.ldif.builder; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Assertions; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.batch.infrastructure.item.ExecutionContext; |
| 22 | +import org.springframework.batch.infrastructure.item.ItemStreamException; |
| 23 | +import org.springframework.batch.infrastructure.item.ldif.LdifReader; |
| 24 | +import org.springframework.batch.infrastructure.item.ldif.RecordCallbackHandler; |
| 25 | +import org.springframework.batch.infrastructure.item.ldif.support.LdifReaderTestSupport; |
| 26 | +import org.springframework.ldap.core.LdapAttributes; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Banseok Kim |
| 34 | + */ |
| 35 | +class LdifReaderBuilderTests extends LdifReaderTestSupport { |
| 36 | + |
| 37 | + private String callbackDn; |
| 38 | + |
| 39 | + @Test |
| 40 | + void itemReaderBasicRead() throws Exception { |
| 41 | + LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).build(); |
| 42 | + |
| 43 | + verify(reader, expectedDns()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void recordsToSkipSkipsFirstRecord() throws Exception { |
| 48 | + LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).recordsToSkip(1).build(); |
| 49 | + |
| 50 | + reader.open(new ExecutionContext()); |
| 51 | + LdapAttributes item = reader.read(); |
| 52 | + assertEquals("cn=Bjorn Jensen,ou=Accounting,dc=airius,dc=com", item.getName().toString()); |
| 53 | + reader.close(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void currentItemCountStartsFromThirdRecord() throws Exception { |
| 58 | + LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).currentItemCount(2).build(); |
| 59 | + |
| 60 | + reader.open(new ExecutionContext()); |
| 61 | + LdapAttributes item = reader.read(); |
| 62 | + assertEquals("cn=Gern Jensen,ou=Product Testing,dc=airius,dc=com", item.getName().toString()); |
| 63 | + reader.close(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void currentItemCountAtEndReturnsNull() throws Exception { |
| 68 | + LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).currentItemCount(3).build(); |
| 69 | + |
| 70 | + reader.open(new ExecutionContext()); |
| 71 | + Assertions.assertNull(reader.read()); |
| 72 | + reader.close(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void maxItemCountLimitsReads() throws Exception { |
| 77 | + LdifReader reader = new LdifReaderBuilder().name("ldif").resource(ldifResource).maxItemCount(1).build(); |
| 78 | + |
| 79 | + reader.open(new ExecutionContext()); |
| 80 | + LdapAttributes first = reader.read(); |
| 81 | + LdapAttributes second = reader.read(); |
| 82 | + assertEquals("cn=Barbara Jensen,ou=Product Development,dc=airius,dc=com", first.getName().toString()); |
| 83 | + assertNull(second); |
| 84 | + reader.close(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void skippedRecordsCallbackIsInvoked() throws Exception { |
| 89 | + this.callbackDn = null; |
| 90 | + |
| 91 | + LdifReader reader = new LdifReaderBuilder().name("ldif") |
| 92 | + .resource(ldifResource) |
| 93 | + .recordsToSkip(1) |
| 94 | + .skippedRecordsCallback(new TestCallback()) |
| 95 | + .build(); |
| 96 | + |
| 97 | + reader.open(new ExecutionContext()); |
| 98 | + reader.read(); |
| 99 | + assertEquals("cn=Barbara Jensen,ou=Product Development,dc=airius,dc=com", this.callbackDn); |
| 100 | + reader.close(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void saveStateUpdatesExecutionContext() throws Exception { |
| 105 | + LdifReader reader = new LdifReaderBuilder().name("foo").resource(ldifResource).build(); |
| 106 | + |
| 107 | + ExecutionContext ec = new ExecutionContext(); |
| 108 | + reader.open(ec); |
| 109 | + reader.read(); |
| 110 | + reader.update(ec); |
| 111 | + |
| 112 | + assertEquals(1, ec.getInt("foo.read.count")); |
| 113 | + reader.close(); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void strictTrueThrowsOnMissingResource() throws Exception { |
| 118 | + LdifReader reader = new LdifReaderBuilder().name("ldif").resource(missingResource()).strict(true).build(); |
| 119 | + |
| 120 | + ItemStreamException ex = assertThrows(ItemStreamException.class, () -> reader.open(new ExecutionContext())); |
| 121 | + assertEquals("Failed to initialize the reader", ex.getMessage()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void strictFalseDoesNotThrowOnMissingResource() throws Exception { |
| 126 | + LdifReader reader = new LdifReaderBuilder().name("ldif").resource(missingResource()).strict(false).build(); |
| 127 | + |
| 128 | + reader.open(new ExecutionContext()); |
| 129 | + reader.close(); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void itemReaderWithNoResourceShouldFail() { |
| 134 | + assertThrows(IllegalArgumentException.class, () -> new LdifReaderBuilder().name("ldif").build()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void itemReaderWithNoNameAndDefaultSaveStateShouldFail() { |
| 139 | + assertThrows(IllegalArgumentException.class, () -> new LdifReaderBuilder().resource(ldifResource).build()); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void itemReaderWithNoNameButSaveStateFalseShouldSucceed() throws Exception { |
| 144 | + LdifReader reader = new LdifReaderBuilder().resource(ldifResource).saveState(false).build(); |
| 145 | + |
| 146 | + verify(reader, expectedDns()); |
| 147 | + } |
| 148 | + |
| 149 | + private class TestCallback implements RecordCallbackHandler { |
| 150 | + |
| 151 | + @Override |
| 152 | + public void handleRecord(LdapAttributes attributes) { |
| 153 | + callbackDn = attributes.getName().toString(); |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments