Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DeploymentServer Prototype/HeuristicLab.Services/HeuristicLab.Services.Deployment.Test/PluginStoreTest.cs @ 2766

Last change on this file since 2766 was 2766, checked in by gkronber, 14 years ago

Worked on deployment server. Fixed DB-schema. Implemented all test-cases. #860

File size: 17.5 KB
RevLine 
[2742]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 = Guid.NewGuid().ToString();
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 = Guid.NewGuid().ToString();
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 = Guid.NewGuid().ToString();
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 = Guid.NewGuid().ToString();
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 = Guid.NewGuid().ToString();
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 = Guid.NewGuid().ToString();
171          string dependencyName = Guid.NewGuid().ToString();
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 = Guid.NewGuid().ToString();
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);
[2766]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        }
[2742]201      }
202      #endregion
203
204      #region update the dependencies of an existing plugin
205      {
206        string dependency1Name = Guid.NewGuid().ToString();
207        var newDependency1 = new PluginDescription(dependency1Name, vers01);
208        target.Persist(newDependency1, enc.GetBytes("Zipped " + dependency1Name));
209
210        string pluginName = Guid.NewGuid().ToString();
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 = Guid.NewGuid().ToString();
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 = Guid.NewGuid().ToString();
235        var depPlugin = new PluginDescription(depName, vers01);
236        target.Persist(depPlugin, enc.GetBytes("Zipped " + depName));
237
238        // insert new plugin
239        string pluginName = Guid.NewGuid().ToString();
240        var plugin = new PluginDescription(pluginName, vers01, new PluginDescription[] { depPlugin, depPlugin });
241        try {
[2766]242          target.Persist(plugin, enc.GetBytes("Zipped " + depName));
[2742]243          Assert.Fail("Expected ArgumentException");
244        }
245        catch (ArgumentException) {
246          Assert.IsTrue(true, "Exception thrown as expected");
247        }
248      }
249      #endregion
250    }
251
252    /// <summary>
253    ///A test for Persist
254    ///</summary>
255    [TestMethod()]
256    public void PersistProductTest() {
257      Version vers01 = new Version(0, 1);
258      PluginStore target = new PluginStore();
259
260      #region persist a product without plugins to the db
261      {
262        string prodName = Guid.NewGuid().ToString();
263        int oldCount = target.Products.Count();
264        ProductDescription product = new ProductDescription(prodName, vers01);
265        target.Persist(product);
266        int newCount = target.Products.Count();
267        Assert.AreEqual(oldCount + 1, newCount);
268      }
269      #endregion
270
271      #region persist a product with the same name and version as an existant product
272      {
273        string prodName = Guid.NewGuid().ToString();
274        int oldCount = target.Products.Count();
275        ProductDescription product = new ProductDescription(prodName, vers01);
276        target.Persist(product);
277        int newCount = target.Products.Count();
278        Assert.AreEqual(oldCount + 1, newCount);
279
280        // write a product with same name and version
281        oldCount = target.Products.Count();
282        product = new ProductDescription(prodName, vers01);
283        target.Persist(product);
284        newCount = target.Products.Count();
285
286        // make sure that the old entry was updated
287        Assert.AreEqual(oldCount, newCount);
288
289        // write a product with same name and different version
290        oldCount = target.Products.Count();
291        product = new ProductDescription(prodName, new Version(0, 2));
292        target.Persist(product);
293        newCount = target.Products.Count();
294
295        // make sure that a new entry was created
296        Assert.AreEqual(oldCount + 1, newCount);
297      }
298      #endregion
299
300      #region try to persist a product referencing an non-existant plugin and check the expected exception
301      {
302        // try to persist a product referencing an non-existant plugin
303        string prodName = Guid.NewGuid().ToString();
304        string pluginName = Guid.NewGuid().ToString();
305        var plugin = new PluginDescription(pluginName, vers01);
306        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin, 1));
307        try {
308          target.Persist(product);
309          Assert.Fail("persist should fail with ArgumentException");
310        }
311        catch (ArgumentException e) {
312          // this is expected
313          Assert.IsTrue(true, "expected exception");
314        }
315      }
316      #endregion
317
318      #region persist a product with a single plugin reference
319      {
320        string prodName = Guid.NewGuid().ToString();
321        string pluginName = Guid.NewGuid().ToString();
322        var plugin = new PluginDescription(pluginName, vers01);
323        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin, 1));
324
325        // persist the plugin first
326        int oldCount = target.Products.Count();
327        target.Persist(plugin, enc.GetBytes("Zipped " + plugin.Name));
328        target.Persist(product);
329        int newCount = target.Products.Count();
330        // make sure the store went through
331        Assert.AreEqual(oldCount + 1, newCount);
332        // retrieve the product and check if the plugin list was stored/retrieved correctly
333        var dbProd = target.Products.Where(p => p.Name == prodName).Single();
334        Assert.AreEqual(dbProd.Plugins.Count(), 1);
335        Assert.AreEqual(dbProd.Plugins.First().Name, pluginName);
336      }
337      #endregion
338
339      #region update the plugin list of an existing product
340      {
341        string prodName = Guid.NewGuid().ToString();
342        string plugin1Name = Guid.NewGuid().ToString();
343        var plugin1 = new PluginDescription(plugin1Name, vers01);
344        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin1, 1));
345
346        // persist the plugin first
347        int oldCount = target.Products.Count();
348        target.Persist(plugin1, enc.GetBytes("Zipped " + plugin1.Name));
349        target.Persist(product);
350        int newCount = target.Products.Count();
351        // make sure the store went through
352        Assert.AreEqual(oldCount + 1, newCount);
353
354        var plugin2Name = Guid.NewGuid().ToString();
355        var plugin2 = new PluginDescription(plugin2Name, vers01);
356        target.Persist(plugin2, enc.GetBytes("Zipped " + plugin2.Name));
357        product = new ProductDescription(prodName, vers01, new PluginDescription[] { plugin1, plugin2 });
358        oldCount = target.Products.Count();
359        target.Persist(product);
360        newCount = target.Products.Count();
361        // make sure that a new entry was not created
362        Assert.AreEqual(oldCount, newCount);
363
364        // check the plugin list of the product
365        var dbProduct = target.Products.Where(p => p.Name == prodName).Single();
366        Assert.AreEqual(dbProduct.Plugins.Count(), 2);
367      }
368      #endregion
369
370      #region insert a product which references the same plugin twice and check if the correct exception is thrown
371      {
372        string prodName = Guid.NewGuid().ToString();
373        string plugin1Name = Guid.NewGuid().ToString();
374        var plugin1 = new PluginDescription(plugin1Name, vers01);
375        var product = new ProductDescription(prodName, vers01, Enumerable.Repeat(plugin1, 2));
376
377        // persist the plugin first
378        target.Persist(plugin1, enc.GetBytes("Zipped " + plugin1.Name));
379        try {
380          target.Persist(product);
381          Assert.Fail("Expected ArgumentException");
382        }
383        catch (ArgumentException) {
384          Assert.IsTrue(true, "Expected exception was thrown.");
385        }
386      }
387      #endregion
388    }
389
390    [TestMethod]
391    public void InsertPluginPerformanceTest() {
392      int nPlugins = 100;
393      int maxDependencies = 10;
394      int avgZipFileLength = 32000;
395
396      var store = new PluginStore();
397      Version vers01 = new Version(0, 1);
398      // create a random byte array to represent file length
399      byte[] zippedConstant = new byte[avgZipFileLength];
400      Random r = new Random();
401      r.NextBytes(zippedConstant);
402
403      Stopwatch stopWatch = new Stopwatch();
404      stopWatch.Start();
405      // create plugins
406      List<PluginDescription> plugins = new List<PluginDescription>();
407      for (int i = 0; i < nPlugins; i++) {
408        string name = Guid.NewGuid().ToString();
409        var dependencies = store.Plugins;
410        if (dependencies.Count() > maxDependencies) dependencies = dependencies.Take(maxDependencies);
411        var plugin = new PluginDescription(name, vers01, dependencies);
412        store.Persist(plugin, zippedConstant);
413        plugins.Add(plugin);
414      }
415      stopWatch.Stop();
416      Assert.Inconclusive("Created " + nPlugins + " plugins in " + stopWatch.ElapsedMilliseconds +
417        " ms (" + (double)stopWatch.ElapsedMilliseconds / nPlugins + " ms / plugin )");
418    }
419    [TestMethod]
420    public void InsertProductPerformanceTest() {
421      int nProducts = 50;
422      int avgProductPlugins = 30;
423
424      var store = new PluginStore();
425      Version vers01 = new Version(0, 1);
426      Stopwatch stopWatch = new Stopwatch();
427      Random r = new Random();
428      var plugins = store.Plugins.ToList();
429      stopWatch.Start();
430      // create products
431      for (int i = 0; i < nProducts; i++) {
432        string name = Guid.NewGuid().ToString();
433        List<PluginDescription> prodPlugins = new List<PluginDescription>();
434        for (int j = 0; j < avgProductPlugins; j++) {
435          prodPlugins.Add(plugins[r.Next(0, plugins.Count)]);
436        }
437        var prod = new ProductDescription(name, vers01, prodPlugins);
438        store.Persist(prod);
439      }
440      stopWatch.Stop();
441      Assert.Inconclusive("Created " + nProducts + " products in " + stopWatch.ElapsedMilliseconds +
442        " ms (" + (double)stopWatch.ElapsedMilliseconds / nProducts + " ms / product )");
443    }
444  }
445}
Note: See TracBrowser for help on using the repository browser.