2018-03-19 19:29:41 +02:00
package com . sap . piper . tools
2018-05-03 16:09:36 +02:00
import org.junit.Ignore
2018-03-19 19:29:41 +02:00
import org.junit.BeforeClass
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
2018-06-06 11:19:19 +02:00
import util.BasePiperTest
2018-03-19 19:29:41 +02:00
import util.JenkinsLoggingRule
import util.Rules
import hudson.AbortException
2018-06-06 11:19:19 +02:00
class JavaArchiveDescriptorTest extends BasePiperTest {
2018-03-19 19:29:41 +02:00
private ExpectedException thrown = new ExpectedException ( )
private JenkinsLoggingRule jlr = new JenkinsLoggingRule ( this )
@Rule
public RuleChain rules = Rules . getCommonRules ( this )
. around ( thrown )
. around ( jlr )
2018-04-17 16:53:04 +02:00
private static javaArchive
2018-03-19 19:29:41 +02:00
private static configuration
private script
@BeforeClass
static void init ( ) {
def java = new ToolDescriptor ( 'Java' , 'JAVA_HOME' , '' , '/bin/' , 'java' , '1.8.0' , '-version 2>&1' )
2018-04-17 16:53:04 +02:00
javaArchive = new JavaArchiveDescriptor ( 'SAP Multitarget Application Archive Builder' , 'MTA_JAR_LOCATION' , 'mtaJarLocation' , '1.0.6' , '-v' , java )
2018-03-19 19:29:41 +02:00
}
@Before
void setup ( ) {
helper . registerAllowedMethod ( 'sh' , [ Map ] , { Map m - > getNoEnvVars ( m ) } )
2018-04-17 16:53:04 +02:00
helper . registerAllowedMethod ( 'error' , [ String ] , { s - > throw new hudson . AbortException ( s ) } )
2018-03-19 19:29:41 +02:00
script = loadScript ( 'mtaBuild.groovy' ) . mtaBuild
2018-04-26 17:29:26 +02:00
configuration = [ : ] //no default configuration
2018-03-19 19:29:41 +02:00
}
@Test
2018-04-17 16:53:04 +02:00
void getJavaArchiveFileFromEnvironmentTest ( ) {
2018-03-19 19:29:41 +02:00
helper . registerAllowedMethod ( 'sh' , [ Map ] , { Map m - > getEnvVars ( m ) } )
2018-04-17 16:53:04 +02:00
def javaArchiveFile = javaArchive . getFile ( script , configuration )
2018-03-19 19:29:41 +02:00
2018-06-12 18:32:26 +02:00
assert javaArchiveFile = = '/env/mta/mta_archive_builder-1.1.0.jar'
assert jlr . log . contains ( "SAP Multitarget Application Archive Builder file '/env/mta/mta_archive_builder-1.1.0.jar' retrieved from environment." )
2018-03-19 19:29:41 +02:00
}
@Test
2018-04-17 16:53:04 +02:00
void getJavaArchiveFileFromConfigurationTest ( ) {
2018-03-19 19:29:41 +02:00
2018-06-12 18:32:26 +02:00
configuration = [ mtaJarLocation: '/config/mta/mta_archive_builder-1.1.0.jar' ]
2018-03-19 19:29:41 +02:00
2018-04-17 16:53:04 +02:00
def javaArchiveFile = javaArchive . getFile ( script , configuration )
2018-03-19 19:29:41 +02:00
2018-06-12 18:32:26 +02:00
assert javaArchiveFile = = '/config/mta/mta_archive_builder-1.1.0.jar'
assert jlr . log . contains ( "SAP Multitarget Application Archive Builder file '/config/mta/mta_archive_builder-1.1.0.jar' retrieved from configuration." )
2018-03-19 19:29:41 +02:00
}
2018-05-03 16:09:36 +02:00
// Compatibility tests
@Test
void getJavaArchiveFileFromEnvironment_CompatibilityTest ( ) {
helper . registerAllowedMethod ( 'sh' , [ Map ] , { Map m - > getEnvVarsWithCompatibility ( m ) } )
def javaArchiveFile = javaArchive . getFile ( script , configuration )
assert javaArchiveFile = = '/env/mta/mta.jar'
assert jlr . log . contains ( "SAP Multitarget Application Archive Builder file '/env/mta' retrieved from environment." )
}
@Test
void getJavaArchiveFileFromConfiguration_CompatibilityTest ( ) {
configuration = [ mtaJarLocation: '/config/mta' ]
def javaArchiveFile = javaArchive . getFile ( script , configuration )
assert javaArchiveFile = = '/config/mta/mta.jar'
assert jlr . log . contains ( "SAP Multitarget Application Archive Builder file '/config/mta' retrieved from configuration." )
}
//
2018-04-26 17:29:26 +02:00
@Test
void getJavaArchiveFileFailedTest ( ) {
thrown . expect ( AbortException )
thrown . expectMessage ( "Please, configure SAP Multitarget Application Archive Builder. SAP Multitarget Application Archive Builder can be set using the environment variable 'MTA_JAR_LOCATION', or " +
"using the configuration key 'mtaJarLocation'." )
javaArchive . getFile ( script , configuration )
}
2018-05-03 16:09:36 +02:00
@Ignore ( 'while compatibility code is not removed' )
2018-04-17 16:53:04 +02:00
void getJavaArchiveFileFromEnvironment_UnexpectedFormatTest ( ) {
2018-03-19 19:29:41 +02:00
2018-04-17 16:53:04 +02:00
thrown . expect ( AbortException )
2018-06-12 18:32:26 +02:00
thrown . expectMessage ( "The value '/env/mta/mta_archive_builder-1.1.0.jarr' of the environment variable 'MTA_JAR_LOCATION' has an unexpected format." )
2018-03-19 19:29:41 +02:00
2018-04-17 16:53:04 +02:00
helper . registerAllowedMethod ( 'sh' , [ Map ] , { Map m - > getUnexpectedFormatEnvVars ( m ) } )
2018-03-19 19:29:41 +02:00
2018-04-17 16:53:04 +02:00
javaArchive . getFile ( script , configuration )
2018-03-19 19:29:41 +02:00
}
2018-05-03 16:09:36 +02:00
@Ignore ( 'while compatibility code is not removed' )
2018-04-17 16:53:04 +02:00
void getJavaArchiveFileFromConfiguration_UnexpectedFormatTest ( ) {
2018-03-19 19:29:41 +02:00
2018-04-17 16:53:04 +02:00
thrown . expect ( AbortException )
2018-06-12 18:32:26 +02:00
thrown . expectMessage ( "The value '/config/mta/mta_archive_builder-1.1.0.jarr' of the configuration key 'mtaJarLocation' has an unexpected format." )
2018-03-19 19:29:41 +02:00
2018-06-12 18:32:26 +02:00
configuration = [ mtaJarLocation: '/config/mta/mta_archive_builder-1.1.0.jarr' ]
2018-03-19 19:29:41 +02:00
2018-04-17 16:53:04 +02:00
javaArchive . getFile ( script , configuration )
2018-03-19 19:29:41 +02:00
}
@Test
2018-04-17 16:53:04 +02:00
void getJavaArchiveCallTest ( ) {
2018-03-19 19:29:41 +02:00
2018-06-12 18:32:26 +02:00
configuration = [ mtaJarLocation: '/config/mta/mta_archive_builder-1.1.0.jar' ]
2018-03-19 19:29:41 +02:00
2018-04-17 16:53:04 +02:00
def javaArchiveCall = javaArchive . getCall ( script , configuration )
2018-03-19 19:29:41 +02:00
2018-06-12 18:32:26 +02:00
assert javaArchiveCall = = 'java -jar /config/mta/mta_archive_builder-1.1.0.jar'
assert jlr . log . contains ( "Using SAP Multitarget Application Archive Builder '/config/mta/mta_archive_builder-1.1.0.jar'." )
2018-03-19 19:29:41 +02:00
}
@Test
2018-04-17 16:53:04 +02:00
void verifyJavaArchiveFileTest ( ) {
2018-03-19 19:29:41 +02:00
helper . registerAllowedMethod ( 'sh' , [ Map ] , { Map m - > getEnvVars ( m ) } )
2018-04-17 16:53:04 +02:00
javaArchive . verifyFile ( script , configuration )
2018-03-19 19:29:41 +02:00
2018-06-12 18:32:26 +02:00
assert jlr . log . contains ( "Verifying SAP Multitarget Application Archive Builder '/env/mta/mta_archive_builder-1.1.0.jar'." )
assert jlr . log . contains ( "Verification success. SAP Multitarget Application Archive Builder '/env/mta/mta_archive_builder-1.1.0.jar' exists." )
2018-03-19 19:29:41 +02:00
}
@Test
2018-04-17 16:53:04 +02:00
void verifyJavaArchiveVersionTest ( ) {
2018-06-12 18:32:26 +02:00
configuration = [ mtaJarLocation: 'mta_archive_builder-1.1.0.jar' ]
2018-03-19 19:29:41 +02:00
helper . registerAllowedMethod ( 'sh' , [ Map ] , { Map m - > getVersion ( m ) } )
2018-04-17 16:53:04 +02:00
javaArchive . verifyVersion ( script , configuration )
2018-03-19 19:29:41 +02:00
2018-04-17 16:53:04 +02:00
assert jlr . log . contains ( "Verifying SAP Multitarget Application Archive Builder version 1.0.6 or compatible version." )
assert jlr . log . contains ( "Verification success. SAP Multitarget Application Archive Builder version 1.0.6 is installed." )
2018-03-19 19:29:41 +02:00
}
@Test
2018-04-17 16:53:04 +02:00
void verifyJavaArchiveVersion_FailedTest ( ) {
2018-06-12 18:32:26 +02:00
configuration = [ mtaJarLocation: 'mta_archive_builder-1.1.0.jar' ]
2018-03-19 19:29:41 +02:00
thrown . expect ( AbortException )
2018-06-12 18:32:26 +02:00
thrown . expectMessage ( "The verification of SAP Multitarget Application Archive Builder failed. Please check 'java -jar mta_archive_builder-1.1.0.jar'. script returned exit code 127." )
2018-03-19 19:29:41 +02:00
helper . registerAllowedMethod ( 'sh' , [ Map ] , { Map m - > getVersionFailed ( m ) } )
2018-04-17 16:53:04 +02:00
javaArchive . verifyVersion ( script , configuration )
2018-03-19 19:29:41 +02:00
}
@Test
2018-04-17 16:53:04 +02:00
void verifyJavaArchiveVersion_IncompatibleVersionTest ( ) {
2018-06-12 18:32:26 +02:00
configuration = [ mtaJarLocation: '/config/mta/mta_archive_builder-1.1.0.jar' ]
2018-03-19 19:29:41 +02:00
thrown . expect ( AbortException )
2018-04-17 16:53:04 +02:00
thrown . expectMessage ( "The installed version of SAP Multitarget Application Archive Builder is 1.0.5. Please install version 1.0.6 or a compatible version." )
2018-03-19 19:29:41 +02:00
helper . registerAllowedMethod ( 'sh' , [ Map ] , { Map m - > getIncompatibleVersion ( m ) } )
2018-04-17 16:53:04 +02:00
javaArchive . verifyVersion ( script , configuration )
2018-03-19 19:29:41 +02:00
}
private getEnvVars ( Map m ) {
if ( m . script . contains ( 'JAVA_HOME' ) ) {
return '/env/java'
} else if ( m . script . contains ( 'MTA_JAR_LOCATION' ) ) {
2018-06-12 18:32:26 +02:00
return '/env/mta/mta_archive_builder-1.1.0.jar'
2018-04-17 16:53:04 +02:00
} else {
return 0
}
}
2018-05-03 16:09:36 +02:00
private getEnvVarsWithCompatibility ( Map m ) {
if ( m . script . contains ( 'JAVA_HOME' ) ) {
return '/env/java'
} else if ( m . script . contains ( 'MTA_JAR_LOCATION' ) ) {
return '/env/mta'
} else {
return 0
}
}
/ *
2018-04-17 16:53:04 +02:00
private getUnexpectedFormatEnvVars ( Map m ) {
if ( m . script . contains ( 'JAVA_HOME' ) ) {
return '/env/java'
} else if ( m . script . contains ( 'MTA_JAR_LOCATION' ) ) {
2018-06-12 18:32:26 +02:00
return '/env/mta/mta_archive_builder-1.1.0.jarr'
2018-03-19 19:29:41 +02:00
} else {
return 0
}
}
2018-05-03 16:09:36 +02:00
* /
2018-03-19 19:29:41 +02:00
private getNoEnvVars ( Map m ) {
if ( m . script . contains ( 'JAVA_HOME' ) ) {
return ''
} else if ( m . script . contains ( 'MTA_JAR_LOCATION' ) ) {
return ''
2018-03-20 16:14:02 +02:00
} else if ( m . script . contains ( 'which java' ) ) {
2018-04-10 18:23:44 +02:00
return 0
2018-03-19 19:29:41 +02:00
} else {
return 0
}
}
private getVersion ( Map m ) {
if ( m . script . contains ( 'java -version' ) ) {
return '' ' openjdk version \ "1.8.0_121\"
OpenJDK Runtime Environment ( build 1.8 . 0 _121 - 8 u121 - b13 - 1 ~ bpo8 + 1 - b13 )
OpenJDK 64 - Bit Server VM ( build 25.121 - b13 , mixed mode ) '' '
2018-06-12 18:32:26 +02:00
} else if ( m . script . contains ( 'mta_archive_builder-1.1.0.jar -v' ) ) {
2018-03-19 19:29:41 +02:00
return '1.0.6'
} else {
2018-03-20 16:14:02 +02:00
return getNoEnvVars ( m )
2018-03-19 19:29:41 +02:00
}
}
private getVersionFailed ( Map m ) {
2018-06-12 18:32:26 +02:00
if ( m . script . contains ( 'java -version' ) | | m . script . contains ( 'mta_archive_builder-1.1.0.jar -v' ) ) {
2018-03-19 19:29:41 +02:00
throw new AbortException ( 'script returned exit code 127' )
} else {
2018-03-20 16:14:02 +02:00
return getNoEnvVars ( m )
2018-03-19 19:29:41 +02:00
}
}
private getIncompatibleVersion ( Map m ) {
2018-06-12 18:32:26 +02:00
if ( m . script . contains ( 'java -version' ) | | m . script . contains ( 'mta_archive_builder-1.1.0.jar -v' ) ) {
2018-03-19 19:29:41 +02:00
return '1.0.5'
} else {
2018-03-20 16:14:02 +02:00
return getNoEnvVars ( m )
2018-03-19 19:29:41 +02:00
}
}
}