1 | import grails.test.* |
---|
2 | |
---|
3 | /** |
---|
4 | * Unit tests for AppConfigService class. |
---|
5 | */ |
---|
6 | class AppConfigServiceTests extends GrailsUnitTestCase { |
---|
7 | |
---|
8 | // Not automatically injected, assignment is bellow. |
---|
9 | def appConfigService |
---|
10 | |
---|
11 | // Some test data. |
---|
12 | def configName01 = "baseDataCreated" |
---|
13 | def configName02 = "dateFormat" |
---|
14 | def configName03 = "blankValue" |
---|
15 | def blankString = '' |
---|
16 | def configDefaultValue = "true" |
---|
17 | def configValue01 = "false" |
---|
18 | def configValue02= "EEE, dd-MMM-yyyy" |
---|
19 | |
---|
20 | protected void setUp() { |
---|
21 | super.setUp() |
---|
22 | |
---|
23 | // Mock the domain class. |
---|
24 | mockDomain(AppConfig) |
---|
25 | mockForConstraintsTests(AppConfig) |
---|
26 | |
---|
27 | // Mock logging with debug enabled/disabled. |
---|
28 | mockLogging(AppConfigService, true) |
---|
29 | |
---|
30 | // Get an instance of the service. |
---|
31 | appConfigService = new AppConfigService() |
---|
32 | } |
---|
33 | |
---|
34 | protected void tearDown() { |
---|
35 | super.tearDown() |
---|
36 | } |
---|
37 | |
---|
38 | void testSet() { |
---|
39 | |
---|
40 | // Set returns false for blank name. |
---|
41 | assert AppConfig.list().size == 0 |
---|
42 | assertFalse appConfigService.set(blankString) |
---|
43 | assert AppConfig.list().size == 0 |
---|
44 | |
---|
45 | // Set returns true and saves an object. |
---|
46 | assertTrue appConfigService.set(configName01) |
---|
47 | assert AppConfig.list().size == 1 |
---|
48 | |
---|
49 | // Set returns true and does not save a new object for existing appConfig. |
---|
50 | assertTrue appConfigService.set(configName01, configValue01) |
---|
51 | assert AppConfig.list().size == 1 |
---|
52 | |
---|
53 | // Set returns true and saves different objects. |
---|
54 | assertTrue appConfigService.set(configName02, configValue02) |
---|
55 | assert AppConfig.list().size == 2 |
---|
56 | |
---|
57 | // Set returns false when trying to set a blank value. |
---|
58 | assertFalse appConfigService.set(configName03, blankString) |
---|
59 | assert AppConfig.list().size == 2 |
---|
60 | } |
---|
61 | |
---|
62 | void testExists() { |
---|
63 | |
---|
64 | // Exists returns false for non existant appConfig. |
---|
65 | assertFalse appConfigService.exists(configName01) |
---|
66 | |
---|
67 | // Exists returns true for existing appConfig. |
---|
68 | appConfigService.set(configName01) |
---|
69 | assertTrue appConfigService.exists(configName01) |
---|
70 | } |
---|
71 | |
---|
72 | void testGetValue() { |
---|
73 | |
---|
74 | // Get returns false for non existant appConfig. |
---|
75 | assertFalse appConfigService.getValue(configName01) |
---|
76 | |
---|
77 | // Set default and check that it returns. |
---|
78 | appConfigService.set(configName01) |
---|
79 | assert appConfigService.getValue(configName01) == configDefaultValue |
---|
80 | |
---|
81 | // Set a value and check that it returns. |
---|
82 | appConfigService.set(configName01, configValue01) |
---|
83 | assert appConfigService.getValue(configName01) == configValue01 |
---|
84 | } |
---|
85 | |
---|
86 | void testDelete() { |
---|
87 | |
---|
88 | // Deleting returns false for non existant appConfig. |
---|
89 | assert AppConfig.list().size == 0 |
---|
90 | assertFalse appConfigService.delete(configName01) |
---|
91 | |
---|
92 | // First set. |
---|
93 | appConfigService.set(configName01) |
---|
94 | assert AppConfig.list().size == 1 |
---|
95 | |
---|
96 | // Second set. |
---|
97 | appConfigService.set(configName02, configValue02) |
---|
98 | assert AppConfig.list().size == 2 |
---|
99 | |
---|
100 | // Delete first set. |
---|
101 | assertTrue appConfigService.delete(configName01) |
---|
102 | assert AppConfig.list().size == 1 |
---|
103 | |
---|
104 | // Delete second set. |
---|
105 | assertTrue appConfigService.delete(configName02) |
---|
106 | assert AppConfig.list().size == 0 |
---|
107 | } |
---|
108 | |
---|
109 | } |
---|