### Question:
GoRulesDefinition implements RulesDefinition { @Override public void define(Context context) { NewRepository repository = context.createRepository(REPOSITORY_KEY, GoLanguage.KEY) .setName("SonarAnalyzer"); RuleMetadataLoader metadataLoader = new RuleMetadataLoader(GoPlugin.RESOURCE_FOLDER); List<Class> checks = GoCheckList.checks(); metadataLoader.addRulesByAnnotatedClass(repository, checks); RulesDefinitionUtils.setDefaultValuesForParameters(repository, checks, Language.GO); repository.done(); if (externalIssuesSupported) { AbstractReportSensor.createExternalRuleRepository(context, GoVetReportSensor.LINTER_ID, GoVetReportSensor.LINTER_NAME); AbstractReportSensor.createExternalRuleRepository(context, GoLintReportSensor.LINTER_ID, GoLintReportSensor.LINTER_NAME); } } GoRulesDefinition(boolean externalIssuesSupported); @Override void define(Context context); static final String REPOSITORY_KEY; }### Answer:
@Test public void test() { GoRulesDefinition rulesDefinition = new GoRulesDefinition(false); RulesDefinition.Context context = new RulesDefinition.Context(); rulesDefinition.define(context); assertThat(context.repositories()).hasSize(1); RulesDefinition.Repository goRepository = context.repository("go"); assertThat(goRepository.name()).isEqualTo("SonarAnalyzer"); assertThat(goRepository.language()).isEqualTo("go"); assertThat(goRepository.rules()).hasSize(GoCheckList.checks().size()); RulesDefinition.Rule rule = goRepository.rule("S4663"); assertThat(rule).isNotNull(); assertThat(rule.name()).isEqualTo("Multi-line comments should not be empty"); assertThat(rule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE); assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL); }
@Test public void test_external_repositories() { GoRulesDefinition rulesDefinition = new GoRulesDefinition(true); RulesDefinition.Context context = new RulesDefinition.Context(); rulesDefinition.define(context); RulesDefinition.Repository golintRepository = context.repository("external_golint"); RulesDefinition.Repository govetRepository = context.repository("external_govet"); assertThat(context.repositories()).hasSize(3); assertThat(golintRepository.name()).isEqualTo("Golint"); assertThat(govetRepository.name()).isEqualTo("go vet"); assertThat(golintRepository.language()).isEqualTo("go"); assertThat(govetRepository.language()).isEqualTo("go"); assertThat(golintRepository.isExternal()).isEqualTo(true); assertThat(govetRepository.isExternal()).isEqualTo(true); assertThat(golintRepository.rules().size()).isEqualTo(18); assertThat(ExternalKeyUtils.GO_LINT_KEYS.size()).isEqualTo(18); assertThat(govetRepository.rules().size()).isEqualTo(21); assertThat(ExternalKeyUtils.GO_VET_KEYS.size()).isEqualTo(21); List<String> govetKeysWithoutDefinition = ExternalKeyUtils.GO_VET_KEYS.stream() .map(x -> x.key) .filter(key -> govetRepository.rule(key) == null) .collect(Collectors.toList()); assertThat(govetKeysWithoutDefinition).isEmpty(); List<String> golintKeysWithoutDefinition = ExternalKeyUtils.GO_LINT_KEYS.stream() .map(x -> x.key) .filter(key -> golintRepository.rule(key) == null) .collect(Collectors.toList()); assertThat(golintKeysWithoutDefinition).isEmpty(); }