Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests for SampleEnvSetter. #228

Merged
merged 8 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion JMS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,13 @@ By default these samples will run in client mode. If you do want to run the samp
"BINDINGS": true
````

to the `env.json` file.
to the `env.json` file.

## Unit Test
The samples also contain unit tests in `src/test` , which you can run using maven with the following command :

````
mvn test
````

NOTE : If you are on a windows machine make sure you have the symbolic link between `./src/main/java/com` and `./com` as mentioned above.
8 changes: 4 additions & 4 deletions JMS/com/ibm/mq/samples/jms/SampleEnvSetter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (c) Copyright IBM Corporation 2019, 2023
* (c) Copyright IBM Corporation 2019, 2024
chughts marked this conversation as resolved.
Show resolved Hide resolved
chughts marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,9 +38,9 @@ public class SampleEnvSetter {
private static final String ZOS = "z/os";
private static final int DEFAULT_MQI_PORT = 1414;

private static final String ENV_FILE = "EnvFile";
private static final String DEFAULT_ENV_FILE = "../env.json";
private static final String DEFAULT_Z_ENV_FILE ="../env-zbindings.json";
public static final String ENV_FILE = "EnvFile";
public static final String DEFAULT_ENV_FILE = "../env.json";
public static final String DEFAULT_Z_ENV_FILE ="../env-zbindings.json";

public SampleEnvSetter() {
JSONObject mqEnvSettings = null;
Expand Down
88 changes: 53 additions & 35 deletions JMS/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand All @@ -37,37 +38,37 @@
<profiles>

<profile>
<id>JMS</id>
<id>JMS</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</activation>
<dependencies>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.allclient</artifactId>
<version>9.3.4.1</version>
</dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.allclient</artifactId>
<version>9.3.4.1</version>
</dependency>
</dependencies>
</profile>

<profile>
<id>JAKARTA</id>
<id>JAKARTA</id>

<dependencies>
<dependency>
<groupId>jakarta.jms</groupId>
<artifactId>jakarta.jms-api</artifactId>
<version>3.1.0</version>
<groupId>jakarta.jms</groupId>
<artifactId>jakarta.jms-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.jakarta.client</artifactId>
<version>9.3.4.1</version>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.jakarta.client</artifactId>
<version>9.3.4.1</version>
</dependency>
</dependencies>
</profile>
Expand All @@ -76,21 +77,36 @@

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to
parent pom)-->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<!-- clean lifecycle, see
https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<!-- default lifecycle, jar packaging: see
https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
Expand All @@ -115,7 +131,8 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<!-- site lifecycle, see
https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle-->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
Expand All @@ -141,19 +158,20 @@
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.ibm.mq.samples.jms.BasicSampleDriver</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
Expand All @@ -163,4 +181,4 @@
</plugins>

</build>
</project>
</project>
145 changes: 145 additions & 0 deletions JMS/src/test/java/com/ibm/mq/samples/jms/SampleEnvSetterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* (c) Copyright IBM Corporation 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.ibm.mq.samples.jms;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;

public class SampleEnvSetterTest {
chughts marked this conversation as resolved.
Show resolved Hide resolved

private static SampleEnvSetter envSetter;

@BeforeAll
public static void setUp(){
chughts marked this conversation as resolved.
Show resolved Hide resolved
System.setProperty(SampleEnvSetter.ENV_FILE , SampleEnvSetter.DEFAULT_ENV_FILE);
envSetter = new SampleEnvSetter();
}


@Test
public void testGetEnvValueWithoutEnv() {

// Test for HOST key
String value = envSetter.getEnvValue("HOST", 0);
assertNotNull(value);

// Test with Non-Existing key
value = envSetter.getEnvValue("NON_EXISTING_KEY", 0);
assertNull(value);

}

@Test
public void testGetEnvValueWithEnv() {
System.setProperty("APP_USER", "testUser");

// Test for non-existing key but existing Environment variable
String value = envSetter.getEnvValue("APP_USER", 1);
assertEquals("testUser", value);
}

@Test
public void testGetEnvValueOrDefault() {

// Test for existing key and index with random default value
String value = envSetter.getEnvValueOrDefault("QMGR", "QM3", 0);
assertNotNull(value);

// Test for non-existing key with default value
value = envSetter.getEnvValueOrDefault("NON_EXISTING_KEY", "test", 1);
assertEquals("test", value);

}

@Test
public void testGetPortEnvValue(){
//Test for port key
int value = envSetter.getPortEnvValue("PORT", 0);
assertNotNull(value);

//Test for default port given invalid port key
value = envSetter.getPortEnvValue("INVALID_PORT", 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is INVALID_PORT returning 1414 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function returns the default port if the key is not found.

assertEquals(1414 , value);

}

@Test
public void testGEBVWithEnv(){
System.setProperty("BINDINGS", "true");
//Test for Non existing but existing env key
Boolean value = envSetter.getEnvBooleanValue("BINDINGS", 0);
assertTrue(value);
}

@Test
public void testGetCheckCCDT(){
//MQCCDTURL is not set
String value = envSetter.getCheckForCCDT();
//Test for getCheckForCCDT when MQCCDTURL is not set
assertNull(value);

//MQCCDTURL is set
System.setProperty("MQCCDTURL", "file://../ccdt.json");
value = envSetter.getCheckForCCDT();
//Test for getCheckForCCDT with MQCCDTURL set to correct ccdt file location
assertEquals("file://../ccdt.json", value);
System.setProperty("MQCCDTURL", "file://ccdt.json");
value = envSetter.getCheckForCCDT();
//Test for getCheckForCCDT with MQCCDTURL set to incorrect ccdt file location
assertNull(value);
}

@Test
public void testGetConnectionString(){
int count = envSetter.getCount();
String value = envSetter.getConnectionString();
String[] arrStrings = value.split(",");
assertEquals(count, arrStrings.length);
}

@Test
public void testMissingEnpointValues(){
int count = envSetter.getCount();
//Check for each endpoint for missing essential values
for(int idx = 0 ; idx < count ; idx++){
String value = envSetter.getEnvValue("HOST", idx);
assertNotNull(value);
value = envSetter.getEnvValue("PORT", idx);
assertNotNull(value);
value = envSetter.getEnvValue("CHANNEL", idx);
assertNotNull(value);
value = envSetter.getEnvValue("QMGR", idx);
assertNotNull(value);
value = envSetter.getEnvValue("APP_USER", idx);
assertNotNull(value);
value = envSetter.getEnvValue("APP_PASSWORD", idx);
assertNotNull(value);
value = envSetter.getEnvValue("QUEUE_NAME", idx);
assertNotNull(value);
value = envSetter.getEnvValue("BACKOUT_QUEUE", idx);
assertNotNull(value);
value = envSetter.getEnvValue("MODEL_QUEUE_NAME", idx);
assertNotNull(value);
value = envSetter.getEnvValue("DYNAMIC_QUEUE_PREFIX", idx);
assertNotNull(value);
value = envSetter.getEnvValue("TOPIC_NAME", idx);
assertNotNull(value);
}
}

}