41b2454eb4b986e827cb3f3ecc19d7acadeab91e
[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.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 SourcePackageInfo("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     m.assertIsSatisfied();\r
87   }\r
88 \r
89   @Test\r
90   public void test_should_trigger_on_new_package() {\r
91     final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";\r
92 \r
93     params.put(TriggerConstants.PACKAGE, "Common");\r
94     params.put(TriggerConstants.SOURCE, source);\r
95     m.checking(new Expectations(){{\r
96       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
97       will(returnValue(Arrays.asList(new SourcePackageInfo(source, "Common", "1.0.0.21"))));\r
98 \r
99       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
100       will(returnValue(Arrays.asList(\r
101               new SourcePackageInfo(source, "Common", "1.0.0.21"),\r
102               new SourcePackageInfo(source, "Common", "2.0.0.22")\r
103               )));\r
104 \r
105       final String hash1 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21";\r
106       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
107 \r
108       oneOf(store).getValue("hash"); will(returnValue(null));\r
109       oneOf(store).putValue("hash", hash1);\r
110       oneOf(store).flush();\r
111 \r
112       oneOf(store).getValue("hash"); will(returnValue(hash1));\r
113       oneOf(store).putValue("hash", hash2);\r
114       oneOf(store).flush();\r
115 \r
116     }});\r
117     Assert.assertNull(checker.checkChanges(desr, store));\r
118 \r
119 \r
120     Assert.assertNotNull(checker.checkChanges(desr, store));\r
121 \r
122     m.assertIsSatisfied();\r
123   }\r
124 \r
125   @Test\r
126   public void test_should_trigger_on_new_package2() {\r
127     final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";\r
128 \r
129     params.put(TriggerConstants.PACKAGE, "Common");\r
130     params.put(TriggerConstants.SOURCE, source);\r
131     m.checking(new Expectations(){{\r
132       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
133       will(returnValue(Arrays.asList(new SourcePackageInfo(source, "Common", "1.0.0.21"))));\r
134 \r
135       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
136       will(returnValue(Arrays.asList(\r
137               new SourcePackageInfo(source, "Common", "1.0.0.21"),\r
138               new SourcePackageInfo(source, "Common", "2.0.0.22")\r
139               )));\r
140 \r
141       final String hash1 = "|s:\\\\ServerNameRemoved\\NugetTest\\Repository|p:Common|v:1.0.0.21";\r
142       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
143 \r
144       oneOf(store).getValue("hash"); will(returnValue(hash1));\r
145       oneOf(store).getValue("hash"); will(returnValue(hash1));\r
146       oneOf(store).putValue("hash", hash2);\r
147       oneOf(store).flush();\r
148 \r
149     }});\r
150     Assert.assertNull(checker.checkChanges(desr, store));\r
151 \r
152 \r
153     Assert.assertNotNull(checker.checkChanges(desr, store));\r
154 \r
155     m.assertIsSatisfied();\r
156   }\r
157 \r
158   @Test\r
159   public void test_should_sort_packages() {\r
160     final String source = "\\\\ServerNameRemoved\\NugetTest\\Repository";\r
161 \r
162     params.put(TriggerConstants.PACKAGE, "Common");\r
163     params.put(TriggerConstants.SOURCE, source);\r
164     m.checking(new Expectations(){{\r
165       oneOf(cmd).checkForChanges(nugetFakePath, source, "Common", null);\r
166       will(returnValue(Arrays.asList(\r
167               new SourcePackageInfo(source, "Common", "1.0.0.21"),\r
168               new SourcePackageInfo(null, "Common", "2.0.0.22"),\r
169               new SourcePackageInfo("s2", "C3ommon", "2.0.0.22"),\r
170               new SourcePackageInfo(null, "C3ommon", "2.0.0.22"),\r
171               new SourcePackageInfo(null, "C3o4mmon", "2.0.0.22"),\r
172               new SourcePackageInfo("s4", "C3o3mmon", "2.0.0.22"),\r
173               new SourcePackageInfo(null, "C3omm5on", "2.0.0.22")\r
174               )));\r
175 \r
176       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
177       oneOf(store).getValue("hash"); will(returnValue("foo"));\r
178       oneOf(store).putValue("hash", hash1);\r
179       oneOf(store).flush();\r
180 \r
181     }});\r
182     Assert.assertNotNull(checker.checkChanges(desr, store));\r
183 \r
184     m.assertIsSatisfied();\r
185   }\r
186 \r
187   @Test\r
188   public void test_check_should_not_trigger_twice() {\r
189     m.checking(new Expectations(){{\r
190       oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
191       will(returnValue(Arrays.asList(new SourcePackageInfo("src", "pkg", "5.6.87"))));\r
192 \r
193       oneOf(cmd).checkForChanges(nugetFakePath, null, "NUnit", null);\r
194       will(returnValue(Arrays.asList(new SourcePackageInfo("src", "pkg", "5.6.87"))));\r
195 \r
196       oneOf(store).getValue("hash"); will(returnValue("aaa"));\r
197       oneOf(store).putValue("hash", "|s:src|p:pkg|v:5.6.87");\r
198       oneOf(store).getValue("hash"); will(returnValue("|s:src|p:pkg|v:5.6.87"));\r
199       oneOf(store).flush();\r
200     }});\r
201 \r
202     Assert.assertNotNull(checker.checkChanges(desr, store));\r
203     Assert.assertNull(checker.checkChanges(desr, store));\r
204 \r
205     m.assertIsSatisfied();\r
206   }\r
207 \r
208   @Test\r
209   public void test_check_should_fail_on_error() {\r
210     m.checking(new Expectations() {{\r
211       oneOf(cmd).checkForChanges(with(equal(nugetFakePath)), with(any(String.class)), with(any(String.class)), with(any(String.class)));\r
212       will(throwException(new RuntimeException("Failed to execute command")));\r
213     }});\r
214     try {\r
215       checker.checkChanges(desr, store);\r
216       Assert.fail("should throw an exception");\r
217     } catch (BuildTriggerException e) {\r
218       //NOP\r
219     }\r
220 \r
221     m.assertIsSatisfied();\r
222   }\r
223 }\r