test trigger updater
[teamcity/dotNetPackagesSupport.git] / nuget-tests / src / jetbrains / buildServer / nuget / tests / server / NamedPackagesUpdateCheckerTest.java
1 /*\r
2  * Copyright 2000-2011 JetBrains s.r.o.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package jetbrains.buildServer.nuget.tests.server;\r
18 \r
19 import jetbrains.buildServer.BaseTestCase;\r
20 import jetbrains.buildServer.buildTriggers.BuildTriggerDescriptor;\r
21 import jetbrains.buildServer.buildTriggers.BuildTriggerException;\r
22 import jetbrains.buildServer.nuget.server.exec.ListPackagesCommand;\r
23 import jetbrains.buildServer.nuget.server.exec.PackageInfo;\r
24 import jetbrains.buildServer.nuget.server.trigger.NamedPackagesUpdateChecker;\r
25 import jetbrains.buildServer.nuget.server.trigger.TriggerConstants;\r
26 import jetbrains.buildServer.nuget.tests.integration.Paths;\r
27 import jetbrains.buildServer.serverSide.CustomDataStorage;\r
28 import junit.framework.Assert;\r
29 import org.jmock.Expectations;\r
30 import org.jmock.Mockery;\r
31 import org.testng.annotations.BeforeMethod;\r
32 import org.testng.annotations.Test;\r
33 \r
34 import java.io.File;\r
35 import java.util.Arrays;\r
36 import java.util.Map;\r
37 import java.util.TreeMap;\r
38 \r
39 /**\r
40  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
41  * Date: 14.07.11 18:57\r
42  */\r
43 public class NamedPackagesUpdateCheckerTest extends BaseTestCase {\r
44   private Mockery m;\r
45   private ListPackagesCommand cmd;\r
46   private NamedPackagesUpdateChecker checker;\r
47   private BuildTriggerDescriptor desr;\r
48   private CustomDataStorage store;\r
49   private Map<String, String> params;\r
50   private File nugetFakePath;\r
51 \r
52   @BeforeMethod\r
53   @Override\r
54   protected void setUp() throws Exception {\r
55     super.setUp();\r
56 \r
57     m = new Mockery();\r
58     cmd = m.mock(ListPackagesCommand.class);\r
59     desr = m.mock(BuildTriggerDescriptor.class);\r
60     store = m.mock(CustomDataStorage.class);\r
61     params = new TreeMap<String, String>();\r
62 \r
63     checker = new NamedPackagesUpdateChecker(cmd);\r
64 \r
65     m.checking(new Expectations(){{\r
66       allowing(desr).getProperties(); will(returnValue(params));\r
67     }});\r
68     nugetFakePath = Paths.getNuGetRunnerPath();\r
69 \r
70     params.put(TriggerConstants.NUGET_EXE, nugetFakePath.getPath());\r
71     params.put(TriggerConstants.PACKAGE, "NUnit");\r
72   }\r
73 \r
74   @Test\r
75   public void test_check_first_time_should_not_trigger() {\r
76     m.checking(new Expectations(){{\r
77       oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
78       will(returnValue(Arrays.asList(new PackageInfo("src", "pkg", "5.6.87"))));\r
79 \r
80       oneOf(store).getValue("hash"); will(returnValue(null));\r
81       oneOf(store).putValue(with(equal("hash")), with(any(String.class)));\r
82       oneOf(store).flush();\r
83     }});\r
84     Assert.assertNull(checker.checkChanges(desr, store));\r
85   }\r
86 \r
87   @Test\r
88   public void test_check_should_not_trigger_twice() {\r
89     m.checking(new Expectations(){{\r
90       oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
91       will(returnValue(Arrays.asList(new PackageInfo("src", "pkg", "5.6.87"))));\r
92 \r
93       oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
94       will(returnValue(Arrays.asList(new PackageInfo("src", "pkg", "5.6.87"))));\r
95 \r
96       oneOf(store).getValue("hash"); will(returnValue("aaa"));\r
97       oneOf(store).putValue("hash", "|s:src|p:pkg|v:5.6.87");\r
98       oneOf(store).getValue("hash"); will(returnValue("|s:src|p:pkg|v:5.6.87"));\r
99       oneOf(store).flush();\r
100     }});\r
101 \r
102     Assert.assertNotNull(checker.checkChanges(desr, store));\r
103     Assert.assertNull(checker.checkChanges(desr, store));\r
104   }\r
105 \r
106   @Test\r
107   public void test_check_should_fail_on_error() {\r
108     m.checking(new Expectations() {{\r
109       oneOf(cmd).checkForChanges(with(equal(nugetFakePath)), with(any(String.class)), with(any(String.class)), with(any(String.class)));\r
110       will(throwException(new RuntimeException("Failed to execute command")));\r
111     }});\r
112     try {\r
113       checker.checkChanges(desr, store);\r
114     } catch (BuildTriggerException e) {\r
115       return;\r
116     }\r
117     Assert.fail("should throw an exception");\r
118   }\r
119 }\r