<commit_before>/*(c) Copyright 2008, VersionOne, Inc. All rights reserved. (c)*/
package com.versionone.integration.teamcity.tests;

import com.versionone.integration.ciCommon.V1Config;
import com.versionone.integration.teamcity.SettingsBean;
import com.versionone.integration.teamcity.V1ServerListener;
import com.versionone.integration.teamcity.V1SettingsController;
import jetbrains.buildServer.serverSide.crypt.RSACipher;
import jetbrains.buildServer.web.openapi.PagePlaces;
import jetbrains.buildServer.web.openapi.WebControllerManager;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Assert;
import org.junit.Test;

public class V1SettingsControllerTester {
    private Mockery mockery = new Mockery();

    {
        mockery.setImposteriser(ClassImposteriser.INSTANCE);
    }

    @Test
    public void testValidate() {
        final V1ServerListener v1Listener = mockery.mock(V1ServerListener.class);
        mockery.checking(new Expectations() {
            {
                allowing(v1Listener).getConfig();
                will(returnValue(null));
            }
        });

        final V1SettingsController v1Controller = new V1SettingsController(null, v1Listener, null, null, null) {

            @Override
            protected void registerController(WebControllerManager webControllerManager, PagePlaces places) {
                //do nothing
            }
        };

        final V1Config config = new V1Config();
        config.setDefaults();
        SettingsBean bean = new SettingsBean(config);
        Assert.assertFalse(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setUrl("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setUrl("ggg://dfg");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setUserName(null);
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setUserName("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setEncryptedPassword(RSACipher.encryptDataForWeb(null));
        Assert.assertFalse(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setEncryptedPassword(RSACipher.encryptDataForWeb(""));
        Assert.assertFalse(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setEncryptedPassword(RSACipher.encryptDataForWeb(""));
        bean.setUserName("");
        Assert.assertFalse(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setReferenceField(null);
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setReferenceField("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setPattern(null);
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setPattern("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setPattern("\\");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setPattern("");
        bean.setReferenceField("");
        Assert.assertFalse(v1Controller.validate(bean).hasErrors());
    }

    @Test
    public void testPasswordEncription() {
        final V1Config config = new V1Config();
        config.setDefaults();
        SettingsBean bean = new SettingsBean(config);

        Assert.assertFalse(bean.getEncryptedPassword().contains(bean.getPassword()));
    }

    @Test
    public void testValidateConnection() {
        final V1ServerListener v1Listener = mockery.mock(V1ServerListener.class);
        mockery.checking(new Expectations() {
            {
                allowing(v1Listener).getConfig();
                will(returnValue(null));<commit_msg>fix test<commit_after>/*(c) Copyright 2008, VersionOne, Inc. All rights reserved. (c)*/
package com.versionone.integration.teamcity.tests;

import com.versionone.integration.ciCommon.V1Config;
import com.versionone.integration.teamcity.SettingsBean;
import com.versionone.integration.teamcity.V1ServerListener;
import com.versionone.integration.teamcity.V1SettingsController;
import jetbrains.buildServer.serverSide.crypt.RSACipher;
import jetbrains.buildServer.web.openapi.PagePlaces;
import jetbrains.buildServer.web.openapi.WebControllerManager;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Assert;
import org.junit.Test;

public class V1SettingsControllerTester {
    private Mockery mockery = new Mockery();

    {
        mockery.setImposteriser(ClassImposteriser.INSTANCE);
    }

    @Test
    public void testValidate() {
        final V1ServerListener v1Listener = mockery.mock(V1ServerListener.class);
        mockery.checking(new Expectations() {
            {
                allowing(v1Listener).getConfig();
                will(returnValue(null));
            }
        });

        final V1SettingsController v1Controller = new V1SettingsController(null, v1Listener, null, null, null) {

            @Override
            protected void registerController(WebControllerManager webControllerManager, PagePlaces places) {
                //do nothing
            }
        };

        final V1Config config = new V1Config();
        config.setDefaults();
        SettingsBean bean = new SettingsBean(config);
        Assert.assertFalse(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setUrl("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setUrl("ggg://dfg");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setUserName(null);
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setUserName("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setEncryptedPassword(RSACipher.encryptDataForWeb(null));
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setEncryptedPassword(RSACipher.encryptDataForWeb(""));
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setEncryptedPassword(RSACipher.encryptDataForWeb(""));
        bean.setUserName("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setReferenceField(null);
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setReferenceField("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setPattern(null);
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setPattern("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setPattern("\\");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());

        bean = new SettingsBean(config);
        bean.setPattern("");
        bean.setReferenceField("");
        Assert.assertTrue(v1Controller.validate(bean).hasErrors());
    }

    @Test
    public void testPasswordEncription() {
        final V1Config config = new V1Config();
        config.setDefaults();
        SettingsBean bean = new SettingsBean(config);

        Assert.assertFalse(bean.getEncryptedPassword().contains(bean.getPassword()));
    }

    @Test
    public void testValidateConnection() {
        final V1ServerListener v1Listener = mockery.mock(V1ServerListener.class);
        mockery.checking(new Expectations() {
            {
                allowing(v1Listener).getConfig();
                will(returnValue(null));
{'fraction_non_alphanumeric': 0.11519061583577712, 'fraction_numerical': 0.008914956011730205, 'mean_word_length': 3.1630859375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5662331', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2271, 'n_words': 358}