### Question:
ExchangeRegistry { boolean deleteExchange(String exchangeName, boolean ifUnused) throws BrokerException, ValidationException { Exchange exchange = exchangeMap.get(exchangeName); if (Objects.isNull(exchange)) { return false; } if (!isBuiltInExchange(exchange)) { if (!ifUnused || exchange.isUnused()) { if (exchange.isDurable()) { exchangeDao.delete(exchange); } exchangeMap.remove(exchangeName); return true; } else { throw new ValidationException("Cannot delete exchange. Exchange " + exchangeName + " has bindings."); } } else { throw new ValidationException("Cannot delete built in exchange '" + exchangeName + "'"); } } ExchangeRegistry(ExchangeDao exchangeDao, BindingDao bindingDao); void createExchange(String exchangeName, Exchange.Type type, boolean durable); Exchange getDefaultExchange(); void retrieveFromStore(QueueRegistry queueRegistry); Collection<Exchange> getAllExchanges(); static final String DEFAULT_DEAD_LETTER_EXCHANGE; }### Answer:
@Test(description = "Test exchange delete operation") public void testDeleteExchange() throws BrokerException, ValidationException { exchangeRegistry.declareExchange(NON_EXISTING_EXCHANGE, DIRECT.toString(), false, false); exchangeRegistry.deleteExchange(NON_EXISTING_EXCHANGE, true); Exchange deletedExchange = exchangeRegistry.getExchange(NON_EXISTING_EXCHANGE); Assert.assertNull(deletedExchange, "deleted exchange cannot exist"); }
@Test(dataProvider = "exchangeNames", description = "try to delete built in exchanges", expectedExceptions = ValidationException.class) public void testDeleteBuiltInExchanges(String exchangeName) throws BrokerException, ValidationException { exchangeRegistry.deleteExchange(exchangeName, true); Assert.fail("built in exchange type - " + exchangeName + " shouldn't be allowed to delete"); }