use of com.opensymphony.xwork2.DefaultLocaleProviderFactory in project struts by apache.
the class I18nInterceptorTest method setUp.
public void setUp() throws Exception {
interceptor = new I18nInterceptor();
interceptor.setLocaleProviderFactory(new DefaultLocaleProviderFactory());
interceptor.init();
session = new HashMap<>();
ac = ActionContext.of(new HashMap<>()).bind().withSession(session).withParameters(HttpParameters.create().build());
request = new MockHttpServletRequest();
request.setSession(new MockHttpSession());
ServletActionContext.setRequest(request);
Action action = () -> Action.SUCCESS;
MockActionProxy proxy = new MockActionProxy();
proxy.setAction(action);
proxy.setNamespace("i18n");
proxy.setActionName("anAction");
mai = new MockActionInvocation();
((MockActionInvocation) mai).setAction(action);
((MockActionInvocation) mai).setInvocationContext(ac);
((MockActionInvocation) mai).setProxy(proxy);
}
use of com.opensymphony.xwork2.DefaultLocaleProviderFactory in project ipt by gbif.
the class TranslationActionTest method setup.
@BeforeEach
public void setup() throws Exception {
// mock needed managers
SimpleTextProvider mockTextProvider = mock(SimpleTextProvider.class);
LocaleProviderFactory localeProviderFactory = new DefaultLocaleProviderFactory();
AppConfig mockCfg = mock(AppConfig.class);
ResourceManager mockResourceManager = mock(ResourceManager.class);
SourceManager mockSourceManager = mock(SourceManager.class);
VocabulariesManager mockVocabManager = mock(VocabulariesManager.class);
TranslationAction.Translation translation = new TranslationAction.Translation();
RegistrationManager mockRegistrationManager = mock(RegistrationManager.class);
Container container = mock(Container.class);
// mock getting list of values back for BasisOfRecord field/column in source
Set<String> values = new LinkedHashSet<>();
values.add("spe");
values.add("obs");
values.add("fos");
when(mockSourceManager.inspectColumn(any(SourceBase.class), anyInt(), anyInt(), anyInt())).thenReturn(values);
// mock getI18nVocab - only called in prepare()
Map<String, String> mockVocab = new HashMap<>();
mockVocab.put("NomenclaturalChecklist", "Nomenclatural Checklist");
mockVocab.put("MachineObservation", "Machine Observation");
when(mockVocabManager.getI18nVocab(anyString(), anyString(), anyBoolean())).thenReturn(mockVocab);
// initialize new Resource
Resource resource = new Resource();
String resourceShortName = "TestResource";
resource.setShortname(resourceShortName);
// initialize new ExtensionMapping
ExtensionMapping mapping = new ExtensionMapping();
// add source to mapping
mapping.setSource(new TextFileSource());
ExtensionFactory factory = ExtensionFactoryTest.getFactory();
Extension e = factory.build(ExtensionFactoryTest.class.getResourceAsStream("/extensions/dwc_occurrence.xml"));
// ensure rowType for Extension is set
if (e.getRowType() == null) {
e.setRowType(Constants.DWC_ROWTYPE_TAXON);
}
// add extension to ExtensionMapping
mapping.setExtension(e);
// create map of source value
TreeMap<String, String> sourceValues = new TreeMap<>();
sourceValues.put("k1", "spe");
sourceValues.put("k2", "obs");
// create map of translation values
TreeMap<String, String> translatedValues = new TreeMap<>();
translatedValues.put("k1", "Preserved Specimen");
translatedValues.put("k2", "observation");
// create map of translations that get persisted
Map<String, String> persistedTranslations = new HashMap<>();
persistedTranslations.put("spe", "Preserved Specimen");
persistedTranslations.put("obs", "observation");
// initialize PropertyMapping for BasisOfRecord term
PropertyMapping field = new PropertyMapping();
// set ConceptTerm
field.setTerm(DwcTerm.basisOfRecord);
// set index
field.setIndex(1);
// add translations to field
field.setTranslation(persistedTranslations);
// add set of PropertyMapping, including field, to ExtensionMapping
Set<PropertyMapping> fields = new TreeSet<>();
fields.add(field);
mapping.setFields(fields);
// add ExtensionMapping to resource, with mapping ID 0
List<ExtensionMapping> mappings = new LinkedList<>();
mappings.add(mapping);
resource.setMappings(mappings);
// mock resourceManager.get - called only in ManagerBaseAction.prepare()
when(mockResourceManager.get(anyString())).thenReturn(resource);
// mock a locale provider
when(container.getInstance(LocaleProviderFactory.class)).thenReturn(localeProviderFactory);
// create mock Action
action = new TranslationAction(mockTextProvider, mockCfg, mockRegistrationManager, mockResourceManager, mockSourceManager, mockVocabManager, translation);
action.setContainer(container);
// initialize ExtensionProperty representing BasisOfRecord field on Occurrence core Extension
ExtensionProperty property = mapping.getExtension().getProperty(field.getTerm());
// set a vocabulary for the BasisOfRecord field
// mock creation of BasisOfRecord vocabulary
VocabularyConcept concept = new VocabularyConcept();
concept.setIdentifier("PreservedSpecimen");
concept.setUri("http://rs.tdwg.org/dwc/dwctype/PreservedSpecimen");
// preferred titles
Set<VocabularyTerm> preferredTerms = new HashSet<>();
VocabularyTerm term = new VocabularyTerm();
term.setLang("en");
term.setTitle("Preserved Specimen");
preferredTerms.add(term);
concept.setPreferredTerms(preferredTerms);
// alternative titles
Set<VocabularyTerm> alternateTerms = new HashSet<>();
term = new VocabularyTerm();
term.setLang("en");
term.setTitle("Conserved Specimen");
alternateTerms.add(term);
concept.setAlternativeTerms(alternateTerms);
Vocabulary vocab = new Vocabulary();
List<VocabularyConcept> concepts = new ArrayList<>();
concepts.add(concept);
vocab.setConcepts(concepts);
vocab.setUriString("http://rs.gbif.org/vocabulary/dwc/basis_of_record");
property.setVocabulary(vocab);
// create sessionScoped Translation
// populate sessionScoped Translation with translations
action.getTrans().setTmap(mapping.getExtension().getRowType(), property, sourceValues, translatedValues);
// set various properties on Action
action.setField(field);
action.setExtensionMapping(mapping);
action.setProperty(property);
// mock servlet request
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
// the mapping id is 0 - relates to resource's List<ExtensionMapping> mappings
when(mockRequest.getParameter(TranslationAction.REQ_PARAM_MAPPINGID)).thenReturn("0");
when(mockRequest.getParameter(TranslationAction.REQ_PARAM_ROWTYPE)).thenReturn(Constants.DWC_ROWTYPE_OCCURRENCE);
when(mockRequest.getParameter(TranslationAction.REQ_PARAM_TERM)).thenReturn(DwcTerm.basisOfRecord.qualifiedName());
when(mockRequest.getParameter(Constants.REQ_PARAM_RESOURCE)).thenReturn(resourceShortName);
action.setServletRequest(mockRequest);
// ensure the resource is set
action.setResource(resource);
}
use of com.opensymphony.xwork2.DefaultLocaleProviderFactory in project ipt by gbif.
the class HomeActionTest method setup.
@BeforeEach
public void setup() throws IOException {
LocaleProviderFactory localeProviderFactory = new DefaultLocaleProviderFactory();
Container container = mock(Container.class);
organisation.setName("NHM");
// construct public resource having one public published version 1.34
Resource p = new Resource();
p.setShortname("res");
// different than version 1.34
p.setTitle("Danish Lepidoptera");
// different than version 1.34
p.setModified(new Date());
p.setNextPublished(NEXT_PUBLISHED);
p.setLastPublished(LAST_PUBLISHED);
p.setEmlVersion(VERSION_ONE_THREE_FOUR);
p.setStatus(PublicationStatus.PUBLIC);
VersionHistory vh = new VersionHistory(VERSION_ONE_THREE_FOUR, LAST_PUBLISHED, PublicationStatus.PUBLIC);
vh.setRecordsPublished(RECORDS_PUBLISHED);
p.addVersionHistory(vh);
// different than version 1.34
p.setRecordsPublished(999999);
p.setOrganisation(organisation);
List<Resource> publishedPublic = new ArrayList<>();
publishedPublic.add(p);
ResourceManager resourceManager = mock(ResourceManager.class);
when(resourceManager.listPublishedPublicVersions()).thenReturn(publishedPublic);
when(resourceManager.get(anyString())).thenReturn(p);
AppConfig appConfig = mock(AppConfig.class);
DataDir dataDir = mock(DataDir.class);
// retrieve eml.xml file corresponding to version 1.34
File emlXMLv134 = FileUtils.getClasspathFile("resources/res1/eml.xml");
when(dataDir.resourceEmlFile(anyString(), any(BigDecimal.class))).thenReturn(emlXMLv134);
when(appConfig.getDataDir()).thenReturn(dataDir);
// mock a locale provider
when(container.getInstance(LocaleProviderFactory.class)).thenReturn(localeProviderFactory);
action = new HomeAction(mock(SimpleTextProvider.class), appConfig, mock(RegistrationManager.class), resourceManager, mock(VocabulariesManager.class));
action.setContainer(container);
action.setServletRequest(mock(HttpServletRequest.class));
}
use of com.opensymphony.xwork2.DefaultLocaleProviderFactory in project ipt by gbif.
the class ResourceActionTest method setup.
@BeforeEach
public void setup() throws IOException, TemplateException {
SimpleTextProvider textProvider = new SimpleTextProvider();
LocaleProviderFactory localeProviderFactory = new DefaultLocaleProviderFactory();
AppConfig mockCfg = mock(AppConfig.class);
RegistrationManager mockRegistrationManager = mock(RegistrationManager.class);
ResourceManager mockResourceManager = mock(ResourceManager.class);
VocabulariesManager mockVocabManager = mock(VocabulariesManager.class);
DataDir mockDataDir = mock(DataDir.class);
Container container = mock(Container.class);
// mock: vocabManager.getI18nVocab(Constants.VOCAB_URI_RANKS, Locale.getDefault().getLanguage(), false);
Map<String, String> ranks = new LinkedHashMap<>();
ranks.put("kingdom", "http://rs.gbif.org/vocabulary/gbif/rank/kingdom");
ranks.put("Class", "http://rs.gbif.org/vocabulary/gbif/rank/class");
when(mockVocabManager.getI18nVocab(anyString(), anyString(), anyBoolean())).thenReturn(ranks);
// setup Resource with TaxonomicCoverage with 3 TaxonKeyword
resource = new Resource();
resource.setShortname(RESOURCE_SHORT_NAME);
resource.setEmlVersion(LATEST_RESOURCE_VERSION);
// setup manager as resource creator
MANAGER = new User();
MANAGER.setEmail("jc@gbif.org");
MANAGER.setLastname("Costa");
MANAGER.setFirstname("Jose");
MANAGER.setRole(User.Role.Manager);
resource.setCreator(MANAGER);
// add three published versions to version history, all published by manager, some private, other public
VersionHistory v1 = new VersionHistory(RESOURCE_VERSION_ONE, new Date(), PublicationStatus.PRIVATE);
resource.addVersionHistory(v1);
VersionHistory v2 = new VersionHistory(RESOURCE_VERSION_TWO, new Date(), PublicationStatus.PUBLIC);
resource.addVersionHistory(v2);
VersionHistory v3 = new VersionHistory(LATEST_RESOURCE_VERSION, new Date(), PublicationStatus.PRIVATE);
resource.addVersionHistory(v3);
assertEquals(3, resource.getVersionHistory().size());
TaxonomicCoverage coverage1 = new TaxonomicCoverage();
coverage1.setDescription("Description1");
TaxonKeyword keyword1 = new TaxonKeyword();
keyword1.setRank("Kingdom");
keyword1.setCommonName("Plants");
keyword1.setScientificName("Plantae");
TaxonKeyword keyword2 = new TaxonKeyword();
keyword2.setRank("Class");
keyword2.setScientificName("Equisetopsida");
TaxonKeyword keyword3 = new TaxonKeyword();
keyword3.setCommonName("Sedges");
List<TaxonKeyword> keywordList = new ArrayList<>();
keywordList.add(keyword1);
keywordList.add(keyword2);
keywordList.add(keyword3);
coverage1.setTaxonKeywords(keywordList);
List<TaxonomicCoverage> coverages = new ArrayList<>();
coverages.add(coverage1);
resource.getEml().setTaxonomicCoverages(coverages);
// mock returning EML file, with actual resource metadata
File emlFile = File.createTempFile("eml-3.0.xml", ".xml");
EmlWriter.writeEmlFile(emlFile, resource.getEml());
when(mockDataDir.resourceEmlFile(anyString(), any(BigDecimal.class))).thenReturn(emlFile);
// mock returning RTF file, with empty content
File rtfFile = File.createTempFile(RESOURCE_SHORT_NAME + "-3.0", ".rtf");
when(mockDataDir.resourceRtfFile(anyString(), any(BigDecimal.class))).thenReturn(rtfFile);
// mock returning DwC-A file, with file that doesn't exist. This means the resource is metadata-only
File nonExistingDwca = new File("dwca", ".zip");
assertFalse(nonExistingDwca.exists());
when(mockDataDir.resourceDwcaFile(anyString(), any(BigDecimal.class))).thenReturn(nonExistingDwca);
// mock a locale provider
when(container.getInstance(LocaleProviderFactory.class)).thenReturn(localeProviderFactory);
action = new ResourceAction(textProvider, mockCfg, mockRegistrationManager, mockResourceManager, mockVocabManager, mockDataDir, mock(ExtensionManager.class));
action.setResource(resource);
action.setContainer(container);
}
Aggregations