Skip to content

Commit

Permalink
I18N-1323 Update Mojito CLI to use OpenAPI spec for rest calls
Browse files Browse the repository at this point in the history
Fixed schemas' issues
  • Loading branch information
DarKhaos committed Dec 9, 2024
1 parent ca48c7c commit 5282d54
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.box.l10n.mojito.cli.apiclient;

import com.box.l10n.mojito.cli.command.CommandException;
import com.box.l10n.mojito.cli.model.AssetAssetSummary;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
public class AssetWsApiHelper {
@Autowired AssetWsApi assetClient;

public AssetAssetSummary getAssetByPathAndRepositoryId(String path, Long repositoryId)
throws CommandException {
Assert.notNull(path, "path must not be null");
Assert.notNull(repositoryId, "repository must not be null");

List<AssetAssetSummary> assets;
try {
assets = this.assetClient.getAssets(repositoryId, path, null, null, null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
if (!assets.isEmpty()) {
return assets.getFirst();
} else {
throw new CommandException(
"Could not find asset with path = [" + path + "] at repo id [" + repositoryId + "]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.apiclient.RepositoryWsApiHelper;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.rest.client.exception.RepositoryNotFoundException;
import com.box.l10n.mojito.rest.entity.IntegrityChecker;
import com.box.l10n.mojito.rest.entity.Repository;
import com.box.l10n.mojito.rest.entity.RepositoryLocale;
import com.box.l10n.mojito.cli.model.AssetIntegrityCheckerRepository;
import com.box.l10n.mojito.cli.model.RepositoryLocaleRepository;
import com.box.l10n.mojito.cli.model.RepositoryRepository;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.fusesource.jansi.Ansi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

Expand All @@ -38,35 +39,40 @@ public class RepoViewCommand extends RepoCommand {
description = Param.REPOSITORY_NAME_DESCRIPTION)
String nameParam;

@Autowired RepositoryWsApiHelper repositoryWsApiHelper;

@Override
public void execute() throws CommandException {
consoleWriter.a("View repository: ").fg(Ansi.Color.CYAN).a(nameParam).println();

try {
Repository repository = repositoryClient.getRepositoryByName(nameParam);
consoleWriter
.newLine()
.a("Repository id --> ")
.fg(Ansi.Color.MAGENTA)
.a(repository.getId())
.println();
printIntegrityChecker(repository);
printLocales(repository);
consoleWriter.println();
} catch (RepositoryNotFoundException ex) {
throw new CommandException(ex.getMessage(), ex);
}
RepositoryRepository repository = this.repositoryWsApiHelper.findRepositoryByName(nameParam);
consoleWriter
.newLine()
.a("Repository id --> ")
.fg(Ansi.Color.MAGENTA)
.a(repository.getId())
.println();
printIntegrityChecker(repository);
printLocales(repository);
consoleWriter.println();
}

private void printIntegrityChecker(Repository repository) {
if (repository.getIntegrityCheckers() != null && !repository.getIntegrityCheckers().isEmpty()) {
List<IntegrityChecker> integrityCheckers = new ArrayList<>();
integrityCheckers.addAll(repository.getIntegrityCheckers());
Collections.sort(integrityCheckers, IntegrityChecker.getComparator());
private void printIntegrityChecker(RepositoryRepository repository) {
if (repository.getAssetIntegrityCheckers() != null
&& !repository.getAssetIntegrityCheckers().isEmpty()) {
List<AssetIntegrityCheckerRepository> integrityCheckers = new ArrayList<>();
integrityCheckers.addAll(repository.getAssetIntegrityCheckers());
Collections.sort(
integrityCheckers,
(integrityChecker1, integrityChecker2) -> {
String extension1 = integrityChecker1.getAssetExtension();
String extension2 = integrityChecker2.getAssetExtension();
return extension1.compareTo(extension2);
});

consoleWriter.newLine().a("Integrity checkers --> ").fg(Ansi.Color.MAGENTA);
for (int i = 0; i < integrityCheckers.size(); i++) {
IntegrityChecker integrityChecker = integrityCheckers.get(i);
AssetIntegrityCheckerRepository integrityChecker = integrityCheckers.get(i);
consoleWriter
.a(integrityChecker.getAssetExtension())
.a(":")
Expand All @@ -80,20 +86,26 @@ private void printIntegrityChecker(Repository repository) {
}
}

private void printLocales(Repository repository) {
private void printLocales(RepositoryRepository repository) {
if (repository.getRepositoryLocales() != null && !repository.getRepositoryLocales().isEmpty()) {
List<RepositoryLocale> repositoryLocales = new ArrayList<>();
List<RepositoryLocaleRepository> repositoryLocales = new ArrayList<>();
repositoryLocales.addAll(repository.getRepositoryLocales());
Collections.sort(repositoryLocales, RepositoryLocale.getComparator());
Collections.sort(
repositoryLocales,
(repositoryLocale1, repositoryLocale2) -> {
String bcp47Tag1 = repositoryLocale1.getLocale().getBcp47Tag();
String bcp47Tag2 = repositoryLocale2.getLocale().getBcp47Tag();
return bcp47Tag1.compareTo(bcp47Tag2);
});

consoleWriter.newLine().a("Repository locales --> ").fg(Ansi.Color.MAGENTA);
for (int j = 0; j < repositoryLocales.size(); j++) {
RepositoryLocale repositoryLocale = repositoryLocales.get(j);
RepositoryLocaleRepository repositoryLocale = repositoryLocales.get(j);
String bcp47Tag = repositoryLocale.getLocale().getBcp47Tag();
if (repositoryLocale.isToBeFullyTranslated()) {
consoleWriter.a(bcp47Tag).a(" ");
} else {
RepositoryLocale parentRepositoryLocale = repositoryLocale.getParentLocale();
RepositoryLocaleRepository parentRepositoryLocale = repositoryLocale.getParentLocale();
if (parentRepositoryLocale != null) {
String parentBcp47Tag = parentRepositoryLocale.getLocale().getBcp47Tag();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.box.l10n.mojito.cli.command;

import com.box.l10n.mojito.cli.CLITestBase;
import com.box.l10n.mojito.cli.apiclient.AssetWsApiHelper;
import com.box.l10n.mojito.cli.model.AssetAssetSummary;
import com.box.l10n.mojito.entity.Repository;
import com.box.l10n.mojito.rest.client.AssetClient;
import com.box.l10n.mojito.rest.client.RepositoryClient;
import com.box.l10n.mojito.rest.entity.Asset;
import com.box.l10n.mojito.test.XliffUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,9 +13,7 @@
*/
public class TMExportCommandTest extends CLITestBase {

@Autowired RepositoryClient repositoryClient;

@Autowired AssetClient assetClient;
@Autowired AssetWsApiHelper assetWsApiHelper;

@Test
public void export() throws Exception {
Expand All @@ -31,13 +28,15 @@ public void export() throws Exception {
"-s",
getInputResourcesTestDir("source").getAbsolutePath());

Asset asset =
assetClient.getAssetByPathAndRepositoryId("source-xliff.xliff", repository.getId());
AssetAssetSummary asset =
this.assetWsApiHelper.getAssetByPathAndRepositoryId(
"source-xliff.xliff", repository.getId());
importTranslations(asset.getId(), "source-xliff_", "fr-FR");
importTranslations(asset.getId(), "source-xliff_", "ja-JP");

Asset asset2 =
assetClient.getAssetByPathAndRepositoryId("source2-xliff.xliff", repository.getId());
AssetAssetSummary asset2 =
this.assetWsApiHelper.getAssetByPathAndRepositoryId(
"source2-xliff.xliff", repository.getId());
importTranslations(asset2.getId(), "source2-xliff_", "fr-FR");
importTranslations(asset2.getId(), "source2-xliff_", "ja-JP");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public Schema<?> resolve(
new BooleanSchema()));
return objectSchema;
}
if (annotatedType.getJsonViewAnnotation() != null) {
if (this.hasAnnotation(annotatedType, RequestBody.class)) {
annotatedType.jsonViewAnnotation(null);
return super.resolve(annotatedType, context, next);
}
if (annotatedType.getJsonViewAnnotation() != null
&& this.hasAnnotation(annotatedType, RequestBody.class)) {
annotatedType.jsonViewAnnotation(null);
return super.resolve(annotatedType, context, next);
}
if (this.hasAnnotation(annotatedType, JsonBackReference.class)) {
if (this.hasAnnotation(annotatedType, JsonBackReference.class)
&& !this.hasAnnotation(annotatedType, io.swagger.v3.oas.annotations.media.Schema.class)) {
return null;
}
return super.resolve(annotatedType, context, next);
Expand Down

0 comments on commit 5282d54

Please sign in to comment.