001/*
002 * Copyright (C) 2022 - 2024, the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.github.ascopes.jct.assertions;
017
018import static io.github.ascopes.jct.utils.IoExceptionUtils.uncheckedIo;
019import static io.github.ascopes.jct.utils.IterableUtils.requireAtLeastOne;
020import static io.github.ascopes.jct.utils.IterableUtils.requireNonNullValues;
021import static io.github.ascopes.jct.utils.StringUtils.quoted;
022import static io.github.ascopes.jct.utils.StringUtils.quotedIterable;
023import static java.util.function.Predicate.not;
024import static org.assertj.core.api.Assertions.assertThat;
025
026import io.github.ascopes.jct.containers.PackageContainerGroup;
027import io.github.ascopes.jct.repr.LocationRepresentation;
028import io.github.ascopes.jct.utils.StringUtils;
029import java.nio.file.Path;
030import java.util.ArrayList;
031import java.util.List;
032import java.util.Objects;
033import java.util.Optional;
034import java.util.Set;
035import java.util.stream.Collectors;
036import java.util.stream.StreamSupport;
037import org.assertj.core.api.AbstractPathAssert;
038import org.assertj.core.description.TextDescription;
039import org.assertj.core.error.MultipleAssertionsError;
040import org.jspecify.annotations.Nullable;
041
042/**
043 * Assertions for package container groups.
044 *
045 * @author Ashley Scopes
046 * @since 0.0.1
047 */
048public final class PackageContainerGroupAssert
049    extends AbstractContainerGroupAssert<PackageContainerGroupAssert, PackageContainerGroup> {
050
051  /**
052   * Initialize the container group assertions.
053   *
054   * @param containerGroup the container group to assert upon.
055   */
056  public PackageContainerGroupAssert(@Nullable PackageContainerGroup containerGroup) {
057    super(containerGroup, PackageContainerGroupAssert.class);
058  }
059
060  /**
061   * Assert that all given files exist.
062   *
063   * @param paths paths to check for.
064   * @return this object for further call chaining.
065   * @throws AssertionError           if the container group is null, or if any of the files do not
066   *                                  exist.
067   * @throws NullPointerException     if any of the paths are null.
068   * @throws IllegalArgumentException if no fragments are provided.
069   */
070  public PackageContainerGroupAssert allFilesExist(String... paths) {
071    requireNonNullValues(paths, "paths");
072    requireAtLeastOne(paths, "paths");
073
074    allFilesExist(List.of(paths));
075    return this;
076  }
077
078  /**
079   * Assert that all given files exist.
080   *
081   * @param paths paths to check for.
082   * @return this object for further call chaining.
083   * @throws AssertionError       if the container group is null, or if any of the files do not
084   *                              exist.
085   * @throws NullPointerException if any of the paths are null.
086   */
087  public PackageContainerGroupAssert allFilesExist(Iterable<String> paths) {
088    requireNonNullValues(paths, "paths");
089
090    isNotNull();
091
092    var errors = new ArrayList<AssertionError>();
093
094    for (var path : paths) {
095      try {
096        fileExists(path);
097      } catch (AssertionError ex) {
098        errors.add(ex);
099      }
100    }
101
102    if (errors.isEmpty()) {
103      return this;
104    }
105
106    throw new MultipleAssertionsError(
107        new TextDescription(
108            "Expected all paths in %s to exist but one or more did not",
109            quotedIterable(paths)
110        ),
111        errors
112    );
113  }
114
115  /**
116   * Get assertions to perform on the class loader associated with this container group.
117   *
118   * @return the assertions to perform.
119   * @throws AssertionError if the container group is null.
120   */
121  public ClassLoaderAssert classLoader() {
122    isNotNull();
123
124    return new ClassLoaderAssert(actual.getClassLoader());
125  }
126
127  /**
128   * Assert that the given file does not exist.
129   *
130   * <pre><code>
131   *   // Using platform-specific separators.
132   *   assertions.fileDoesNotExist("foo/bar/baz.txt")...;
133   *
134   *   // Letting JCT infer the correct path separators to use (recommended).
135   *   assertions.fileDoesNotExist("foo", "bar", "baz.txt");
136   * </code></pre>
137   *
138   * @param fragments any additional parts of the path.
139   * @return this assertion object for further assertions.
140   * @throws AssertionError           if the file exists, or if the container group is null.
141   * @throws NullPointerException     if any of the fragments are null.
142   * @throws IllegalArgumentException if no fragments are provided.
143   */
144  public PackageContainerGroupAssert fileDoesNotExist(String... fragments) {
145    requireNonNullValues(fragments, "fragments");
146    requireAtLeastOne(fragments, "fragments");
147
148    isNotNull();
149
150    var actualFile = actual.getFile(fragments);
151
152    if (actualFile == null) {
153      return this;
154    }
155
156    throw failure(
157        "Expected path %s to not exist in %s but it was found at %s",
158        quotedUserProvidedPath(List.of(fragments)),
159        LocationRepresentation.getInstance().toStringOf(actual.getLocation()),
160        quoted(actualFile)
161    );
162  }
163
164  /**
165   * Assert that the given file exists.
166   *
167   * <pre><code>
168   *   // Letting JCT infer the correct path separators to use (recommended).
169   *   assertions.fileExists("foo", "bar", "baz.txt");
170   *
171   *   // Using platform-specific separators (more likely to produce unexpected results).
172   *   assertions.fileExists("foo/bar/baz.txt")...;
173   * </code></pre>
174   *
175   * <p>If the file does not exist, then this object will attempt to find the
176   * closest matches and list them in an error message along with the assertion error.
177   *
178   * @param fragments parts of the path.
179   * @return assertions to perform on the path of the file that exists.
180   * @throws AssertionError           if the file does not exist, or if the container group is
181   *                                  null.
182   * @throws NullPointerException     if any of the fragments are null.
183   * @throws IllegalArgumentException if no fragments are provided.
184   */
185  public AbstractPathAssert<?> fileExists(String... fragments) {
186    requireNonNullValues(fragments, "fragments");
187    requireAtLeastOne(fragments, "fragments");
188
189    isNotNull();
190
191    var actualFile = actual.getFile(fragments);
192
193    if (actualFile != null) {
194      // File exists with this path. Hooray, lets return assertions on it.
195      return assertThat(actualFile);
196    }
197
198    var expected = List.of(fragments);
199    var message = StringUtils.resultNotFoundWithFuzzySuggestions(
200        fuzzySafePath(expected),
201        quotedUserProvidedPath(expected),
202        listAllUniqueFilesForAllContainers(),
203        this::fuzzySafePath,
204        this::quotedUserProvidedPath,
205        "file with relative path"
206    );
207
208    throw failure(message);
209  }
210
211  private Set<Path> listAllUniqueFilesForAllContainers() {
212    return uncheckedIo(actual::listAllFiles)
213        .keySet()
214        .stream()
215        // Make all the files relative to their roots.
216        .flatMap(container -> uncheckedIo(container::listAllFiles)
217            .stream()
218            .map(container.getInnerPathRoot().getPath()::relativize))
219        // Filter out the inner path root itself, preventing few confusing issues with zero-length
220        // file names. In ZIP paths this can also be null, so we have to check for both "" and null
221        // here.
222        .filter(this::fileNameIsPresent)
223        // Remove duplicates (don't think this can ever happen but this is just to be safe).
224        .collect(Collectors.toSet());
225  }
226
227  private <T> String quotedUserProvidedPath(Iterable<T> parts) {
228    return StreamSupport
229        .stream(parts.spliterator(), false)
230        .map(Objects::toString)
231        .collect(Collectors.collectingAndThen(
232            Collectors.joining("/"),
233            StringUtils::quoted
234        ));
235  }
236
237  private <T> String fuzzySafePath(Iterable<T> parts) {
238    // Join on null bytes as we don't ever use those in normal path names. This way, we ignore
239    // file-system and OS-specific path separators creating ambiguities (like how Windows uses
240    // backslashes rather than forward slashes to delimit paths).
241    return StreamSupport
242        .stream(parts.spliterator(), false)
243        .map(Objects::toString)
244        .collect(Collectors.joining("\0"));
245  }
246
247  private boolean fileNameIsPresent(@Nullable Path path) {
248    // Path can be null if no path elements exist in ZipPath implementations.
249    return Optional
250        .ofNullable(path)
251        .map(Path::getFileName)
252        .map(Path::toString)
253        .filter(not(String::isBlank))
254        .isPresent();
255  }
256}