Skip to content

Commit

Permalink
feat: Change valid characters logic in the application name (#87)
Browse files Browse the repository at this point in the history
* feat: Change valid characters logic in the application name

* test: Fix and add unit tests for application name feature

* Improve regex to validate application name

Co-authored-by: Manik Magar <manik.magar@gmail.com>

* feat: Use the new validateName method to validate application name

* test: Fix unit tests for the new message about invalid application name

---------

Co-authored-by: Manik Magar <manik.magar@gmail.com>
  • Loading branch information
talyssoncastro and manikmagar authored Jan 10, 2024
1 parent 1ee6ade commit 9f57339
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ class ApplicationName {
}

private void runGuards(){
assert (baseAppName != null && !baseAppName.isBlank() && !baseAppName.contains(" ")) : "you should specify an non-empty baseAppName. It shouldn't contain spaces as well"
if (usePrefix == true) assert (prefix != null && !prefix.isBlank() && !prefix.contains(" ")) : "as you going to use a prefix, you should specify a non-empty one. Prefix should not contain spaces"
if (useSuffix == true) assert (suffix != null && !suffix.isBlank() && !suffix.contains(" ")) : "as you going to use a suffix, you should specify a non-empty one. Prefix should not contain spaces"
validateName(baseAppName, "Name")
if (usePrefix == true) validateName(prefix, "Prefix")
if (useSuffix == true) validateName(suffix, "Suffix")

}

private void validateName(String name, String kind) {
boolean valid = (name != null && !name.isBlank() && name ==~ /^[^-][a-zA-Z0-9-]*[^-]$/)
assert valid : kind + " must be alphanumeric with dashes allowed within"
}

private void runNameLengthGuard(String normalizedAppName){
if (normalizedAppName.length() >= MAX_SIZE_APPLICATION_NAME){
throw new Exception("Maximum size of application name is ${MAX_SIZE_APPLICATION_NAME} and the provided name has ${normalizedAppName.length()} characters")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.avioconsulting.mule.deployment.api.models

import com.avioconsulting.mule.deployment.api.models.deployment.ApplicationName
import org.hamcrest.MatcherAssert
import org.junit.jupiter.api.Test

import static groovy.test.GroovyAssert.shouldFail

class ApplicationNameTest {

@Test
void valid_name() {
// arrange

// act
def name = (new ApplicationName('avio-consulting', true, true, 'app-prefix', 'app-suffix')).normalizedAppName
// assert
assert name == 'app-prefix-avio-consulting-app-suffix'
}

@Test
void name_with_spaces() {
// arrange

// act
def exception = shouldFail {
(new ApplicationName('avio consulting', true, true, 'app-prefix', 'app-suffix')).normalizedAppName
}
// assert
MatcherAssert.assertThat('fail', exception.message.contains("Name must be alphanumeric with dashes allowed within"))
}

@Test
void name_starting_with_dash() {
// arrange

// act
def exception = shouldFail {
(new ApplicationName('-avio-consulting', true, true, 'app-prefix', 'app-suffix')).normalizedAppName
}
// assert
MatcherAssert.assertThat('fail', exception.message.contains("Name must be alphanumeric with dashes allowed within"))
}

@Test
void name_ending_with_dash() {
// arrange

// act
def exception = shouldFail {
(new ApplicationName('avio-consulting-', true, true, 'app-prefix', 'app-suffix')).normalizedAppName
}
// assert
MatcherAssert.assertThat('fail', exception.message.contains("Name must be alphanumeric with dashes allowed within"))
}

@Test
void prefix_with_special_char() {
// arrange

// act
def exception = shouldFail {
(new ApplicationName('avio-consulting', true, true, 'app@prefix', 'app-suffix')).normalizedAppName
}
// assert
MatcherAssert.assertThat('fail', exception.message.contains("Prefix must be alphanumeric with dashes allowed within"))
}

@Test
void suffix_with_special_char() {
// arrange

// act
def exception = shouldFail {
(new ApplicationName('avio-consulting', true, true, 'app-prefix', 'app$suffix')).normalizedAppName
}
// assert
MatcherAssert.assertThat('fail', exception.message.contains("Suffix must be alphanumeric with dashes allowed within"))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class CloudhubDeploymentRequestTest implements MavenInvoke {
(new ApplicationName('some app name',true,false,'client',null)).normalizedAppName
}
// assert
MatcherAssert.assertThat('fail', exception.message.contains("you should specify an non-empty baseAppName. It shouldn't contain spaces as well"))
MatcherAssert.assertThat('fail', exception.message.contains("Name must be alphanumeric with dashes allowed within"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class CloudhubV2DeploymentRequestTest implements MavenInvoke {
'f2ea2cb4-c600-4bb5-88e8-e952ff5591ee')
}

MatcherAssert.assertThat('fail', exception.message.contains("you should specify an non-empty baseAppName. It shouldn't contain spaces as well"))
MatcherAssert.assertThat('fail', exception.message.contains("Name must be alphanumeric with dashes allowed within"))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ class OnPremDeploymentRequestTest implements MavenInvoke {
}

// assert
MatcherAssert.assertThat('fail', exception.message.contains("you should specify an non-empty baseAppName. It shouldn't contain spaces as well"))
MatcherAssert.assertThat('fail', exception.message.contains("Name must be alphanumeric with dashes allowed within"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class RuntimeFabricDeploymentRequestTest implements MavenInvoke {
'1.2.3',
'f2ea2cb4-c600-4bb5-88e8-e952ff5591ee')
}
MatcherAssert.assertThat('fail', exception.message.contains("you should specify an non-empty baseAppName. It shouldn't contain spaces as well"))
MatcherAssert.assertThat('fail', exception.message.contains("Name must be alphanumeric with dashes allowed within"))
}

@Test
Expand Down

0 comments on commit 9f57339

Please sign in to comment.