|
| 1 | +package org.openapitools.codegen; |
| 2 | + |
| 3 | +import org.openapitools.codegen.templating.TemplateManagerOptions; |
| 4 | +import org.testng.annotations.Test; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import static org.testng.Assert.*; |
| 13 | + |
| 14 | +public class DryRunTemplateManagerTest { |
| 15 | + @Test |
| 16 | + public void testError() throws IOException { |
| 17 | + final DryRunTemplateManager templateManager = getTemplateManager(false, false); |
| 18 | + final File tempFile = File.createTempFile("dryrun-test", ".txt"); |
| 19 | + tempFile.deleteOnExit(); |
| 20 | + |
| 21 | + templateManager.error(tempFile.toPath(), "errored"); |
| 22 | + final Map<String, DryRunStatus> result = templateManager.getDryRunStatusMap(); |
| 23 | + |
| 24 | + assertEquals(result.size(), 1); |
| 25 | + assertDryRunStatus( |
| 26 | + result.get(tempFile.getAbsolutePath()), |
| 27 | + tempFile.toPath(), |
| 28 | + DryRunStatus.State.Error, |
| 29 | + "errored" |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testIgnore() throws IOException { |
| 35 | + final DryRunTemplateManager templateManager = getTemplateManager(false, false); |
| 36 | + final File tempFile = File.createTempFile("dryrun-test", ".txt"); |
| 37 | + tempFile.deleteOnExit(); |
| 38 | + |
| 39 | + templateManager.ignore(tempFile.toPath(), "ignored"); |
| 40 | + final Map<String, DryRunStatus> result = templateManager.getDryRunStatusMap(); |
| 41 | + |
| 42 | + assertEquals(result.size(), 1); |
| 43 | + assertDryRunStatus( |
| 44 | + result.get(tempFile.getAbsolutePath()), |
| 45 | + tempFile.toPath(), |
| 46 | + DryRunStatus.State.Ignored, |
| 47 | + "ignored" |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testWrite() throws IOException { |
| 53 | + DryRunTemplateManager templateManager = getTemplateManager(false, false); |
| 54 | + final File tempFile = File.createTempFile("dryrun-test", ".txt"); |
| 55 | + final Map<String, Object> dummyData = new HashMap<>(); |
| 56 | + |
| 57 | + templateManager.write(dummyData, "dummy", tempFile); |
| 58 | + Map<String, DryRunStatus> result = templateManager.getDryRunStatusMap(); |
| 59 | + |
| 60 | + assertEquals(result.size(), 1); |
| 61 | + assertDryRunStatus( |
| 62 | + result.get(tempFile.getAbsolutePath()), |
| 63 | + tempFile.toPath(), |
| 64 | + DryRunStatus.State.Write, |
| 65 | + "File will be written." |
| 66 | + ); |
| 67 | + |
| 68 | + templateManager = getTemplateManager(true, false); |
| 69 | + templateManager.write(dummyData, "dummy", tempFile); |
| 70 | + result = templateManager.getDryRunStatusMap(); |
| 71 | + |
| 72 | + assertEquals(result.size(), 1); |
| 73 | + assertDryRunStatus( |
| 74 | + result.get(tempFile.getAbsolutePath()), |
| 75 | + tempFile.toPath(), |
| 76 | + DryRunStatus.State.WriteIfNewer, |
| 77 | + "File will be written only if it is new or if contents differ from an existing file." |
| 78 | + ); |
| 79 | + |
| 80 | + templateManager = getTemplateManager(true, true); |
| 81 | + templateManager.write(dummyData, "dummy", tempFile); |
| 82 | + result = templateManager.getDryRunStatusMap(); |
| 83 | + |
| 84 | + assertEquals(result.size(), 1); |
| 85 | + assertDryRunStatus( |
| 86 | + result.get(tempFile.getAbsolutePath()), |
| 87 | + tempFile.toPath(), |
| 88 | + DryRunStatus.State.SkippedOverwrite, |
| 89 | + "File exists and skip overwrite option is enabled." |
| 90 | + ); |
| 91 | + |
| 92 | + tempFile.delete(); |
| 93 | + |
| 94 | + templateManager = getTemplateManager(true, false); |
| 95 | + templateManager.write(dummyData, "dummy", tempFile); |
| 96 | + result = templateManager.getDryRunStatusMap(); |
| 97 | + |
| 98 | + assertEquals(result.size(), 1); |
| 99 | + assertDryRunStatus( |
| 100 | + result.get(tempFile.getAbsolutePath()), |
| 101 | + tempFile.toPath(), |
| 102 | + DryRunStatus.State.WriteIfNewer, |
| 103 | + "File will be written only if it is new or if contents differ from an existing file." |
| 104 | + ); |
| 105 | + |
| 106 | + templateManager = getTemplateManager(false, false); |
| 107 | + templateManager.write(dummyData, "dummy", tempFile); |
| 108 | + result = templateManager.getDryRunStatusMap(); |
| 109 | + |
| 110 | + assertEquals(result.size(), 1); |
| 111 | + assertDryRunStatus( |
| 112 | + result.get(tempFile.getAbsolutePath()), |
| 113 | + tempFile.toPath(), |
| 114 | + DryRunStatus.State.Write, |
| 115 | + "File will be written." |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testSkip() throws IOException { |
| 121 | + DryRunTemplateManager templateManager = getTemplateManager(false, false); |
| 122 | + final File tempFile = File.createTempFile("dryrun-test", ".txt"); |
| 123 | + tempFile.deleteOnExit(); |
| 124 | + |
| 125 | + templateManager.skip(tempFile.toPath(), "skipped"); |
| 126 | + Map<String, DryRunStatus> result = templateManager.getDryRunStatusMap(); |
| 127 | + |
| 128 | + assertEquals(result.size(), 1); |
| 129 | + assertDryRunStatus( |
| 130 | + result.get(tempFile.getAbsolutePath()), |
| 131 | + tempFile.toPath(), |
| 132 | + DryRunStatus.State.Skipped, |
| 133 | + "skipped" |
| 134 | + ); |
| 135 | + |
| 136 | + templateManager = getTemplateManager(false, true); |
| 137 | + |
| 138 | + templateManager.skip(tempFile.toPath(), "skipped"); |
| 139 | + result = templateManager.getDryRunStatusMap(); |
| 140 | + |
| 141 | + assertEquals(result.size(), 1); |
| 142 | + assertDryRunStatus( |
| 143 | + result.get(tempFile.getAbsolutePath()), |
| 144 | + tempFile.toPath(), |
| 145 | + DryRunStatus.State.SkippedOverwrite, |
| 146 | + "File is configured not to overwrite an existing file of the same name." |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + private DryRunTemplateManager getTemplateManager(final boolean minimalUpdate, final boolean skipOverwrite) { |
| 151 | + return new DryRunTemplateManager( |
| 152 | + new TemplateManagerOptions(minimalUpdate, skipOverwrite) |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + private void assertDryRunStatus( |
| 157 | + final DryRunStatus dryRunStatus, |
| 158 | + final Path path, |
| 159 | + final DryRunStatus.State state, |
| 160 | + final String reason |
| 161 | + ) { |
| 162 | + assertEquals(dryRunStatus.getPath().toString(), path.toString()); |
| 163 | + assertEquals(dryRunStatus.getState().toString(), state.toString()); |
| 164 | + assertEquals(dryRunStatus.getReason(), reason); |
| 165 | + } |
| 166 | +} |
0 commit comments