Skip to content

Commit a4ebdbb

Browse files
committed
Initial commit of Guid.cls & Guid_Tests.cls
0 parents  commit a4ebdbb

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Jonathan Gillespie
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

src/classes/Guid.cls

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public without sharing class Guid {
2+
public static String createGuid() {
3+
Blob generatedBlob = Crypto.GenerateAESKey(128);
4+
String hex = EncodingUtil.ConvertTohex(generatedBlob);
5+
String guid = hex.substring(0, 8)
6+
+ '-' + hex.substring(8, 12)
7+
+ '-' + hex.substring(12, 16)
8+
+ '-' + hex.substring(16, 20)
9+
+ '-' + hex.substring(20);
10+
11+
return guid.toUpperCase();
12+
}
13+
14+
public static boolean isEmpty(String guid) {
15+
return String.isBlank(guid) || guid == '00000000-0000-0000-0000-000000000000';
16+
}
17+
18+
public static Boolean isGuid(String guid) {
19+
if(String.isBlank(guid)) return false;
20+
21+
String guidRegEx = '[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}';
22+
Pattern guidPattern = Pattern.compile(guidRegEx);
23+
Matcher guidMatcher = guidPattern.matcher(guid.toUpperCase());
24+
Boolean result = guidMatcher.matches();
25+
return result;
26+
}
27+
}

src/classes/Guid.cls-meta.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>38.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

src/classes/Guid_Tests.cls

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@isTest
2+
private class Guid_Tests {
3+
4+
@isTest
5+
static void it_should_consider_all_zeroes_string_to_be_empty() {
6+
System.assert(Guid.isEmpty('00000000-0000-0000-0000-000000000000'));
7+
}
8+
9+
@isTest
10+
static void it_should_create_a_new_guid() {
11+
String generatedGuid = Guid.createGuid();
12+
System.assertEquals(36, generatedGuid.length());
13+
}
14+
15+
@isTest
16+
static void it_should_verify_that_a_guid_is_a_guid() {
17+
String generatedGuid = Guid.createGuid();
18+
System.assert(Guid.isGuid(generatedGuid));
19+
}
20+
21+
@isTest
22+
static void it_should_consider_null_string_an_empty_guid() {
23+
System.assert(Guid.isEmpty(null));
24+
}
25+
26+
@isTest
27+
static void it_should_not_consider_an_empty_string_a_guid() {
28+
System.assertEquals(false, Guid.isGuid(''));
29+
}
30+
31+
@isTest
32+
static void it_should_not_consider_null_a_guid() {
33+
System.assertEquals(false, Guid.isGuid(null));
34+
}
35+
36+
@isTest
37+
static void it_should_validate_a_guid_in_upper_case() {
38+
String exampleGuid = 'f3665813-1a60-4924-ad9b-23a9cef17d80'.toUpperCase();
39+
System.assertEquals(true, Guid.isGuid(exampleGuid));
40+
}
41+
42+
@isTest
43+
static void it_should_validate_a_guid_in_lower_case() {
44+
String exampleGuid = 'f3665813-1a60-4924-ad9b-23a9cef17d80'.toLowerCase();
45+
System.assertEquals(true, Guid.isGuid(exampleGuid));
46+
}
47+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>38.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

0 commit comments

Comments
 (0)