1717package com .ctrip .framework .apollo .biz .service ;
1818
1919import com .ctrip .framework .apollo .biz .AbstractIntegrationTest ;
20+ import com .ctrip .framework .apollo .biz .config .BizConfig ;
2021import com .ctrip .framework .apollo .biz .entity .Cluster ;
2122import com .ctrip .framework .apollo .biz .entity .Commit ;
2223import com .ctrip .framework .apollo .biz .entity .InstanceConfig ;
2526import com .ctrip .framework .apollo .biz .entity .Release ;
2627import com .ctrip .framework .apollo .biz .entity .ReleaseHistory ;
2728import com .ctrip .framework .apollo .biz .repository .InstanceConfigRepository ;
29+ import com .ctrip .framework .apollo .biz .repository .NamespaceRepository ;
2830import com .ctrip .framework .apollo .common .entity .AppNamespace ;
2931
32+ import com .ctrip .framework .apollo .common .exception .ServiceException ;
3033import java .text .ParseException ;
3134import java .text .SimpleDateFormat ;
35+ import java .util .Arrays ;
3236import java .util .Date ;
37+ import java .util .HashSet ;
38+ import org .junit .Assert ;
3339import org .junit .Test ;
3440import org .springframework .beans .factory .annotation .Autowired ;
41+ import org .springframework .boot .test .mock .mockito .MockBean ;
3542import org .springframework .data .domain .Page ;
3643import org .springframework .data .domain .PageRequest ;
3744import org .springframework .test .context .jdbc .Sql ;
3845
3946import java .util .List ;
47+ import org .springframework .test .util .ReflectionTestUtils ;
4048
4149import static org .junit .Assert .assertEquals ;
4250import static org .junit .Assert .assertNotNull ;
4351import static org .junit .Assert .assertNull ;
4452import static org .junit .Assert .assertTrue ;
53+ import static org .mockito .Mockito .when ;
4554
4655public class NamespaceServiceIntegrationTest extends AbstractIntegrationTest {
4756
@@ -62,6 +71,11 @@ public class NamespaceServiceIntegrationTest extends AbstractIntegrationTest {
6271 private ReleaseHistoryService releaseHistoryService ;
6372 @ Autowired
6473 private InstanceConfigRepository instanceConfigRepository ;
74+ @ Autowired
75+ private NamespaceRepository namespaceRepository ;
76+
77+ @ MockBean
78+ private BizConfig bizConfig ;
6579
6680 private String testApp = "testApp" ;
6781 private String testCluster = "default" ;
@@ -134,4 +148,85 @@ public void testGetCommitsByModifiedTime() throws ParseException {
134148 }
135149
136150
151+ @ Test
152+ @ Sql (scripts = "/sql/namespace-test.sql" , executionPhase = Sql .ExecutionPhase .BEFORE_TEST_METHOD )
153+ @ Sql (scripts = "/sql/clean.sql" , executionPhase = Sql .ExecutionPhase .AFTER_TEST_METHOD )
154+ public void testNamespaceNumLimit () {
155+
156+ when (bizConfig .isNamespaceNumLimitEnabled ()).thenReturn (true );
157+ when (bizConfig .namespaceNumLimit ()).thenReturn (2 );
158+
159+ Namespace namespace = new Namespace ();
160+ namespace .setAppId (testApp );
161+ namespace .setClusterName (testCluster );
162+ namespace .setNamespaceName ("demo-namespace" );
163+ namespaceService .save (namespace );
164+
165+ try {
166+ Namespace namespace2 = new Namespace ();
167+ namespace2 .setAppId (testApp );
168+ namespace2 .setClusterName (testCluster );
169+ namespace2 .setNamespaceName ("demo-namespace2" );
170+ namespaceService .save (namespace2 );
171+
172+ Assert .fail ();
173+ } catch (Exception e ) {
174+ Assert .assertTrue (e instanceof ServiceException );
175+ }
176+
177+ int nowCount = namespaceRepository .countByAppIdAndClusterName (testApp , testCluster );
178+ Assert .assertEquals (2 , nowCount );
179+
180+ }
181+
182+ @ Test
183+ @ Sql (scripts = "/sql/namespace-test.sql" , executionPhase = Sql .ExecutionPhase .BEFORE_TEST_METHOD )
184+ @ Sql (scripts = "/sql/clean.sql" , executionPhase = Sql .ExecutionPhase .AFTER_TEST_METHOD )
185+ public void testNamespaceNumLimitFalse () {
186+
187+ when (bizConfig .namespaceNumLimit ()).thenReturn (2 );
188+
189+ Namespace namespace = new Namespace ();
190+ namespace .setAppId (testApp );
191+ namespace .setClusterName (testCluster );
192+ namespace .setNamespaceName ("demo-namespace" );
193+ namespaceService .save (namespace );
194+
195+ Namespace namespace2 = new Namespace ();
196+ namespace2 .setAppId (testApp );
197+ namespace2 .setClusterName (testCluster );
198+ namespace2 .setNamespaceName ("demo-namespace2" );
199+ namespaceService .save (namespace2 );
200+
201+ int nowCount = namespaceRepository .countByAppIdAndClusterName (testApp , testCluster );
202+ Assert .assertEquals (3 , nowCount );
203+
204+ }
205+
206+ @ Test
207+ @ Sql (scripts = "/sql/namespace-test.sql" , executionPhase = Sql .ExecutionPhase .BEFORE_TEST_METHOD )
208+ @ Sql (scripts = "/sql/clean.sql" , executionPhase = Sql .ExecutionPhase .AFTER_TEST_METHOD )
209+ public void testNamespaceNumLimitWhite () {
210+
211+ when (bizConfig .isNamespaceNumLimitEnabled ()).thenReturn (true );
212+ when (bizConfig .namespaceNumLimit ()).thenReturn (2 );
213+ when (bizConfig .namespaceNumLimitWhite ()).thenReturn (new HashSet <>(Arrays .asList (testApp )));
214+
215+ Namespace namespace = new Namespace ();
216+ namespace .setAppId (testApp );
217+ namespace .setClusterName (testCluster );
218+ namespace .setNamespaceName ("demo-namespace" );
219+ namespaceService .save (namespace );
220+
221+ Namespace namespace2 = new Namespace ();
222+ namespace2 .setAppId (testApp );
223+ namespace2 .setClusterName (testCluster );
224+ namespace2 .setNamespaceName ("demo-namespace2" );
225+ namespaceService .save (namespace2 );
226+
227+ int nowCount = namespaceRepository .countByAppIdAndClusterName (testApp , testCluster );
228+ Assert .assertEquals (3 , nowCount );
229+
230+ }
231+
137232}
0 commit comments