Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Deployment/3.3/Tests/PluginStoreTest.cs @ 3088

Last change on this file since 3088 was 3088, checked in by gkronber, 15 years ago

copied unit test cases for class PluginStore of the deployment service from the feature development branch. #918 (Integrate deployment service into trunk and HL3.3 solution file)

File size: 18.1 KB
Line 
1using HeuristicLab.Services.Deployment.DataAccess;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using System.Collections.Generic;
4using System.Linq;
5using System;
6using System.Diagnostics;
7
8namespace HeuristicLab.Services.Deployment.Test {
9
10
11  /// <summary>
12  ///This is a test class for PluginStoreTest and is intended
13  ///to contain all PluginStoreTest Unit Tests
14  ///</summary>
15  [TestClass()]
16  public class PluginStoreTest {
17
18    private System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
19    private TestContext testContextInstance;
20
21    /// <summary>
22    ///Gets or sets the test context which provides
23    ///information about and functionality for the current test run.
24    ///</summary>
25    public TestContext TestContext {
26      get {
27        return testContextInstance;
28      }
29      set {
30        testContextInstance = value;
31      }
32    }
33
34    #region Additional test attributes
35    //
36    //You can use the following additional attributes as you write your tests:
37    //
38    //Use ClassInitialize to run code before running the first test in the class
39    //[ClassInitialize()]
40    //public static void MyClassInitialize(TestContext testContext)
41    //{
42    //}
43    //
44    //Use ClassCleanup to run code after all tests in a class have run
45    //[ClassCleanup()]
46    //public static void MyClassCleanup()
47    //{
48    //}
49    //
50    //Use TestInitialize to run code before running each test
51    //[TestInitialize()]
52    //public void MyTestInitialize()
53    //{
54    //}
55    //
56    //Use TestCleanup to run code after each test has run
57    //[TestCleanup()]
58    //public void MyTestCleanup()
59    //{
60    //}
61    //
62    #endregion
63
64
65    /// <summary>
66    ///A test for PluginFile
67    ///</summary>
68    [TestMethod()]
69    public void PluginFileTest() {
70      PluginStore target = new PluginStore();
71      #region insert and retrieve a plugin file
72      int oldCount = target.Plugins.Count();
73      // store a new entry in the db
74      string name = RandomName();
75      target.Persist(new PluginDescription(name, new Version(0, 1)), enc.GetBytes("Zipped " + name));
76      int newCount = target.Plugins.Count();
77      // make sure the new entry was added to the db
78      Assert.AreEqual(oldCount + 1, newCount);
79
80      // get matching description from db and try to retrieve the stored file
81      PluginDescription pluginDescription = target.Plugins.Where(p => p.Name == name).Single();
82      byte[] expected = enc.GetBytes("Zipped " + name);
83      byte[] actual = target.PluginFile(pluginDescription);
84      // check retrieved file
85      Assert.AreEqual(expected.Length, actual.Length);
86      for (int i = 0; i < expected.Length; i++) {
87        Assert.AreEqual(expected[i], actual[i]);
88      }
89      #endregion
90    }
91
92    /// <summary>
93    ///A test for Persist
94    ///</summary>
95    [TestMethod()]
96    public void PersistPluginTest() {
97      var vers01 = new Version(0, 1);
98      PluginStore target = new PluginStore();
99
100      #region persist single plugin without dependencies
101      {
102        string name = RandomName();
103        int oldCount = target.Plugins.Count();
104        PluginDescription pluginDescription = new PluginDescription(name, vers01);
105        byte[] pluginPackage = enc.GetBytes("Zipped " + name);
106        target.Persist(pluginDescription, pluginPackage);
107        int newCount = target.Plugins.Count();
108        Assert.AreEqual(oldCount + 1, newCount);
109      }
110      #endregion
111
112      #region persist a product with same name and version as an existent product
113      {
114        string name = RandomName();
115        int oldCount = target.Plugins.Count();
116        PluginDescription pluginDescription = new PluginDescription(name, vers01);
117        byte[] pluginPackage = enc.GetBytes("Zipped " + name);
118        target.Persist(pluginDescription, pluginPackage);
119        int newCount = target.Plugins.Count();
120        Assert.AreEqual(oldCount + 1, newCount);
121
122        // insert same name and version
123        oldCount = target.Plugins.Count();
124        pluginDescription = new PluginDescription(name, vers01);
125        pluginPackage = enc.GetBytes("Zipped " + name);
126        target.Persist(pluginDescription, pluginPackage);
127        newCount = target.Plugins.Count();
128        // make sure old entry was updated
129        Assert.AreEqual(oldCount, newCount);
130
131        // insert new with different version
132        oldCount = target.Plugins.Count();
133        pluginDescription = new PluginDescription(name, new Version(0, 2));
134        pluginPackage = enc.GetBytes("Zipped " + name);
135        target.Persist(pluginDescription, pluginPackage);
136        newCount = target.Plugins.Count();
137        // make sure a new entry was created
138        Assert.AreEqual(oldCount + 1, newCount);
139      }
140      #endregion
141
142      #region persist a plugin with an already persisted dependency
143      {
144        string name = RandomName();
145        PluginDescription dependency = new PluginDescription(name, vers01);
146        // insert dependency first
147        target.Persist(dependency, enc.GetBytes("Zipped " + name));
148
149        // persist another plugin that has a dependency on the first plugin
150        string name2 = RandomName();
151        int oldCount = target.Plugins.Count();
152
153        PluginDescription newEntity = new PluginDescription(name2, vers01, Enumerable.Repeat(dependency, 1));
154        byte[] newEntityPackage = enc.GetBytes("Zipped " + name2);
155        target.Persist(newEntity, newEntityPackage);
156        int newCount = target.Plugins.Count();
157        Assert.AreEqual(oldCount + 1, newCount); // only one new plugin should be added
158
159        // retrieve second plugin and check dependencies
160        newEntity = target.Plugins.Where(p => p.Name == name2).Single();
161        Assert.AreEqual(newEntity.Dependencies.Count(), 1);
162        Assert.AreEqual(newEntity.Dependencies.First().Name, name);
163      }
164      #endregion
165
166      #region try to persist a new  plugin with a non-existant dependency and check the expected exception
167      {
168        // try to persist a new plugin with a non-existant dependency
169        try {
170          string pluginName = RandomName();
171          string dependencyName = RandomName();
172          var dependency = new PluginDescription(dependencyName, vers01);
173          var newEntity = new PluginDescription(pluginName, vers01, Enumerable.Repeat(dependency, 1));
174          target.Persist(newEntity, enc.GetBytes("Zipped " + pluginName));
175          Assert.Fail("persist should fail with ArgumentException");
176        }
177        catch (ArgumentException e) {
178          // this is expected
179          Assert.IsTrue(true, "expected exception");
180        }
181      }
182      #endregion
183
184      #region update the plugin file of an existing plugin
185      {
186        // insert new plugin
187        string pluginName = RandomName();
188        var newPlugin = new PluginDescription(pluginName, vers01);
189        target.Persist(newPlugin, enc.GetBytes("Zipped " + pluginName));
190
191        // update the plugin file
192        byte[] expected = enc.GetBytes("Zipped2 " + pluginName);
193        target.Persist(newPlugin, expected);
194        // check if the updated file is returned
195        byte[] actual = target.PluginFile(newPlugin);
196        // check retrieved file
197        Assert.AreEqual(expected.Length, actual.Length);
198        for (int i = 0; i < expected.Length; i++) {
199          Assert.AreEqual(expected[i], actual[i]);
200        }
201      }
202      #endregion
203
204      #region update the dependencies of an existing plugin
205      {
206        string dependency1Name = RandomName();
207        var newDependency1 = new PluginDescription(dependency1Name, vers01);
208        target.Persist(newDependency1, enc.GetBytes("Zipped " + dependency1Name));
209
210        string pluginName = RandomName();
211        var newPlugin = new PluginDescription(pluginName, vers01, Enumerable.Repeat(newDependency1, 1));
212        target.Persist(newPlugin, enc.GetBytes("Zipped " + pluginName));
213
214        // retrieve plugin
215        var dbPlugin = target.Plugins.Where(p => p.Name == pluginName).Single();
216        Assert.AreEqual(dbPlugin.Dependencies.Count(), 1);
217        Assert.AreEqual(dbPlugin.Dependencies.First().Name, dependency1Name);
218
219        // change dependencies
220        string dependency2Name = RandomName();
221        var newDependency2 = new PluginDescription(dependency2Name, vers01);
222        target.Persist(newDependency2, enc.GetBytes("Zipped " + pluginName));
223
224        newPlugin = new PluginDescription(pluginName, vers01, new PluginDescription[] { newDependency1, newDependency2 });
225        target.Persist(newPlugin, enc.GetBytes("Zipped " + pluginName));
226        // retrieve plugin
227        dbPlugin = target.Plugins.Where(p => p.Name == pluginName).Single();
228        Assert.AreEqual(dbPlugin.Dependencies.Count(), 2);
229      }
230      #endregion
231
232      #region try to insert a plugin that references the same dependency twice and check if the correct exception is thrown
233      {
234        string depName = RandomName();
235        var depPlugin = new PluginDescription(depName, vers01);
236        target.Persist(depPlugin, enc.GetBytes("Zipped " + depName));
237
238        // insert new plugin
239        string pluginName = RandomName();
240        var plugin = new PluginDescription(pluginName, vers01, new PluginDescription[] { depPlugin, depPlugin });
241        try {
242          target.Persist(plugin, enc.GetBytes("Zipped " + depName));
243          Assert.Fail("Expected ArgumentException");
244        }
245        catch (ArgumentException) {
246          Assert.IsTrue(true, "Exception thrown as expected");
247        }
248      }
249      #endregion
250
251      #region try to insert a plugin that with a cyclic reference
252      {
253        string depName = RandomName();
254        var depPlugin = new PluginDescription(depName, vers01);
255        target.Persist(depPlugin, enc.GetBytes("Zipped " + depName));
256
257        // update the plugin so that it has a dependency on itself
258        var plugin = new PluginDescription(depName, vers01, new PluginDescription[] { depPlugin });
259        try {
260          target.Persist(plugin, enc.GetBytes("Zipped " + depName));
261          Assert.Fail("Expected ArgumentException");
262        }
263        catch (ArgumentException) {
264          Assert.IsTrue(true, "Exception thrown as expected");
265        }
266      }
267      #endregion
268
269    }
270
271    /// <summary>
272    ///A test for Persist
273    ///</summary>
274    [TestMethod()]
275    public void PersistProductTest() {
276      Version vers01 = new Version(0, 1);
277      PluginStore target = new PluginStore();
278
279      #region persist a product without plugins to the db
280      {
281        string prodName = RandomName();
282        int oldCount = target.Products.Count();
283        ProductDescription product = new ProductDescription(prodName, vers01);
284        target.Persist(product);
285        int newCount = target.Products.Count();
286        Assert.AreEqual(oldCount + 1, newCount);
287      }
288      #endregion
289
290      #region persist a product with the same name and version as an existant product
291      {
292        string prodName = RandomName();
293        int oldCount = target.Products.Count();
294        ProductDescription product = new ProductDescription(prodName, vers01);
295        target.Persist(product);
296        int newCount = target.Products.Count();
297        Assert.AreEqual(oldCount + 1, newCount);
298
299        // write a product with same name and version
300        oldCount = target.Products.Count();
301        product = new ProductDescription(prodName, vers01);
302        target.Persist(product);
303        newCount = target.Products.Count();
304
305        // make sure that the old entry was updated
306        Assert.AreEqual(oldCount, newCount);
307
308        // write a product with same name and different version
309        oldCount = target.Products.Count();
310        product = new ProductDescription(prodName, new Version(0, 2));
311        target.Persist(product);
312        newCount = target.Products.Count();
313
314        // make sure that a new entry was created
315        Assert.AreEqual(oldCount + 1, newCount);
316      }
317      #endregion
318
319      #region try to persist a product referencing an non-existant plugin and check the expected exception
320      {
321        // try to persist a product referencing a non-existant plugin
322        string prodName = RandomName();
323        string pluginName = RandomName();
324        var plugin = new PluginDescription(pluginName, vers01);
325        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin, 1));
326        try {
327          target.Persist(product);
328          Assert.Fail("persist should fail with ArgumentException");
329        }
330        catch (ArgumentException e) {
331          // this is expected
332          Assert.IsTrue(true, "expected exception");
333        }
334      }
335      #endregion
336
337      #region persist a product with a single plugin reference
338      {
339        string prodName = RandomName();
340        string pluginName = RandomName();
341        var plugin = new PluginDescription(pluginName, vers01);
342        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin, 1));
343
344        // persist the plugin first
345        int oldCount = target.Products.Count();
346        target.Persist(plugin, enc.GetBytes("Zipped " + plugin.Name));
347        target.Persist(product);
348        int newCount = target.Products.Count();
349        // make sure the store went through
350        Assert.AreEqual(oldCount + 1, newCount);
351        // retrieve the product and check if the plugin list was stored/retrieved correctly
352        var dbProd = target.Products.Where(p => p.Name == prodName).Single();
353        Assert.AreEqual(dbProd.Plugins.Count(), 1);
354        Assert.AreEqual(dbProd.Plugins.First().Name, pluginName);
355      }
356      #endregion
357
358      #region update the plugin list of an existing product
359      {
360        string prodName = RandomName();
361        string plugin1Name = RandomName();
362        var plugin1 = new PluginDescription(plugin1Name, vers01);
363        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin1, 1));
364
365        // persist the plugin first
366        int oldCount = target.Products.Count();
367        target.Persist(plugin1, enc.GetBytes("Zipped " + plugin1.Name));
368        target.Persist(product);
369        int newCount = target.Products.Count();
370        // make sure the store went through
371        Assert.AreEqual(oldCount + 1, newCount);
372
373        var plugin2Name = RandomName();
374        var plugin2 = new PluginDescription(plugin2Name, vers01);
375        target.Persist(plugin2, enc.GetBytes("Zipped " + plugin2.Name));
376        product = new ProductDescription(prodName, vers01, new PluginDescription[] { plugin1, plugin2 });
377        oldCount = target.Products.Count();
378        target.Persist(product);
379        newCount = target.Products.Count();
380        // make sure that a new entry was not created
381        Assert.AreEqual(oldCount, newCount);
382
383        // check the plugin list of the product
384        var dbProduct = target.Products.Where(p => p.Name == prodName).Single();
385        Assert.AreEqual(dbProduct.Plugins.Count(), 2);
386      }
387      #endregion
388
389      #region insert a product which references the same plugin twice and check if the correct exception is thrown
390      {
391        string prodName = RandomName();
392        string plugin1Name = RandomName();
393        var plugin1 = new PluginDescription(plugin1Name, vers01);
394        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin1, 2));
395
396        // persist the plugin first
397        target.Persist(plugin1, enc.GetBytes("Zipped " + plugin1.Name));
398        try {
399          target.Persist(product);
400          Assert.Fail("Expected ArgumentException");
401        }
402        catch (ArgumentException) {
403          Assert.IsTrue(true, "Expected exception was thrown.");
404        }
405      }
406      #endregion
407    }
408
409    [TestMethod]
410    public void InsertPluginPerformanceTest() {
411      int nPlugins = 100;
412      int maxDependencies = 10;
413      int avgZipFileLength = 32000;
414
415      var store = new PluginStore();
416      Version vers01 = new Version(0, 1);
417      // create a random byte array to represent file length
418      byte[] zippedConstant = new byte[avgZipFileLength];
419      Random r = new Random();
420      r.NextBytes(zippedConstant);
421
422      Stopwatch stopWatch = new Stopwatch();
423      stopWatch.Start();
424      // create plugins
425      List<PluginDescription> plugins = new List<PluginDescription>();
426      for (int i = 0; i < nPlugins; i++) {
427        string name = RandomName();
428        var dependencies = store.Plugins;
429        if (dependencies.Count() > maxDependencies) dependencies = dependencies.Take(maxDependencies);
430        var plugin = new PluginDescription(name, vers01, dependencies);
431        store.Persist(plugin, zippedConstant);
432        plugins.Add(plugin);
433      }
434      stopWatch.Stop();
435      Assert.Inconclusive("Created " + nPlugins + " plugins in " + stopWatch.ElapsedMilliseconds +
436        " ms (" + (double)stopWatch.ElapsedMilliseconds / nPlugins + " ms / plugin )");
437    }
438    [TestMethod]
439    public void InsertProductPerformanceTest() {
440      int nProducts = 50;
441      int avgProductPlugins = 30;
442
443      var store = new PluginStore();
444      Version vers01 = new Version(0, 1);
445      Stopwatch stopWatch = new Stopwatch();
446      Random r = new Random();
447      var plugins = store.Plugins.ToList();
448      stopWatch.Start();
449      // create products
450      for (int i = 0; i < nProducts; i++) {
451        string name = RandomName();
452        List<PluginDescription> prodPlugins = new List<PluginDescription>();
453        for (int j = 0; j < avgProductPlugins; j++) {
454          var selectedPlugin = plugins[r.Next(0, plugins.Count)];
455          if (!prodPlugins.Contains(selectedPlugin)) prodPlugins.Add(selectedPlugin);
456        }
457        var prod = new ProductDescription(name, vers01, prodPlugins);
458        store.Persist(prod);
459      }
460      stopWatch.Stop();
461      Assert.Inconclusive("Created " + nProducts + " products in " + stopWatch.ElapsedMilliseconds +
462        " ms (" + (double)stopWatch.ElapsedMilliseconds / nProducts + " ms / product )");
463    }
464
465    private string RandomName() {
466      return Guid.NewGuid().ToString();
467    }
468  }
469}
Note: See TracBrowser for help on using the repository browser.