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 4 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
9 changes: 8 additions & 1 deletion JMS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,11 @@ 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
````
2 changes: 1 addition & 1 deletion 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
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>
201 changes: 201 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,201 @@
/*
* (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 java.lang.reflect.Field;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

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

static SampleEnvSetter envSetter;

@BeforeAll
public static void setUp() throws NoSuchFieldException, IllegalAccessException{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we should be mocking the endpoints. The point of the test is to verify:

  1. That the env setter is able to read the file, whose name and location are specified by an environment variable.
  2. That it allows overrides via environment variables.
  3. How it behaves when there are missing values in the env file.
  4. How it behaves when the file can't be found.

envSetter = new SampleEnvSetter();

//Mock endpoints
JSONObject endpoint1 = new JSONObject();
endpoint1.put("HOST", "127.0.0.1");
endpoint1.put("PORT", "1415");
endpoint1.put("CHANNEL", "DEV.APP.SVRCONN");
endpoint1.put("QMGR", "QM1");
endpoint1.put("APP_USER", "app");
endpoint1.put("APP_PASSWORD", "passw0rd");
endpoint1.put("QUEUE_NAME", "DEV.QUEUE.1");
endpoint1.put("BACKOUT_QUEUE", "DEV.QUEUE.2");
endpoint1.put("MODEL_QUEUE_NAME", "DEV.APP.MODEL.QUEUE");
endpoint1.put("DYNAMIC_QUEUE_PREFIX", "APP.REPLIES.*");
endpoint1.put("TOPIC_NAME", "dev/");
endpoint1.put("BINDINGS", false);
endpoint1.put("REQUEST_MESSAGE_EXPIRY", 1000);

JSONObject endpoint2 = new JSONObject();
endpoint2.put("HOST", "127.0.0.2");
endpoint2.put("PORT", "1414");
endpoint2.put("CHANNEL", "");
endpoint2.put("QMGR", "*QMGroupA");
// endpoint2.put("APP_USER", "app");
endpoint2.put("APP_PASSWORD", "passw0rd");
endpoint2.put("QUEUE_NAME", "DEV.QUEUE.1");
endpoint2.put("BACKOUT_QUEUE", "DEV.QUEUE.2");
endpoint2.put("MODEL_QUEUE_NAME", "DEV.APP.MODEL.QUEUE");
endpoint2.put("DYNAMIC_QUEUE_PREFIX", "APP.REPLIES.*");
endpoint2.put("TOPIC_NAME", "dev/");
endpoint2.put("BINDINGS", true);
JSONArray mockEndPoints = new JSONArray();
mockEndPoints.put(endpoint1);
mockEndPoints.put(endpoint2);

Field field = envSetter.getClass().getDeclaredField("mqEndPoints");
field.setAccessible(true);
field.set(envSetter, mockEndPoints);
}
@Test
public void testEnvFile(){
SampleEnvSetter envSetter2 = new SampleEnvSetter();
String value = envSetter2.getEnvValue("HOST", 0);
assertNotNull(value);
}

@Test
public void testGetEnvValueWithoutEnv() {

// Test with existing key and Index
String value = envSetter.getEnvValue("HOST", 0);
assertEquals("127.0.0.1", value);

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

// Test with existing key but Index out of bound
value = envSetter.getEnvValue("HOST", 2);
assertNull(value);
}

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

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

@Test
public void testGetEnvValueOrDefault() {

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

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

// Test for existing key with empty value
value = envSetter.getEnvValueOrDefault("CHANNEL", "DEV.APP.SVRCONN", 1);
assertEquals("DEV.APP.SVRCONN", value);

}

@Test
public void testGetPortEnvValue(){
//Test for correct port key
int value = envSetter.getPortEnvValue("PORT", 0);
assertEquals(1415 , 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 for default port
// value = envSetter.getPortEnvValue("PORT", 1);
// assertEquals(1414 , value);
}

@Test
public void testGetEnvBooleanValue(){
//Test with existing Key with true value
Boolean value = envSetter.getEnvBooleanValue("BINDINGS", 1);
assertTrue(value);

//Test with existing Key with false value
value = envSetter.getEnvBooleanValue("BINDINGS", 0);
assertFalse(value);

//Test with non-boolean key
value = envSetter.getEnvBooleanValue("APP_USER", 1);
assertFalse(value);
//Test with Index out of bound
value = envSetter.getEnvBooleanValue("BINDINGS", 2);
assertFalse(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 testGetEnvLongValue(){
//Test for Existing long key
Long value = envSetter.getEnvLongValue("REQUEST_MESSAGE_EXPIRY", 0);
assertEquals(Long.valueOf(1000), value);
//Test for key that is not long type
value = envSetter.getEnvLongValue("APP_USER", 0);
assertEquals(Long.valueOf(0), 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(){
List<String> coll = new ArrayList<String>();
coll.add("127.0.0.1(1415)");
coll.add("127.0.0.2(1414)");
//Mock connection string
String connString = String.join(",", coll);
String value = envSetter.getConnectionString();
assertEquals(connString, value);
}
}