-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ThirdPartySyncCommand - Adding new parameters #613
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package com.box.l10n.mojito.cli.command; | ||
|
||
import com.box.l10n.mojito.rest.client.ThirdPartySync; | ||
import com.box.l10n.mojito.rest.ThirdPartySyncAction; | ||
|
||
/** | ||
* | ||
* @author sdemyanenko | ||
*/ | ||
public class ThirdPartySyncActionsConverter extends EnumConverter<ThirdPartySync.Action> { | ||
public class ThirdPartySyncActionsConverter extends EnumConverter<ThirdPartySyncAction> { | ||
|
||
@Override | ||
protected Class<ThirdPartySync.Action> getGenericClass() { | ||
return ThirdPartySync.Action.class; | ||
protected Class<ThirdPartySyncAction> getGenericClass() { | ||
return ThirdPartySyncAction.class; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,87 @@ | ||
package com.box.l10n.mojito.cli.command; | ||
|
||
import com.box.l10n.mojito.cli.CLITestBase; | ||
import com.box.l10n.mojito.cli.utils.PollableTaskJobMatcher; | ||
import com.box.l10n.mojito.cli.utils.TestingJobListener; | ||
import com.box.l10n.mojito.entity.Repository; | ||
import com.box.l10n.mojito.rest.client.ThirdPartySync; | ||
import com.box.l10n.mojito.json.ObjectMapper; | ||
import com.box.l10n.mojito.rest.ThirdPartySyncAction; | ||
import com.box.l10n.mojito.service.thirdparty.ThirdPartySyncJob; | ||
import com.box.l10n.mojito.service.thirdparty.ThirdPartySyncJobInput; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.quartz.JobKey; | ||
import org.quartz.Matcher; | ||
import org.quartz.Scheduler; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ThirdPartySyncCommandTest extends CLITestBase { | ||
|
||
/** | ||
* logger | ||
*/ | ||
static Logger logger = LoggerFactory.getLogger(ThirdPartySyncCommandTest.class); | ||
@Autowired | ||
@Qualifier("fail_on_unknown_properties_false") | ||
ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
Scheduler scheduler; | ||
|
||
Matcher<JobKey> jobMatcher; | ||
TestingJobListener testingJobListener; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
testingJobListener = new TestingJobListener(objectMapper); | ||
jobMatcher = new PollableTaskJobMatcher<>(ThirdPartySyncJob.class); | ||
scheduler.getListenerManager().addJobListener(testingJobListener, jobMatcher); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will also impact other test, i'm always reticent with changing the application context. Can we validate in some other ways? |
||
} | ||
|
||
@Test | ||
public void execute() throws Exception { | ||
|
||
String repoName = testIdWatcher.getEntityName("thirdpartysync_execute"); | ||
|
||
Repository repository = repositoryService.createRepository(repoName, repoName + " description", null, false); | ||
getL10nJCommander().run("thirdparty-sync", "-r", repository.getName(), "-p", "does-not-matter-yet", "-ps", " _"); | ||
String projectId = testIdWatcher.getEntityName("projectId"); | ||
|
||
// TODO: For a plural separator like " _" this test will fail. The current version we have for | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see now why you created the issue |
||
// JCommander trims the argument values, even when quoted. | ||
// https://github.com/cbeust/jcommander/issues/417 | ||
// https://github.com/cbeust/jcommander/commit/4aec38b4a0ea63a8dc6f41636fa81c2ebafddc18 | ||
String pluralSeparator = "_"; | ||
String skipTextUnitPattern = "%skip_text_pattern"; | ||
String skipAssetPattern = "%skip_asset_pattern%"; | ||
List<String> options = Arrays.asList( | ||
"special-option=value@of%Option", | ||
"smartling-placeholder-custom=\\{\\{\\}\\}|\\{\\{?.+?\\}\\}?|\\%\\%\\(.+?\\)s|\\%\\(.+?\\)s|\\%\\(.+?\\)d|\\%\\%s|\\%s" | ||
); | ||
|
||
getL10nJCommander().run("thirdparty-sync", | ||
"-r", repository.getName(), | ||
"-p", projectId, | ||
"-a", "MAP_TEXTUNIT", "PUSH_SCREENSHOT", | ||
"-ps", pluralSeparator, | ||
"-st", skipTextUnitPattern, | ||
"-sa", skipAssetPattern, | ||
"-o", options.get(0), options.get(1)); | ||
|
||
waitForCondition("Ensure ThirdPartySyncJob gets executed", | ||
() -> testingJobListener.getExecuted().size() > 0); | ||
|
||
ThirdPartySyncJobInput jobInput = testingJobListener.getFirstInputMapAs(ThirdPartySyncJobInput.class); | ||
|
||
String outputString = outputCapture.toString(); | ||
assertTrue(outputString.contains(Arrays.asList(ThirdPartySync.Action.MAP_TEXTUNIT, ThirdPartySync.Action.PUSH_SCREENSHOT).toString())); | ||
assertThat(jobInput).isNotNull(); | ||
assertThat(jobInput.getRepositoryId()).isEqualTo(repository.getId()); | ||
assertThat(jobInput.getThirdPartyProjectId()).isEqualTo(projectId); | ||
assertThat(jobInput.getActions()).containsExactlyInAnyOrder(ThirdPartySyncAction.MAP_TEXTUNIT, ThirdPartySyncAction.PUSH_SCREENSHOT); | ||
assertThat(jobInput.getPluralSeparator()).isEqualTo(pluralSeparator); | ||
assertThat(jobInput.getSkipTextUnitsWithPattern()).isEqualTo(skipTextUnitPattern); | ||
assertThat(jobInput.getSkipAssetsWithPathPattern()).isEqualTo(skipAssetPattern); | ||
assertThat(jobInput.getOptions()).containsExactlyInAnyOrderElementsOf(options); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.box.l10n.mojito.cli.utils; | ||
|
||
import com.box.l10n.mojito.quartz.QuartzPollableJob; | ||
import org.quartz.JobKey; | ||
import org.quartz.Matcher; | ||
|
||
import static com.box.l10n.mojito.quartz.QuartzConfig.DYNAMIC_GROUP_NAME; | ||
|
||
public class PollableTaskJobMatcher<T extends QuartzPollableJob> implements Matcher<JobKey> { | ||
|
||
private final Class<T> target; | ||
|
||
public PollableTaskJobMatcher(Class<T> target) { | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public boolean isMatch(JobKey key) { | ||
return key.getName().startsWith(target.getName()) && DYNAMIC_GROUP_NAME.equals(key.getGroup()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.box.l10n.mojito.cli.utils; | ||
|
||
import com.box.l10n.mojito.json.ObjectMapper; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
import org.quartz.JobListener; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
import static com.box.l10n.mojito.quartz.QuartzPollableJob.INPUT; | ||
|
||
public class TestingJobListener implements JobListener { | ||
|
||
private final Queue<JobExecutionContext> toBeExecuted = new LinkedList<>(); | ||
private final Queue<JobExecutionContext> executed = new LinkedList<>(); | ||
private final Queue<JobExecutionException> exceptions = new LinkedList<>(); | ||
private final ObjectMapper objectMapper; | ||
|
||
public TestingJobListener(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
public TestingJobListener() { | ||
this.objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "TestingJobListener"; | ||
} | ||
|
||
@Override | ||
public void jobToBeExecuted(JobExecutionContext context) { | ||
toBeExecuted.offer(context); | ||
} | ||
|
||
@Override | ||
public void jobExecutionVetoed(JobExecutionContext context) { | ||
} | ||
|
||
@Override | ||
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) { | ||
executed.offer(context); | ||
exceptions.offer(jobException); | ||
} | ||
|
||
public Queue<JobExecutionContext> getExecuted() { | ||
return executed; | ||
} | ||
|
||
public Queue<JobExecutionContext> getToBeExecuted() { | ||
return toBeExecuted; | ||
} | ||
|
||
public <T> T getFirstInputMapAs(Class<T> klass) { | ||
String input = getExecuted().stream() | ||
.filter(context -> context.getMergedJobDataMap().containsKey(INPUT)) | ||
.map(context -> context.getMergedJobDataMap().getString(INPUT)) | ||
.findFirst() | ||
.orElse(""); | ||
|
||
return objectMapper.readValueUnchecked(input, klass); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.box.l10n.mojito.rest; | ||
|
||
public enum ThirdPartySyncAction { | ||
PUSH, | ||
PUSH_TRANSLATION, | ||
PULL, | ||
MAP_TEXTUNIT, | ||
PUSH_SCREENSHOT | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!