use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.
the class MockAthenzPulsarClient method getPulsarClient.
protected PulsarClientImpl getPulsarClient(String serviceUrl, ClientConfigurationData config) throws PulsarClientException {
try {
CompletableFuture asyncProducerResult = null;
if (serviceUrl == null || serviceUrl.isEmpty()) {
asyncProducerResult = FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException("Producer configuration undefined"));
}
PulsarClientImpl pulsarClient = Mockito.mock(PulsarClientImpl.class);
Producer producer = Mockito.mock(Producer.class);
Consumer consumer = Mockito.mock(Consumer.class);
MessageMetadata meta = new MessageMetadata();
DomainChangeMessage roleChange = new DomainChangeMessage().setDomainName("domain").setObjectType(DomainChangeMessage.ObjectType.ROLE).setApiName("putRole").setObjectName("role1");
MessageImpl<byte[]> msg = MessageImpl.create(meta, ByteBuffer.wrap(new ObjectMapper().writeValueAsBytes(roleChange)), Schema.BYTES, null);
Mockito.when(consumer.receive(1, TimeUnit.SECONDS)).thenReturn(msg);
CompletableFuture finalAsyncProducerResult = asyncProducerResult;
Mockito.when(pulsarClient.createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class))).thenAnswer(invocation -> {
if (serviceUrl != null) {
return ((ProducerConfigurationData) invocation.getArgument(0)).getTopicName() == null ? finalAsyncProducerResult : CompletableFuture.completedFuture(producer);
}
return finalAsyncProducerResult;
});
Mockito.when(pulsarClient.subscribeAsync(any(ConsumerConfigurationData.class), any(Schema.class), any())).thenReturn(CompletableFuture.completedFuture(consumer));
return pulsarClient;
} catch (Exception e) {
fail();
}
return null;
}
use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.
the class ZMSImplTest method testPublisherException.
@Test
public void testPublisherException() {
ZMSImpl zmsImpl = zmsTestInitializer.zmsInit();
String apiName = "postTopLevelDomain";
ResourceContext mockContext = Mockito.mock(ResourceContext.class);
when(mockContext.getApiName()).thenReturn(apiName);
when(mockContext.getDomainChangeMessages()).thenReturn(Collections.singletonList(new DomainChangeMessage()));
MockDomainChangePublisher mockDomainChangePublisher = new MockDomainChangePublisher("domainChanges");
mockDomainChangePublisher.setThrowPublishExceptions(true);
zmsImpl.domainChangePublishers = new ArrayList<>();
zmsImpl.domainChangePublishers.add(mockDomainChangePublisher);
// make sure no exceptions are thrown since we should catch and log them
zmsImpl.publishChangeMessage(mockContext, 200);
}
use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.
the class ZMSImplTest method testPublishEvent.
@Test
public void testPublishEvent() {
System.setProperty(ZMS_PROP_DOMAIN_CHANGE_PUBLISHER_FACTORY_CLASS, "com.yahoo.athenz.common.messaging.MockDomainChangePublisherFactory");
System.setProperty(ZMS_PROP_DOMAIN_CHANGE_TOPIC_NAMES, "topic1");
ZMSImpl zmsImpl = zmsTestInitializer.zmsInit();
assertNotNull(zmsImpl.domainChangePublishers);
List<String> topicNames = zmsImpl.domainChangePublishers.stream().map(publisher -> ((MockDomainChangePublisher) publisher).getTopicName()).collect(Collectors.toList());
assertThat(topicNames, containsInAnyOrder("topic1"));
ResourceContext mockContext = Mockito.mock(ResourceContext.class);
when(mockContext.getApiName()).thenReturn("apiName");
when(mockContext.getDomainChangeMessages()).thenReturn(Collections.singletonList(new DomainChangeMessage().setDomainName("domainName").setObjectName("objectName").setObjectType(DOMAIN).setApiName("apiName").setPublished(Instant.now().toEpochMilli()).setMessageId(java.util.UUID.randomUUID().toString())));
zmsImpl.publishChangeMessage(mockContext, 200);
// verify publish messages
MockDomainChangePublisher.Recorder evtRecorder = getEventRecorder(zmsImpl);
ArgumentCaptor<DomainChangeMessage> evtArgumentCaptor = ArgumentCaptor.forClass(DomainChangeMessage.class);
verify(evtRecorder, Mockito.times(1)).record(evtArgumentCaptor.capture());
DomainChangeMessage actual = evtArgumentCaptor.getValue();
assertEquals(actual.getDomainName(), "domainName");
assertEquals(actual.getApiName(), "apiName");
assertEquals(actual.getObjectType(), DOMAIN);
assertEquals(actual.getObjectName(), "objectName");
System.clearProperty(ZMS_PROP_DOMAIN_CHANGE_PUBLISHER_FACTORY_CLASS);
System.clearProperty(ZMS_PROP_DOMAIN_CHANGE_TOPIC_NAMES);
}
use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.
the class RsrcCtxWrapperTest method testDomainChangeMessageDisabled.
@Test
public void testDomainChangeMessageDisabled() {
HttpServletRequest servletRequest = new MockHttpServletRequest();
HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
AuthorityList authListMock = new AuthorityList();
Authorizer authorizerMock = Mockito.mock(Authorizer.class);
Object timerMetric = new Object();
RsrcCtxWrapper wrapper = new RsrcCtxWrapper(servletRequest, servletResponse, authListMock, false, authorizerMock, timerMetric, "apiName", false);
assertNull(wrapper.getDomainChangeMessages());
// add domain msg
wrapper.addDomainChangeMessage(new DomainChangeMessage().setDomainName("domain1Name").setObjectName("domain1Name1").setObjectType(DOMAIN));
// add role msg for the same domain
wrapper.addDomainChangeMessage(new DomainChangeMessage().setDomainName("domain1Name").setObjectName("domain1role").setObjectType(ROLE));
assertNull(wrapper.getDomainChangeMessages());
}
use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.
the class RsrcCtxWrapperTest method testDomainChanges.
@Test
public void testDomainChanges() {
HttpServletRequest servletRequest = new MockHttpServletRequest();
HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
AuthorityList authListMock = new AuthorityList();
Authorizer authorizerMock = Mockito.mock(Authorizer.class);
Metric metricMock = Mockito.mock(Metric.class);
Object timerMetricMock = Mockito.mock(Object.class);
RsrcCtxWrapper wrapper = new RsrcCtxWrapper(servletRequest, servletResponse, authListMock, false, authorizerMock, metricMock, timerMetricMock, "apiName");
wrapper.addDomainChangeMessage(new DomainChangeMessage());
assertNull(wrapper.getDomainChangeMessages());
}
Aggregations