/*
 * Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved.
 * 项目名称：Java 信息安全性设计准则
 * 项目描述：用于检查Java源代码的安全性设计准则的Sonarqube插件
 * 版权说明：本软件属北京关键科技股份有限公司所有，在未获得北京关键科技股份有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.keyware.sonar.java.rules;

import org.junit.jupiter.api.Test;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.server.rule.RulesDefinition;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * TODO JavaSecurityDesignRulesRepositoryTest
 *
 * @author GuoXin
 * @date 2024/1/6
 */
public class JavaSecurityDesignRulesRepositoryTest {

    @Test
    void test() {
        JavaSecurityDesignRulesRepository rulesDefinition = new JavaSecurityDesignRulesRepository(new JavaSecurityDesignRulesRepository.MockedSonarRuntime());
        RulesDefinition.Context context = new RulesDefinition.Context();
        rulesDefinition.define(context);
        RulesDefinition.Repository repository = context.repository(JavaSecurityDesignRulesRepository.REPOSITORY_KEY);

        assertThat(repository.name()).isEqualTo(JavaSecurityDesignRulesRepository.REPOSITORY_NAME);
        assertThat(repository.language()).isEqualTo("java");
        assertThat(repository.rules()).hasSize(RulesList.getChecks().size());
        assertThat(repository.rules().stream().filter(RulesDefinition.Rule::template)).isEmpty();

        //assertRuleProperties(repository);
        // assertParameterProperties(repository);
        // assertAllRuleParametersHaveDescription(repository);
    }

    private static void assertParameterProperties(RulesDefinition.Repository repository) {
        RulesDefinition.Param max = repository.rule("AvoidAnnotation").param("name");
        assertThat(max).isNotNull();
        assertThat(max.defaultValue()).isEqualTo("Inject");
        assertThat(max.description()).isEqualTo("Name of the annotation to avoid, without the prefix @, for instance 'Override'");
        assertThat(max.type()).isEqualTo(RuleParamType.STRING);
    }

    private static void assertRuleProperties(RulesDefinition.Repository repository) {
        RulesDefinition.Rule rule = repository.rule("AvoidAnnotation");
        assertThat(rule).isNotNull();
        assertThat(rule.name()).isEqualTo("Title of AvoidAnnotation");
        assertThat(rule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
        assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL);
    }

    private static void assertAllRuleParametersHaveDescription(RulesDefinition.Repository repository) {
        for (RulesDefinition.Rule rule : repository.rules()) {
            for (RulesDefinition.Param param : rule.params()) {
                assertThat(param.description()).as("description for " + param.key()).isNotEmpty();
            }
        }
    }
}
