FactoryBaseValidator { protected void validateProjectActions(Factory factory) throws ConflictException { if (!"2.1".equals(factory.getV()) || factory.getIde() == null) { return; } List<Action> applicationActions = new ArrayList<>(); if (factory.getIde().getOnAppClosed() != null) { applicationActions.addAll(factory.getIde().getOnAppClosed().getActions()); } if (factory.getIde().getOnAppLoaded() != null) { applicationActions.addAll(factory.getIde().getOnAppLoaded().getActions()); } for (Action applicationAction : applicationActions) { String id = applicationAction.getId(); if ("openFile".equals(id) || "findReplace".equals(id)) { throw new ConflictException(String.format(FactoryConstants.INVALID_ACTION_SECTION, id)); } } final OnAppLoaded onAppLoaded = factory.getIde().getOnAppLoaded(); if (onAppLoaded != null) { final List<Action> actions = onAppLoaded.getActions(); if (actions != null) { for (Action action : actions) { final Map<String, String> properties = action.getProperties(); if ("openWelcomePage".equals(action.getId()) && (isNullOrEmpty(properties.get("nonAuthenticatedContentUrl")) || isNullOrEmpty(properties.get("authenticatedContentUrl")))) { throw new ConflictException(FactoryConstants.INVALID_WELCOME_PAGE_ACTION); } } } } OnProjectOpened onOpened = factory.getIde().getOnProjectOpened(); if (onOpened != null) { List<Action> onProjectOpenedActions = onOpened.getActions(); for (Action applicationAction : onProjectOpenedActions) { String id = applicationAction.getId(); Map<String, String> properties = applicationAction.getProperties(); switch (id) { case "openFile": if (isNullOrEmpty(properties.get("file"))) { throw new ConflictException(FactoryConstants.INVALID_OPENFILE_ACTION); } break; case "findReplace": if (isNullOrEmpty(properties.get("in")) || isNullOrEmpty(properties.get("find")) || isNullOrEmpty(properties.get("replace"))) { throw new ConflictException(FactoryConstants.INVALID_FIND_REPLACE_ACTION); } break; } } } } FactoryBaseValidator(AccountDao accountDao,
                                UserDao userDao,
                                PreferenceDao preferenceDao);   }
@Test(expectedExceptions = ConflictException.class) public void shouldNotValidateOpenfileActionIfInWrongSectionOnAppClosed() throws Exception { validator = new TestFactoryBaseValidator(accountDao, userDao, preferenceDao); List<Action> actions = Arrays.asList(dto.createDto(Action.class).withId("openFile")); Ide ide = dto.createDto(Ide.class).withOnAppClosed(dto.createDto(OnAppClosed.class).withActions(actions)); Factory factoryWithAccountId = dto.clone(factory).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = ConflictException.class) public void shouldNotValidateFindReplaceActionIfInWrongSectionOnAppLoaded() throws Exception { validator = new TestFactoryBaseValidator(accountDao, userDao, preferenceDao); List<Action> actions = Arrays.asList(dto.createDto(Action.class).withId("findReplace")); Ide ide = dto.createDto(Ide.class).withOnAppLoaded(dto.createDto(OnAppLoaded.class).withActions(actions)); Factory factoryWithAccountId = dto.clone(factory).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = ConflictException.class) public void shouldNotValidateIfOpenfileActionInsufficientParams() throws Exception { validator = new TestFactoryBaseValidator(accountDao, userDao, preferenceDao); List<Action> actions = Arrays.asList(dto.createDto(Action.class).withId("openFile")); Ide ide = dto.createDto(Ide.class).withOnProjectOpened(dto.createDto(OnProjectOpened.class).withActions(actions)); Factory factoryWithAccountId = dto.clone(factory).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = ConflictException.class) public void shouldNotValidateIfOpenWelcomePageActionInsufficientParams() throws Exception { validator = new TestFactoryBaseValidator(accountDao, userDao, preferenceDao); List<Action> actions = Arrays.asList(dto.createDto(Action.class).withId("openWelcomePage")); Ide ide = dto.createDto(Ide.class).withOnAppLoaded((dto.createDto(OnAppLoaded.class).withActions(actions))); Factory factoryWithAccountId = dto.clone(factory).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test(expectedExceptions = ConflictException.class) public void shouldNotValidateIfFindReplaceActionInsufficientParams() throws Exception { validator = new TestFactoryBaseValidator(accountDao, userDao, preferenceDao); Map<String, String> params = new HashMap<>(); params.put("in", "pom.xml"); params.put("replace", "123"); List<Action> actions = Arrays.asList(dto.createDto(Action.class).withId("findReplace").withProperties(params)); Ide ide = dto.createDto(Ide.class).withOnProjectOpened( dto.createDto(OnProjectOpened.class).withActions(actions)); Factory factoryWithAccountId = dto.clone(factory).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test public void shouldValidateFindReplaceAction() throws Exception { validator = new TestFactoryBaseValidator(accountDao, userDao, preferenceDao); Map<String, String> params = new HashMap<>(); params.put("in", "pom.xml"); params.put("find", "123"); params.put("replace", "456"); List<Action> actions = Arrays.asList(dto.createDto(Action.class).withId("findReplace").withProperties(params)); Ide ide = dto.createDto(Ide.class).withOnProjectOpened( dto.createDto(OnProjectOpened.class).withActions(actions)); Factory factoryWithAccountId = dto.clone(factory).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }
@Test public void shouldValidateOpenfileAction() throws Exception { validator = new TestFactoryBaseValidator(accountDao, userDao, preferenceDao); Map<String, String> params = new HashMap<>(); params.put("file", "pom.xml"); List<Action> actions = Arrays.asList(dto.createDto(Action.class).withId("openFile").withProperties(params)); Ide ide = dto.createDto(Ide.class).withOnProjectOpened( dto.createDto(OnProjectOpened.class).withActions(actions)); Factory factoryWithAccountId = dto.clone(factory).withIde(ide); validator.validateProjectActions(factoryWithAccountId); }