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 tests for JmsPut and JmsGet #237

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added tests for JmsPut and JmsGet
  • Loading branch information
harshitsharma2801 committed May 8, 2024
commit 541dea6bba69ab0c433c5ce04bc7bdba46794eff
4 changes: 3 additions & 1 deletion JMS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ By default these samples will run in client mode. If you do want to run the samp
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 :
The samples also contain unit tests in `src/test`. These tests require connection with a Queue Manager and use the default `env.json` present in the repository, hence make sure you have atleast one valid endpoint in the `env.json`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

atleast - typo.


To run these tests, Use the following command:

````
mvn test
Expand Down
53 changes: 53 additions & 0 deletions JMS/src/test/java/com/ibm/mq/samples/jms/JmsGetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* (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.util.Enumeration;

import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;

import org.junit.jupiter.api.Test;

public class JmsGetTest {
private JMSContext context = null;
private Queue destination = null;
private QueueBrowser browser = null;
private ConnectionHelper ch = null;
@Test
public void testJmsGet(){
JmsPut.main(null);
JmsGet.main(null);

ch = new ConnectionHelper("Basic Get", 0);
context = ch.getContext();
destination = (Queue)ch.getDestination();
browser = context.createBrowser(destination);
try {
Enumeration<?> messages = browser.getEnumeration();
assertFalse(messages.hasMoreElements());
} catch (JMSException jmsex) {
chughts marked this conversation as resolved.
Show resolved Hide resolved
jmsex.printStackTrace();
}
ch.closeContext();;
}

}
131 changes: 131 additions & 0 deletions JMS/src/test/java/com/ibm/mq/samples/jms/JmsPutTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* (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 javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.JMSRuntimeException;

import org.junit.jupiter.api.Test;

import com.ibm.mq.MQException;
import com.ibm.mq.constants.MQConstants;


public class JmsPutTest {
private JMSContext context = null;
private Destination destination = null;
private JMSConsumer consumer = null;
private ConnectionHelper ch = null;
private static long TIMEOUTTIME = 5000;
@Test
public void testJmsPut(){
clearQueue();
chughts marked this conversation as resolved.
Show resolved Hide resolved
JmsPut.main(null);

ch = new ConnectionHelper("Basic Get", 0);
context = ch.getContext();
destination = ch.getDestination();
consumer = context.createConsumer(destination);
for(int i = 1 ; i <= 10 ; i++){
try {
Message recievedMessage = consumer.receive(TIMEOUTTIME);
String value = "This is message number " + i + ".";
String message = getMessageBody(recievedMessage);
assertEquals(value, message);
waitAWhile(1000);
} catch (JMSRuntimeException jmsex) {
jmsex.printStackTrace();
waitAWhile(1000);
}
}
ch.closeContext();
}


@Test
public void testIncorrectQM(){
System.setProperty("QMGR", "INVALID_QM");
JMSRuntimeException jmsex = assertThrows(JMSRuntimeException.class , () -> {
JmsPut.main(null);
});

MQException inException = (MQException) jmsex.getCause();
assertEquals(MQConstants.MQRC_Q_MGR_NAME_ERROR, inException.getReason());
System.clearProperty("QMGR");
}

@Test
public void testIncorrectQueue(){
System.setProperty("QUEUE_NAME" , "INVALID_QUEUE");
JMSRuntimeException jmsex = assertThrows(JMSRuntimeException.class, () -> {
JmsPut.main(null);
});

MQException inException = (MQException) jmsex.getCause();
assertEquals(MQConstants.MQRC_UNKNOWN_OBJECT_NAME, inException.getReason());
System.clearProperty("QUEUE_NAME");
}

@Test
public void testIncorrectChannel(){
System.setProperty("CHANNEL" , "INCORRECT_CHANNEL");
JMSRuntimeException jmsex = assertThrows(JMSRuntimeException.class, () -> {
JmsPut.main(null);
});
MQException inException = (MQException) jmsex.getCause();
assertEquals(MQConstants.MQRC_UNKNOWN_CHANNEL_NAME, inException.getReason());
System.clearProperty("CHANNEL");
}


private void clearQueue(){
BasicConsumer bc = new BasicConsumer(BasicConsumer.CONSUMER_GET, 0);
bc.receive(1000);
bc.close();
}

private static String getMessageBody(Message receivedMessage) {
if (receivedMessage instanceof TextMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
try {
return textMessage.getText();
} catch (JMSException jmsex) {
return "Error getting a message";
}
} else if (receivedMessage instanceof Message) {
return "Message received was not of type TextMessage.";
} else {
return "Received object not of JMS Message type!";
}
}

private static void waitAWhile(int duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}