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