Skip to content

Commit

Permalink
Removed endpoints mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
harshitsharma2801 committed Apr 17, 2024
1 parent 6d53d25 commit 55f51ea
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 103 deletions.
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, 2024
* (c) Copyright IBM Corporation 2019, 2023
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
150 changes: 48 additions & 102 deletions JMS/src/test/java/com/ibm/mq/samples/jms/SampleEnvSetterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,139 +17,67 @@
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 {

static SampleEnvSetter envSetter;
private static final String ENV_FILE = "EnvFile";
private static final String ENV_LOCATION = "../env.json";

@BeforeAll
public static void setUp() throws NoSuchFieldException, IllegalAccessException{
public static void setUp(){
System.setProperty(ENV_FILE , ENV_LOCATION);
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
// Test for HOST key
String value = envSetter.getEnvValue("HOST", 0);
assertEquals("127.0.0.1", value);
assertNotNull(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");
System.setProperty("APP_USER", "testUser");

// Test for non-existing key but existing Environment variable
String value = envSetter.getEnvValue("APP_USER", 1);
assertEquals("app", value);
assertEquals("testUser", 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);
assertNotNull(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);
value = envSetter.getEnvValueOrDefault("NON_EXISTING_KEY", "test", 1);
assertEquals("test", value);

}

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

//Test for default port given invalid port key
value = envSetter.getPortEnvValue("INVALID_PORT", 0);
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
Expand All @@ -160,16 +88,6 @@ public void testGEBVWithEnv(){
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
Expand All @@ -190,12 +108,40 @@ public void testGetCheckCCDT(){

@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);
int count = envSetter.getCount();
String value = envSetter.getConnectionString();
assertEquals(connString, value);
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);
}
}

}

0 comments on commit 55f51ea

Please sign in to comment.