@Test public void testUnsafeAOFileNonExistent() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(true); expect( mockI18nHelper.getText( EasyMock.eq("admin.errors.could.not.find.file"), EasyMock.<String>anyObject())) .andReturn("File does not exist."); final File file = File.createTempFile("testUnsafeAOFileNonExistent", "txt"); file.deleteOnExit(); try { final DataImportParams params = new DataImportParams.Builder(null) .setUnsafeJiraBackup(file) .setUnsafeAOBackup( new File("I.really.really.don't.exist.and.if.i.did.it.would.be.very.unlucky")) .build(); executeTest(params, false, DataImportService.ImportError.NONE); fail("Calling doImport with invalid validation result should have thrown an exception!"); } catch (IllegalStateException e) { // yay } finally { file.delete(); } }
@Override public WhitelistUpdateValidationResult validateUpdateRules( final JiraServiceContext context, final List<String> rules, final boolean disabled) { notNull("context", context); notNull("rules", rules); if (checkInvalidPermissions(context)) { return new WhitelistUpdateValidationResult(context.getErrorCollection()); } for (String rule : rules) { if (rule.startsWith(REGEX_PREFIX)) { try { Pattern.compile(rule.substring(REGEX_PREFIX.length())); } catch (PatternSyntaxException e) { final I18nHelper i18n = beanFactory.getInstance(context.getLoggedInUser()); context .getErrorCollection() .addErrorMessage(i18n.getText("whitelist.admin.errors.bad.pattern", e.getMessage())); } } } return new WhitelistUpdateValidationResult(context.getErrorCollection(), rules, disabled); }
boolean checkInvalidPermissions(final JiraServiceContext context) { if (!permissionManager.hasPermission(Permissions.SYSTEM_ADMIN, context.getLoggedInUser())) { final I18nHelper i18n = beanFactory.getInstance(context.getLoggedInUser()); context .getErrorCollection() .addErrorMessage(i18n.getText("whitelist.service.permission.error")); return true; } return false; }
@Test public void testExecuteBuildNumberTooOldInXml() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); expect(mockI18nHelper.getText("data.import.parse.xml")).andReturn("Parsing XML"); expect( mockI18nHelper.getText( EasyMock.eq("data.import.parse.progress"), EasyMock.<String>anyObject(), EasyMock.<String>anyObject())) .andReturn("Parsing progress"); // called during validation! expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(true); // This is called during the first parse of the XML file. At this stage nothing should have // been created yet! final MockGenericValue mockGv = new MockGenericValue("someentity"); expect(mockOfBizDelegator.makeValue(EasyMock.<String>anyObject())).andReturn(mockGv).anyTimes(); expect(mockAttachmentPathManager.getDefaultAttachmentPath()) .andReturn(directories.get(0).getAbsolutePath()) .anyTimes(); expect(mockIndexPathManager.getDefaultIndexRootPath()) .andReturn(directories.get(1).getAbsolutePath()) .anyTimes(); expect( mockLicenseStringFactory.create( EasyMock.<String>anyObject(), EasyMock.<String>anyObject())) .andStubReturn(""); // after the first parse check the build number. expect(mockBuildUtilsInfo.getCurrentBuildNumber()).andStubReturn("400"); expect(mockBuildUtilsInfo.getMinimumUpgradableBuildNumber()).andStubReturn("18"); // after the first parse we also verify the license is good. expect( mockJiraLicenseService.validate( EasyMock.<I18nHelper>anyObject(), EasyMock.<String>anyObject())) .andStubReturn(mockValidationResult); expect(mockValidationResult.getLicenseVersion()).andStubReturn(2); expect(mockValidationResult.getErrorCollection()).andStubReturn(new SimpleErrorCollection()); expect( mockExternalLinkUtil.getProperty( "external.link.jira.confluence.upgrade.guide.for.old.versions")) .andReturn("http://www.atlassian.com"); expect(mockI18nHelper.getText("data.import.error.xml.too.old", "http://www.atlassian.com")) .andReturn("Data is too old visit http://www.atlassian.com/"); final String filePath = getDataFilePath("jira-export-test-too-old.xml"); final DataImportParams params = new DataImportParams.Builder(filePath).build(); executeTest(params, false, DataImportService.ImportError.NONE); }
@Test public void testVersion1License() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); expect(mockI18nHelper.getText("data.import.parse.xml")).andReturn("Parsing XML"); expect( mockI18nHelper.getText( EasyMock.eq("data.import.parse.progress"), EasyMock.<String>anyObject(), EasyMock.<String>anyObject())) .andReturn("Parsing progress"); // called during validation! expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(true); expect(mockJiraLicenseService.validate(mockI18nHelper, "version1license")) .andReturn(mockValidationResult); expect(mockValidationResult.getLicenseVersion()).andReturn(1); expect(mockValidationResult.getErrorCollection()).andReturn(new SimpleErrorCollection()); // This is called during the first parse of the XML file. At this stage nothing should have // been created yet! final MockGenericValue mockGv = new MockGenericValue("someentity"); expect(mockOfBizDelegator.makeValue(EasyMock.<String>anyObject())).andReturn(mockGv).anyTimes(); expect(mockAttachmentPathManager.getDefaultAttachmentPath()) .andReturn(directories.get(0).getAbsolutePath()) .anyTimes(); expect(mockIndexPathManager.getDefaultIndexRootPath()) .andReturn(directories.get(1).getAbsolutePath()) .anyTimes(); expect( mockLicenseStringFactory.create( EasyMock.<String>anyObject(), EasyMock.<String>anyObject())) .andStubReturn(""); // after the first parse check the build number. expect(mockBuildUtilsInfo.getCurrentBuildNumber()).andStubReturn("99999999"); expect(mockBuildUtilsInfo.getMinimumUpgradableBuildNumber()).andStubReturn("0"); // after the first parse we also verify the license is good. expect( mockJiraLicenseService.validate( EasyMock.<I18nHelper>anyObject(), EasyMock.<String>anyObject())) .andStubReturn(mockValidationResult); expect(mockValidationResult.getLicenseVersion()).andStubReturn(2); expect(mockValidationResult.getErrorCollection()).andStubReturn(new SimpleErrorCollection()); final String filePath = getDataFilePath("jira-export-test.xml"); final DataImportParams params = new DataImportParams.Builder(filePath).setLicenseString("version1license").build(); executeTest(params, false, DataImportService.ImportError.V1_LICENSE_EXCEPTION); }
@Test public void testNoFileProvided() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(true); expect(mockI18nHelper.getText("admin.errors.must.enter.xml2")).andReturn("Must provide file"); try { final DataImportParams params = new DataImportParams.Builder("").build(); executeTest(params, false, DataImportService.ImportError.NONE); fail("Calling doImport with invalid validation result should have thrown an exception!"); } catch (IllegalStateException e) { // yay } }
@Test public void testSetupImportWhenAlreadySetup() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); mockProperties.setString(APKeys.JIRA_SETUP, "true"); expect(mockI18nHelper.getText("admin.errors.import.already.setup")) .andReturn("Already setup. Should do xml restore"); expect(mockI18nHelper.getText("admin.errors.must.enter.xml2")).andReturn("Must provide file"); try { final DataImportParams params = new DataImportParams.Builder("").setupImport().build(); executeTest(params, false, DataImportService.ImportError.NONE); fail("Calling doImport with invalid validation result should have thrown an exception!"); } catch (IllegalStateException e) { // yay } }
@Test public void testNoPermission() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(false); expect(mockI18nHelper.getText("admin.errors.import.permission")) .andReturn("No Permission to import data!"); try { final String filePath = getDataFilePath("jira-export-test.xml"); final DataImportParams params = new DataImportParams.Builder(filePath).build(); executeTest(params, false, DataImportService.ImportError.NONE); fail("Calling doImport with invalid validation result should have thrown an exception!"); } catch (IllegalStateException e) { // yay } }
@Test public void testNoAO() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(true); expect(mockJiraHome.getImportDirectory()).andReturn(new File("somewhere")).anyTimes(); expect(mockI18nHelper.getText("data.import.error.no.ao")).andReturn("Data Import."); backup = null; try { final String filePath = getDataFilePath("jira-export-test.xml"); final DataImportParams params = new DataImportParams.Builder(filePath).build(); executeTest(params, false, DataImportService.ImportError.NONE); fail("Calling doImport with invalid validation result should have thrown an exception!"); } catch (IllegalStateException e) { // yay } }
public void validateCommentProperties( Map<String, Object> properties, ErrorCollection errorCollection) { try { final ImmutableMap<String, JSONObject> commentProperties = getCommentPropertiesFromParameters(properties); for (Map.Entry<String, JSONObject> property : commentProperties.entrySet()) { EntityPropertyService.PropertyInput propertyInput = new EntityPropertyService.PropertyInput( property.getValue().toString(), property.getKey()); errorCollection.addErrorCollection( commentPropertyService.validatePropertyInput(propertyInput)); } } catch (JSONException e) { errorCollection.addErrorMessage( i18nFactory .getInstance(authenticationContext.getUser()) .getText( "jira.properties.service.invalid.json", properties.get(PARAM_COMMENT_PROPERTY))); } }
@Test public void testFileNonExistent() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(true); expect(mockJiraHome.getImportDirectory()).andReturn(new File("somewhere")).anyTimes(); expect( mockI18nHelper.getText( EasyMock.eq("admin.errors.could.not.find.file"), EasyMock.<String>anyObject())) .andReturn("File does not exist."); try { final DataImportParams params = new DataImportParams.Builder("idontexisthopefully.txt").build(); executeTest(params, false, DataImportService.ImportError.NONE); fail("Calling doImport with invalid validation result should have thrown an exception!"); } catch (IllegalStateException e) { // yay } }
@Test public void testInvalidLicenseProvided() throws Exception { expect(mockBeanFactory.getInstance(currentUser)).andReturn(mockI18nHelper).anyTimes(); expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(true); expect(mockJiraLicenseService.validate(mockI18nHelper, "thisisnotavalidlicensestring")) .andReturn(mockValidationResult); final ErrorCollection errors = new SimpleErrorCollection(); errors.addErrorMessage("Not a valid license"); expect(mockValidationResult.getErrorCollection()).andReturn(errors); try { final String filePath = getDataFilePath("jira-export-test.xml"); final DataImportParams params = new DataImportParams.Builder(filePath) .setLicenseString("thisisnotavalidlicensestring") .build(); executeTest(params, false, DataImportService.ImportError.NONE); fail("Calling doImport with invalid validation result should have thrown an exception!"); } catch (IllegalStateException e) { // yay } }
@Test public void testExecuteBuildNumberMissing() throws Exception { expect(mockBarrier.await(20, TimeUnit.SECONDS)).andReturn(true); expect(mockBeanFactory.getInstance(currentUser)).andReturn(new MockI18nHelper()).anyTimes(); // called during validation! expect(mockPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, currentUser)) .andReturn(true); // This is called during the first parse of the XML file. At this stage nothing should have // been created yet! final MockGenericValue mockGv = new MockGenericValue("someentity"); expect(mockOfBizDelegator.makeValue(EasyMock.<String>anyObject())).andReturn(mockGv).anyTimes(); expect(mockAttachmentPathManager.getDefaultAttachmentPath()) .andReturn(directories.get(0).getAbsolutePath()) .anyTimes(); expect(mockIndexPathManager.getDefaultIndexRootPath()) .andReturn(directories.get(1).getAbsolutePath()) .anyTimes(); expect( mockLicenseStringFactory.create( EasyMock.<String>anyObject(), EasyMock.<String>anyObject())) .andStubReturn(""); // after the first parse check the build number. expect(mockBuildUtilsInfo.getCurrentBuildNumber()).andStubReturn("1"); expect(mockBuildUtilsInfo.getMinimumUpgradableBuildNumber()).andStubReturn("0"); // after the first parse we also verify the license is good. expect( mockJiraLicenseService.validate( EasyMock.<I18nHelper>anyObject(), EasyMock.<String>anyObject())) .andStubReturn(mockValidationResult); expect(mockValidationResult.getLicenseVersion()).andStubReturn(2); expect(mockValidationResult.getErrorCollection()).andStubReturn(new SimpleErrorCollection()); // this gets called during shutdownAndFlushAsyncServices. After parse and before the import. // This shuts down // the scheduler expect(mockScheduler.isShutdown()).andReturn(false); mockScheduler.shutdown(); mockMailQueue.sendBuffer(); expect(mockTaskManager.shutdownAndWait(5)).andReturn(true); // Expect AO to be cleared. backup.clear(); // Once the import is running one of the first things to do is to clear out the old database // values. expect(mockOfBizDelegator.getModelReader()).andReturn(mockModelReader); expect(mockModelReader.getEntityNames()) .andReturn(CollectionBuilder.<String>list("Issue", "User")); expect(mockModelReader.getModelEntity("Issue")).andReturn(new ModelEntity()); expect(mockOfBizDelegator.removeByAnd("Issue", Collections.<String, Object>emptyMap())) .andReturn(10); expect(mockModelReader.getModelEntity("User")).andReturn(new ModelEntity()); expect(mockOfBizDelegator.removeByAnd("User", Collections.<String, Object>emptyMap())) .andReturn(5); // then we go through and create all our GVs (already mocked out during the first parse above) // once everything's been imported need to refresh the ofbiz sequencer and check for data // consistency. mockOfBizDelegator.refreshSequencer(); mockConsistencyChecker.checkDataConsistency(); // after the consistency check lets do the upgrade expect(mockUpgradeManager.doUpgradeIfNeededAndAllowed(null)) .andReturn(Collections.<String>emptyList()); // now do a reindex mockIndexManager.deactivate(); expect(mockIndexManager.size()).andReturn(5); expect(mockIndexManager.activate((Context) notNull())).andReturn(1L); // raise the JiraStartedEvent mockPluginEventManager.broadcast(EasyMock.<JiraStartedEvent>anyObject()); // finally we can restart the scheduler! expect( mockScheduler.scheduleJob( EasyMock.<JobDetail>anyObject(), EasyMock.<Trigger>anyObject())) .andReturn(new Date()) .anyTimes(); mockScheduler.start(); final String filePath = getDataFilePath("jira-export-test-no-build-number.xml"); final DataImportParams params = new DataImportParams.Builder(filePath).build(); // Finally everything's mocked out. Run the import! executeTest(params, true, DataImportService.ImportError.NONE); // create() should have been called on our GVs assertTrue(mockGv.isCreated()); // the world should have been rebuilt! assertTrue(((MockDataImportDependencies) mockDependencies).globalRefreshCalled); }