Beispiel #1
0
        public void ImportSome_EmptyDatabase_OnlyInserts()
        {
            var incoming = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1"
                        }
                    }
                },
                new LocalizationResource("key2")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 2"
                        }
                    }
                }
            };
            var sut = new ResourceImporter();

            var result = sut.DetectChanges(incoming, new List <LocalizationResource>());

            Assert.Equal(incoming.Count, result.Count(c => c.ChangeType == ChangeType.Insert));
        }
Beispiel #2
0
        public void ImportOne_WithSameTranslation_HasMoreTranslationsAlready()
        {
            var incoming = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "no", Value = "Resurs 1"
                        }
                    }
                }
            };
            var existing = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = null
                        },
                        new LocalizationResourceTranslation {
                            Language = "no", Value = "Resurs 1"
                        }
                    }
                }
            };
            var sut = new ResourceImporter();

            var result = sut.DetectChanges(incoming, existing);

            Assert.Empty(result);
        }
Beispiel #3
0
        public ViewResult ImportResources(bool?previewImport, HttpPostedFileBase importFile, bool?showMenu)
        {
            var model = new ImportResourcesViewModel {
                ShowMenu = showMenu ?? false
            };

            if (importFile == null || importFile.ContentLength == 0)
            {
                return(View("ImportResources", model));
            }

            var fileInfo = new FileInfo(importFile.FileName);

            if (fileInfo.Extension.ToLower() != ".json")
            {
                ModelState.AddModelError("file", "Uploaded file has different extension. Json file expected");
                return(View("ImportResources", model));
            }

            var importer     = new ResourceImporter();
            var serializer   = new JsonDataSerializer();
            var streamReader = new StreamReader(importFile.InputStream);
            var fileContent  = streamReader.ReadToEnd();

            try
            {
                var newResources = serializer.Deserialize <IEnumerable <LocalizationResource> >(fileContent);

                if (previewImport.HasValue && previewImport.Value)
                {
                    var changes = importer.DetectChanges(newResources, new GetAllResources.Query().Execute());

                    var availableLanguagesQuery = new AvailableLanguages.Query();
                    var languages = availableLanguagesQuery.Execute();

                    var previewModel = new PreviewImportResourcesViewModel(changes, showMenu ?? false, languages);

                    return(View("ImportPreview", previewModel));
                }

                var result = importer.Import(newResources, previewImport ?? true);

                ViewData["LocalizationProvider_ImportResult"] = result;
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }
Beispiel #4
0
        public void ImportSome_TheSameWithDifferentTranslationInDatabase_InsertsAndUpdates()
        {
            var incoming = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1"
                        }
                    }
                },
                new LocalizationResource("key2")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 2 - Changed!"
                        }
                    }
                }
            };
            var existing = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1 English"
                        },
                        new LocalizationResourceTranslation {
                            Language = "no", Value = "Resurs 1 Norsk"
                        }
                    }
                }
            };

            var sut = new ResourceImporter();

            var result = sut.DetectChanges(incoming, existing);

            Assert.Equal(1, result.Count(c => c.ChangeType == ChangeType.Insert));
            Assert.Equal(1, result.Count(c => c.ChangeType == ChangeType.Update));
        }
Beispiel #5
0
        public void ImportNewResource_OneDelete_InsertsAndDeletes()
        {
            var incoming = new List <LocalizationResource>
            {
                new LocalizationResource("new1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1"
                        }
                    }
                },
                new LocalizationResource("new2")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 2"
                        }
                    }
                }
            };
            var existing = new List <LocalizationResource>
            {
                new LocalizationResource("existing")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1"
                        }
                    }
                }
            };

            var sut = new ResourceImporter();

            var result = sut.DetectChanges(incoming, existing);

            Assert.Equal(2, result.Count(c => c.ChangeType == ChangeType.Insert));
            Assert.Equal(1, result.Count(c => c.ChangeType == ChangeType.Delete));
        }
Beispiel #6
0
        public void ImportNewResource_OneAlreadyExists_OnlyInserts()
        {
            var incoming = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1"
                        }
                    }
                },
                new LocalizationResource("key2")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 2"
                        }
                    }
                }
            };
            var existing = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1"
                        }
                    }
                }
            };

            var sut = new ResourceImporter();

            var result = sut.DetectChanges(incoming, existing);

            Assert.Equal(1, result.Count(c => c.ChangeType == ChangeType.Insert));
            Assert.Equal(0, result.Count(c => c.ChangeType == ChangeType.Update));
        }
Beispiel #7
0
        public void ImportOne_CheckChangedLanguageWithDifferentTranslations()
        {
            var incoming = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1 CHANGED"
                        },
                        new LocalizationResourceTranslation {
                            Language = "no", Value = "Resurs 1 CHANGED"
                        }
                    }
                }
            };
            var existing = new List <LocalizationResource>
            {
                new LocalizationResource("key1")
                {
                    Translations = new List <LocalizationResourceTranslation>
                    {
                        new LocalizationResourceTranslation {
                            Language = "en", Value = "Resource 1 English"
                        },
                        new LocalizationResourceTranslation {
                            Language = "no", Value = "Resurs 1 Norsk"
                        }
                    }
                }
            };
            var sut = new ResourceImporter();

            var result = sut.DetectChanges(incoming, existing);

            Assert.Equal(1, result.Count(c => c.ChangeType == ChangeType.Update));

            var firstChange = result.First();

            Assert.Equal(new[] { "en", "no" }, firstChange.ChangedLanguages.ToArray());
        }