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 java.util.Objects.requireNonNull;
019
020import io.github.ascopes.jct.containers.ModuleContainerGroup;
021import io.github.ascopes.jct.filemanagers.ModuleLocation;
022import io.github.ascopes.jct.utils.StringUtils;
023import org.jspecify.annotations.Nullable;
024
025/**
026 * Assertions for module container groups.
027 *
028 * @author Ashley Scopes
029 * @since 0.0.1
030 */
031public final class ModuleContainerGroupAssert
032    extends AbstractContainerGroupAssert<ModuleContainerGroupAssert, ModuleContainerGroup> {
033
034  /**
035   * Initialize the container group assertions.
036   *
037   * @param containerGroup the container group to assert upon.
038   */
039  public ModuleContainerGroupAssert(@Nullable ModuleContainerGroup containerGroup) {
040    super(containerGroup, ModuleContainerGroupAssert.class);
041  }
042
043  /**
044   * Assert that the given module exists and then return assertions to perform on that module.
045   *
046   * @param module the module name.
047   * @return the assertions to perform on the package container group.
048   * @throws AssertionError       if the container group is null or if the module does not exist.
049   * @throws NullPointerException if the module parameter is null.
050   */
051  public PackageContainerGroupAssert moduleExists(String module) {
052    requireNonNull(module, "module");
053    isNotNull();
054
055    var moduleGroup = actual.getModule(module);
056
057    if (moduleGroup != null) {
058      return new PackageContainerGroupAssert(moduleGroup);
059    }
060
061    throw failure(StringUtils.resultNotFoundWithFuzzySuggestions(
062        module,
063        module,
064        actual.getModules().keySet(),
065        ModuleLocation::getModuleName,
066        ModuleLocation::getModuleName,
067        "module"
068    ));
069  }
070
071  /**
072   * Assert that the given module does not exist.
073   *
074   * @param module the module name.
075   * @return this assertion object for further assertion calls.
076   * @throws AssertionError       if the container group is null or if the module exists.
077   * @throws NullPointerException if the module parameter is null.
078   */
079  public ModuleContainerGroupAssert moduleDoesNotExist(String module) {
080    requireNonNull(module, "module");
081    isNotNull();
082
083    var moduleGroup = actual.getModule(module);
084
085    if (moduleGroup == null) {
086      return this;
087    }
088
089    throw failure("Found unexpected module %s", module);
090  }
091}