Code example for ByteArrayOutputStream

Methods: reset, sleep, toString

0
	@Test 
	public void testBadConfirmRequest() { 
 
		CNSControllerServlet cns = new CNSControllerServlet();
		CQSControllerServlet cqs = new CQSControllerServlet();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		String topicArn = null;
		String queueURL = null;
 
		try { 
			 
			String topicName = "T" + rand.nextLong();
			CNSTestingUtils.addTopic(cns, user1, out, topicName);
			String res = out.toString();
			topicArn = CNSTestingUtils.getArnFromString(res);
			out.reset();
			 
			CNSTestingUtils.addPermission(cns, user1, out, topicArn, "*", "*", rand.nextLong()+"");
			out.reset();
 
			String token = "faketoken";
 
			CNSTestingUtils.confirmSubscription(cns, user1, out, topicArn, token, null);
			assertTrue(CNSTestingUtils.verifyErrorResponse(out.toString(), "NotFound", "Resource not found."));				
			out.reset();
 
			CNSTestingUtils.confirmSubscription(cns, user1, out, topicArn, null, null);
			assertTrue(CNSTestingUtils.verifyErrorResponse(out.toString(), "InvalidParameter", "request parameter does not comply with the associated constraints."));				
			out.reset();
			 
			String cqsResponse = CNSTestingUtils.addQueue(cqs, user2, "Q" + rand.nextLong());
			ICQSQueuePersistence queueHandler = PersistenceFactory.getQueuePersistence();	
			queueURL = CNSTestingUtils.getQueueUrl(cqsResponse);					
			queueHandler.getQueue(queueURL);
 
			String endpoint = queueURL;
			String protocol = "cqs";
			CNSTestingUtils.setPermissionForUser(cqs, user2, user1, queueURL);
 
			CNSTestingUtils.subscribe(cns, user2, out, endpoint, protocol, topicArn);
			CNSTestingUtils.getSubscriptionArnFromString(out.toString());
			out.reset();
			Thread.sleep(1000);
 
			String msg = CNSTestingUtils.receiveMessage(cqs, user2, queueURL, "1");
			String receiptHandle = CNSTestingUtils.getReceiptHandle(msg);
			String message = CNSTestingUtils.getMessage(msg);
			assertTrue("Failed to receive message", message != null);
			JSONObject js = new JSONObject(message);
 
			token = js.getString("Token");
			assertTrue(token != null);
			assertTrue(receiptHandle != null);
			out.reset();
 
			CNSTestingUtils.confirmSubscription(cns, user1, out, "fakeARN", token, null);
			assertTrue("Missing invalid parameter error", CNSTestingUtils.verifyErrorResponse(out.toString(), "InvalidParameter", "request parameter does not comply with the associated constraints."));				
			out.reset();
 
			CNSTestingUtils.confirmSubscription(cns, user1, out, topicArn, token, "blech");
			assertTrue("Missing invalid parameter error", CNSTestingUtils.verifyErrorResponse(out.toString(), "InvalidParameter", "request parameter does not comply with the associated constraints."));				
			out.reset();
 
		} catch (Exception ex) {
			 
			fail(ex.toString());
 
		} finally { 
		 
			if (topicArn != null) {
				try { 
					CNSTestingUtils.deleteTopic(cns, user1, out, topicArn);
				} catch (Exception e) { }
			} 
 
			if (queueURL != null) {
				try {