mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Prashant
01.48.2025 8ffab931c66010826ed83890526e9d1deead1206
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 *  Copyright 2016 ForgeRock AS.
 */
package com.forgerock.opendj.ldap.tools;
 
import static com.forgerock.opendj.cli.CliMessages.ERR_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS;
import static com.forgerock.opendj.ldap.tools.LDIFDiff.DIFFERENCES_FOUND;
import static com.forgerock.opendj.ldap.tools.LDIFDiff.NO_DIFFERENCES_FOUND;
import static com.forgerock.opendj.ldap.tools.ToolsMessages.ERR_LDIFDIFF_MULTIPLE_USES_OF_STDIN;
import static com.forgerock.opendj.ldap.tools.ToolsTestUtils.addValueNeededLongArgs;
import static com.forgerock.opendj.ldap.tools.ToolsTestUtils.addValueNeededShortArgs;
import static com.forgerock.opendj.ldap.tools.ToolsTestUtils.args;
import static com.forgerock.opendj.ldap.tools.ToolsTestUtils.calcChecksum;
import static com.forgerock.opendj.ldap.tools.ToolsTestUtils.toDataProviderArray;
import static org.fest.assertions.Assertions.assertThat;
 
import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.ldap.ResultCode;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/** A set of test cases for the LDIFDiff tool. */
@Test
public class LDIFDiffTestCase extends ToolsTestCase {
 
    private static final String LDIF_RESOURCES_PATH =
            Paths.get("src", "test", "resources", "ldifdiff").toAbsolutePath().toString();
 
    /**
     * Retrieves sets of invalid arguments that may not be used to initialize the LDAPSearch tool.
     *
     * @return Sets of invalid arguments that may not be used to initialize the LDAPSearch tool.
     */
    @DataProvider
    public Object[][] invalidArgs() {
        final List<List<String>> argLists = new ArrayList<>();
        final List<LocalizableMessage> reasonList = new ArrayList<>();
 
        argLists.add(args());
        reasonList.add(ERR_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS.get(2));
 
        addValueNeededShortArgs(argLists, reasonList, "o");
        addValueNeededLongArgs(argLists, reasonList, "outputLdif");
 
        return toDataProviderArray(argLists, reasonList);
    }
 
    /**
     * Retrieves the names of the files that should be used when testing the
     * ldifdiff tool.  Each element of the outer array should be an array
     * containing the following elements:
     * <OL>
     * <LI>The path to the source LDIF file</LI>
     * <LI>The path to the target LDIF file</LI>
     * <LI>The path to the diff file, or {@code null} if the diff is supposed
     * to fail</LI>
     * </OL>
     */
    @DataProvider
    public Object[][] testDataNoDiffs() {
        return new Object[][] {
            // Both files are empty.
            { "source-empty.ldif", "target-empty.ldif" },
 
            // Both files are the single-entry source.
            { "source-singleentry.ldif", "source-singleentry.ldif" },
 
            // Both files are the single-entry target.
            { "target-singleentry.ldif", "target-singleentry.ldif" },
 
            // Both files are the multiple-entry source.
            { "source-multipleentries.ldif", "source-multipleentries.ldif" },
 
            // Both files are the multiple-entry target.
            { "target-multipleentries.ldif", "target-multipleentries.ldif" },
        };
    }
 
    @Test(dataProvider = "testDataNoDiffs")
    public void testLdifDiffWithoutDiffs(final String sourceFileName, final String targetFileName) throws Exception {
        testLdifDiff(sourceFileName, targetFileName, "diff-nochanges.ldif");
    }
 
    /**
     * Retrieves the names of the files that should be used when testing the
     * ldifdiff tool.  Each element of the outer array should be an array
     * containing the following elements:
     * <OL>
     * <LI>The path to the source LDIF file</LI>
     * <LI>The path to the target LDIF file</LI>
     * <LI>The path to the diff file, or {@code null} if the diff is supposed
     * to fail</LI>
     * </OL>
     */
    @DataProvider
    public Object[][] testDataWithDiffs() {
        return new Object[][] {
            // The source is empty but the target has a single entry.
            { "source-empty.ldif", "target-singleentry.ldif", "diff-emptytosingle.ldif" },
 
            // The source has a single entry but the target is empty.
            { "source-singleentry.ldif", "target-empty.ldif", "diff-singletoempty.ldif" },
 
            // Make a change to only a single entry in the source->target direction.
            { "source-singleentry.ldif", "target-singleentry.ldif", "diff-singleentry.ldif" },
 
            // Make a change to only a single entry in the target->source direction.
            { "target-singleentry.ldif", "source-singleentry.ldif", "diff-singleentry-reverse.ldif" },
 
            // Make changes to multiple entries in the source->target direction.
            { "source-multipleentries.ldif", "target-multipleentries.ldif", "diff-multipleentries.ldif" },
 
            // Make changes to multiple entries in the target->source direction.
            { "target-multipleentries.ldif", "source-multipleentries.ldif", "diff-multipleentries-reverse.ldif" },
 
            // Go from one entry to multiple in the source->target direction.
            { "source-singleentry.ldif", "target-multipleentries.ldif", "diff-singletomultiple.ldif" },
 
            // Go from one entry to multiple in the target->source direction.
            { "target-singleentry.ldif", "source-multipleentries.ldif", "diff-singletomultiple-reverse.ldif" },
 
            // Go from multiple entries to one in the source->target direction.
            { "source-multipleentries.ldif", "target-singleentry.ldif", "diff-multipletosingle.ldif" },
 
            // Go from multiple entries to one in the target->source direction.
            { "target-multipleentries.ldif", "source-singleentry.ldif", "diff-multipletosingle-reverse.ldif" }
        };
    }
 
    @Test(dataProvider = "testDataWithDiffs")
    public void testReconstructWithLdifModify(final String sourceFileName,
                                              final String targetFileName,
                                              final String expectedResultFileName) throws Exception {
        final String ldifDiffResultFilePath = testLdifDiff(sourceFileName, targetFileName, expectedResultFileName);
 
        final File ldifModifyResultFile = new File(LDIF_RESOURCES_PATH, "ldifDiffAndModifyTestCase.ldif");
        ldifModifyResultFile.deleteOnExit();
 
        final int ldifModifyRes = Utils.runTool(new LDIFModify(outStream, errStream),
                                                "-o", ldifModifyResultFile.getAbsolutePath(),
                                                absolutePath(sourceFileName),
                                                ldifDiffResultFilePath);
        assertThat(ldifModifyRes).isEqualTo(ResultCode.SUCCESS.intValue());
 
        testLdifDiff(targetFileName, "ldifDiffAndModifyTestCase.ldif", "diff-nochanges.ldif");
    }
 
    private String testLdifDiff(final String sourceFileName,
                                final String targetFileName,
                                final String expectedResultFileName) throws Exception {
        final String ldifDiffOutputFilePath = ToolsTestUtils.createTempFile();
        final int res = runTool("-o", ldifDiffOutputFilePath,
                                absolutePath(sourceFileName),
                                absolutePath(targetFileName));
 
        final boolean expectNoDiffs = expectedResultFileName.equals("diff-nochanges.ldif");
        assertThat(res).isEqualTo(expectNoDiffs ? NO_DIFFERENCES_FOUND
                                                : DIFFERENCES_FOUND);
        assertThat(calcChecksum(ldifDiffOutputFilePath)).isEqualTo(
                   calcChecksum(absolutePath(expectedResultFileName)));
 
        return ldifDiffOutputFilePath;
    }
 
    @Test
    public void testLdifDiffWithoutWrapping() throws Exception {
        final String ldifDiffOutputFilePath = ToolsTestUtils.createTempFile();
        final int res = runTool("-o", ldifDiffOutputFilePath,
                                "-t", "0",
                                absolutePath("source-singleentry.ldif"),
                                absolutePath("target-multipleentries.ldif"));
        assertThat(res).isEqualTo(DIFFERENCES_FOUND);
        assertThat(calcChecksum(ldifDiffOutputFilePath)).isEqualTo(
                   calcChecksum(absolutePath("diff-singletomultiple-no-wrapping.ldif")));
    }
 
    @Test
    public void testLdifDiffWithBothSourceAndTargetOnStdin() throws Exception {
        final String ldifDiffOutputFilePath = ToolsTestUtils.createTempFile();
        final int res = runTool("-o", ldifDiffOutputFilePath,
                                "--",
                                "-",
                                "-");
        assertThat(res).isEqualTo(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue());
        assertThat(errOnSingleLine()).contains(ERR_LDIFDIFF_MULTIPLE_USES_OF_STDIN.get().toString());
    }
 
    private String absolutePath(final String fileName) {
        return LDIF_RESOURCES_PATH + File.separator + fileName;
    }
 
    @Override
    ToolConsoleApplication createInstance() {
        return new LDIFDiff(outStream, errStream);
    }
}