support nuget tools reference in nuget trigger
[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.SourcePackageInfo;\r
24 import jetbrains.buildServer.nuget.server.toolRegistry.NuGetToolManager;\r
25 import jetbrains.buildServer.nuget.server.trigger.NamedPackagesUpdateChecker;\r
26 import jetbrains.buildServer.nuget.server.trigger.TriggerConstants;\r
27 import jetbrains.buildServer.nuget.tests.integration.Paths;\r
28 import jetbrains.buildServer.serverSide.CustomDataStorage;\r
29 import junit.framework.Assert;\r
30 import org.jmock.Expectations;\r
31 import org.jmock.Mockery;\r
32 import org.testng.annotations.BeforeMethod;\r
33 import org.testng.annotations.Test;\r
34 \r
35 import java.io.File;\r
36 import java.util.Arrays;\r
37 import java.util.Map;\r
38 import java.util.TreeMap;\r
39 \r
40 /**\r
41  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
42  * Date: 14.07.11 18:57\r
43  */\r
44 public class NamedPackagesUpdateCheckerTest extends BaseTestCase {\r
45   private Mockery m;\r
46   private ListPackagesCommand cmd;\r
47   private NamedPackagesUpdateChecker checker;\r
48   private BuildTriggerDescriptor desr;\r
49   private CustomDataStorage store;\r
50   private NuGetToolManager manager;\r
51   private Map<String, String> params;\r
52   private File nugetFakePath;\r
53 \r
54   @BeforeMethod\r
55   @Override\r
56   protected void setUp() throws Exception {\r
57     super.setUp();\r
58 \r
59     m = new Mockery();\r
60     cmd = m.mock(ListPackagesCommand.class);\r
61     desr = m.mock(BuildTriggerDescriptor.class);\r
62     store = m.mock(CustomDataStorage.class);\r
63     params = new TreeMap<String, String>();\r
64     manager = m.mock(NuGetToolManager.class);\r
65 \r
66     checker = new NamedPackagesUpdateChecker(cmd, manager);\r
67     nugetFakePath = Paths.getNuGetRunnerPath();\r
68     final String path = nugetFakePath.getPath();\r
69 \r
70     m.checking(new Expectations(){{\r
71       allowing(desr).getProperties(); will(returnValue(params));\r
72       allowing(manager).getNuGetPath(path); will(returnValue(path));\r
73     }});\r
74 \r
75     params.put(TriggerConstants.NUGET_EXE, path);\r
76     params.put(TriggerConstants.PACKAGE, "NUnit");\r
77   }\r
78 \r
79   @Test\r
80   public void test_check_first_time_should_not_trigger() {\r
81     m.checking(new Expectations(){{\r
82       oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
83       will(returnValue(Arrays.asList(new SourcePackageInfo("src", "pkg", "5.6.87"))));\r
84 \r
85       oneOf(store).getValue("hash"); will(returnValue(null));\r
86       oneOf(store).putValue(with(equal("hash")), with(any(String.class)));\r
87       oneOf(store).flush();\r
88     }});\r
89     Assert.assertNull(checker.checkChanges(desr, store));\r
90 \r
91     m.assertIsSatisfied();\r
92   }\r
93 \r
94   @Test\r
95   public void test_should_trigger_on_new_package() {\r
96     final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";\r
97 \r
98     params.put(TriggerConstants.PACKAGE, "Common");\r
99     params.put(TriggerConstants.SOURCE, source);\r
100     m.checking(new Expectations(){{\r
101       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
102       will(returnValue(Arrays.asList(new SourcePackageInfo(source, "Common", "1.0.0.21"))));\r
103 \r
104       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
105       will(returnValue(Arrays.asList(\r
106               new SourcePackageInfo(source, "Common", "1.0.0.21"),\r
107               new SourcePackageInfo(source, "Common", "2.0.0.22")\r
108               )));\r
109 \r
110       final String hash1 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21";\r
111       final String hash2 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:2.0.0.22";\r
112 \r
113       oneOf(store).getValue("hash"); will(returnValue(null));\r
114       oneOf(store).putValue("hash", hash1);\r
115       oneOf(store).flush();\r
116 \r
117       oneOf(store).getValue("hash"); will(returnValue(hash1));\r
118       oneOf(store).putValue("hash", hash2);\r
119       oneOf(store).flush();\r
120 \r
121     }});\r
122     Assert.assertNull(checker.checkChanges(desr, store));\r
123 \r
124 \r
125     Assert.assertNotNull(checker.checkChanges(desr, store));\r
126 \r
127     m.assertIsSatisfied();\r
128   }\r
129 \r
130   @Test\r
131   public void test_should_trigger_on_new_package2() {\r
132     final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";\r
133 \r
134     params.put(TriggerConstants.PACKAGE, "Common");\r
135     params.put(TriggerConstants.SOURCE, source);\r
136     m.checking(new Expectations(){{\r
137       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
138       will(returnValue(Arrays.asList(new SourcePackageInfo(source, "Common", "1.0.0.21"))));\r
139 \r
140       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
141       will(returnValue(Arrays.asList(\r
142               new SourcePackageInfo(source, "Common", "1.0.0.21"),\r
143               new SourcePackageInfo(source, "Common", "2.0.0.22")\r
144               )));\r
145 \r
146       final String hash1 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21";\r
147       final String hash2 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:2.0.0.22";\r
148 \r
149       oneOf(store).getValue("hash"); will(returnValue(hash1));\r
150       oneOf(store).getValue("hash"); will(returnValue(hash1));\r
151       oneOf(store).putValue("hash", hash2);\r
152       oneOf(store).flush();\r
153 \r
154     }});\r
155     Assert.assertNull(checker.checkChanges(desr, store));\r
156 \r
157 \r
158     Assert.assertNotNull(checker.checkChanges(desr, store));\r
159 \r
160     m.assertIsSatisfied();\r
161   }\r
162 \r
163   @Test\r
164   public void test_should_sort_packages() {\r
165     final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";\r
166 \r
167     params.put(TriggerConstants.PACKAGE, "Common");\r
168     params.put(TriggerConstants.SOURCE, source);\r
169     m.checking(new Expectations(){{\r
170       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
171       will(returnValue(Arrays.asList(\r
172               new SourcePackageInfo(source, "Common", "1.0.0.21"),\r
173               new SourcePackageInfo(null, "Common", "2.0.0.22"),\r
174               new SourcePackageInfo("s2", "C3ommon", "2.0.0.22"),\r
175               new SourcePackageInfo(null, "C3ommon", "2.0.0.22"),\r
176               new SourcePackageInfo(null, "C3o4mmon", "2.0.0.22"),\r
177               new SourcePackageInfo("s4", "C3o3mmon", "2.0.0.22"),\r
178               new SourcePackageInfo(null, "C3omm5on", "2.0.0.22")\r
179               )));\r
180 \r
181       final String hash1 = "|p:Common|v:2.0.0.22|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21|s:s2|p:C3ommon|v:2.0.0.22|s:s4|p:C3o3mmon|v:2.0.0.22|p:C3ommon|v:2.0.0.22|p:C3o4mmon|v:2.0.0.22|p:C3omm5on|v:2.0.0.22";\r
182       oneOf(store).getValue("hash"); will(returnValue("foo"));\r
183       oneOf(store).putValue("hash", hash1);\r
184       oneOf(store).flush();\r
185 \r
186     }});\r
187     Assert.assertNotNull(checker.checkChanges(desr, store));\r
188 \r
189     m.assertIsSatisfied();\r
190   }\r
191 \r
192   @Test\r
193   public void test_check_should_not_trigger_twice() {\r
194     m.checking(new Expectations(){{\r
195       oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
196       will(returnValue(Arrays.asList(new SourcePackageInfo("src", "pkg", "5.6.87"))));\r
197 \r
198       oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
199       will(returnValue(Arrays.asList(new SourcePackageInfo("src", "pkg", "5.6.87"))));\r
200 \r
201       oneOf(store).getValue("hash"); will(returnValue("aaa"));\r
202       oneOf(store).putValue("hash", "|s:src|p:pkg|v:5.6.87");\r
203       oneOf(store).getValue("hash"); will(returnValue("|s:src|p:pkg|v:5.6.87"));\r
204       oneOf(store).flush();\r
205     }});\r
206 \r
207     Assert.assertNotNull(checker.checkChanges(desr, store));\r
208     Assert.assertNull(checker.checkChanges(desr, store));\r
209 \r
210     m.assertIsSatisfied();\r
211   }\r
212 \r
213   @Test\r
214   public void test_check_should_fail_on_error() {\r
215     m.checking(new Expectations() {{\r
216       oneOf(cmd).checkForChanges(with(equal(nugetFakePath)), with(any(String.class)), with(any(String.class)), with(any(String.class)));\r
217       will(throwException(new RuntimeException("Failed to execute command")));\r
218     }});\r
219     try {\r
220       checker.checkChanges(desr, store);\r
221       Assert.fail("should throw an exception");\r
222     } catch (BuildTriggerException e) {\r
223       //NOP\r
224     }\r
225 \r
226     m.assertIsSatisfied();\r
227   }\r
228 }\r