@Before public void before() { settings = mock(Settings.class); when(settings.getBoolean(anyString())).thenReturn(null); when(settings.getString(anyString())).thenReturn(null); configValidator = new ConfigValidator(); }
@Test public void testThatRulesWithoutMatchDoesNotValidate() { when(settings.getString(SETTING_GROUP_ACCEPT + "[0]")) .thenReturn(SSCCGroup.Accept.SHOW_MESSAGE.toString()); when(settings.getString(SETTING_RULE_REGEXP + "[0][0]")).thenReturn("ok"); configValidator.validate(settings, errors, repository); assertEquals(SETTING_GROUP_MATCH + "[0]", on(",").join(fieldErrors.keySet())); assertEquals( "Cannot add a rule group without matching criteria!", on(",").join(fieldErrors.values())); }
@Test public void testThatRulesWithoutRegexpDoesNotValidate() { when(settings.getString(SETTING_GROUP_ACCEPT + "[0]")) .thenReturn(SSCCGroup.Accept.SHOW_MESSAGE.toString()); when(settings.getString(SETTING_GROUP_MATCH + "[0]")) .thenReturn(SSCCGroup.Match.ALL.toString()); when(settings.getString(SETTING_RULE_MESSAGE + "[0][0]")).thenReturn("A Message"); configValidator.validate(settings, errors, repository); assertEquals("ruleRegexp[0][0]", on(",").join(fieldErrors.keySet())); assertEquals("Cannot add a rule without regexp!", on(",").join(fieldErrors.values())); }
@Test public void testThatRulesWithAcceptMatchAndRegexpDoesValidate() throws ValidationException { when(settings.getString(SETTING_GROUP_MATCH + "[0]")) .thenReturn(SSCCGroup.Match.ALL.toString().toLowerCase()); when(settings.getString(SETTING_GROUP_ACCEPT + "[0]")) .thenReturn(SSCCGroup.Accept.SHOW_MESSAGE.toString().toLowerCase()); when(settings.getString(SETTING_RULE_REGEXP + "[0][0]")).thenReturn("ok"); configValidator.validate(settings, errors, repository); assertEquals("", on(",").join(fieldErrors.keySet())); assertEquals("", on(",").join(fieldErrors.values())); assertEquals("ok", sscSettings(settings).getGroups().get(0).getRules().get(0).getRegexp()); }
@Test public void testThatRuleMustHaveAValidRegexp() { when(settings.getString(SETTING_GROUP_ACCEPT + "[0]")) .thenReturn(SSCCGroup.Accept.SHOW_MESSAGE.toString()); when(settings.getString(SETTING_GROUP_MATCH + "[0]")) .thenReturn(SSCCGroup.Match.ALL.toString()); when(settings.getString(SETTING_RULE_REGEXP + "[0][0]")).thenReturn("[notok"); configValidator.validate(settings, errors, repository); assertEquals("ruleRegexp[0][0]", on(",").join(fieldErrors.keySet())); assertEquals( "Invalid Regexp: Unclosed character class near index 5 [notok ^", on(",").join(fieldErrors.values())); }
@Override public void validate( @Nonnull Settings settings, @Nonnull SettingsValidationErrors errors, @Nonnull Repository repository) { final String jenkinsUrl = settings.getString(Notifier.JENKINS_BASE); if (Strings.isNullOrEmpty(jenkinsUrl)) { errors.addFieldError(Notifier.JENKINS_BASE, "The url for your Jenkins instance is required."); } final String cloneType = settings.getString(Notifier.CLONE_URL); if (Strings.isNullOrEmpty(cloneType)) { errors.addFieldError(Notifier.CLONE_URL, "The repository clone url is required"); } }
@Test public void testThatBranchesCanBeEmpty() { when(settings.getString(SETTING_BRANCHES)).thenReturn(""); configValidator.validate(settings, errors, repository); assertEquals("", on(",").join(fieldErrors.keySet())); assertEquals("", on(",").join(fieldErrors.values())); }
@Override public void validate(Settings settings, SettingsValidationErrors errors, Repository repository) { try { Pattern.compile(settings.getString(SETTINGS_PATTERN, "")); } catch (PatternSyntaxException e) { errors.addFieldError(SETTINGS_PATTERN, "Pattern is not a valid regular expression"); } }
@Test public void testThatBranchesMustHaveAValidRegexp() { when(settings.getString(SETTING_BRANCHES)).thenReturn("[notok"); configValidator.validate(settings, errors, repository); assertEquals(SETTING_BRANCHES, on(",").join(fieldErrors.keySet())); assertEquals( "Invalid Regexp: Unclosed character class near index 5 [notok ^", on(",").join(fieldErrors.values())); }
protected List<MirrorSettings> getMirrorSettings(Settings settings) { List<MirrorSettings> results = new ArrayList<MirrorSettings>(); Map<String, Object> allSettings = settings.asMap(); int count = 0; for (String key : allSettings.keySet()) { if (key.startsWith(SETTING_MIRROR_REPO_URL)) { String suffix = key.substring(SETTING_MIRROR_REPO_URL.length()); MirrorSettings ms = new MirrorSettings(); ms.mirrorRepoUrl = settings.getString(SETTING_MIRROR_REPO_URL + suffix, ""); ms.username = settings.getString(SETTING_USERNAME + suffix, ""); ms.password = settings.getString(SETTING_PASSWORD + suffix, ""); ms.suffix = String.valueOf(count++); results.add(ms); } } return results; }
/** * Send notification to Jenkins for the provided repository. * * @param repo The repository to base the notification on. * @param strRef The branch ref related to the commit * @param strSha1 The commit's SHA1 hash code. * @return Text result from Jenkins */ public @Nullable NotificationResult notify( @Nonnull Repository repo, // CHECKSTYLE:annot String strRef, String strSha1) { final RepositoryHook hook = settingsService.getRepositoryHook(repo); final Settings settings = settingsService.getSettings(repo); if (hook == null || !hook.isEnabled() || settings == null) { LOGGER.debug("Hook not configured correctly or not enabled, returning."); return null; } return notify( repo, settings.getString(JENKINS_BASE), settings.getBoolean(IGNORE_CERTS, false), settings.getString(CLONE_TYPE), settings.getString(CLONE_URL), strRef, strSha1, settings.getBoolean(OMIT_HASH_CODE, false), settings.getBoolean(OMIT_BRANCH_NAME, false)); }