X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdbcapi%2Fservice%2FTopicServiceTest.java;h=a37ce029f2d715b8cfce3e9eb1c1ec9e99fb02f5;hb=411cb435b5878b2663bfa9b6d2495c707353cd63;hp=5997ab102befe996d6b65de7fa5b2eb4d7194286;hpb=d5d37c0477744cbaa6a9d9fc690c0dafbb2d1868;p=dmaap%2Fdbcapi.git diff --git a/src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java b/src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java index 5997ab1..a37ce02 100644 --- a/src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java +++ b/src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * org.onap.dmaap * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. * ================================================================================ * 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. @@ -17,126 +17,289 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.dmaap.dbcapi.service; - -import org.onap.dmaap.dbcapi.model.*; -import org.onap.dmaap.dbcapi.testframework.ReflectionHarness; -import static org.junit.Assert.*; +package org.onap.dmaap.dbcapi.service; -import org.junit.After; +import com.google.common.collect.ImmutableMap; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; import org.junit.Before; import org.junit.Test; -import java.util.List; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.dmaap.dbcapi.model.ApiError; +import org.onap.dmaap.dbcapi.model.MR_Client; +import org.onap.dmaap.dbcapi.model.Topic; +import org.onap.dmaap.dbcapi.util.DmaapConfig; + +import javax.ws.rs.core.Response; import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.google.common.collect.Iterables.getOnlyElement; +import static com.google.common.collect.Lists.newArrayList; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; +import static javax.ws.rs.core.Response.Status.OK; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.onap.dmaap.dbcapi.model.ReplicationType.REPLICATION_GLOBAL_TO_FQDN; +@RunWith(MockitoJUnitRunner.class) public class TopicServiceTest { - private static final String fmt = "%24s: %s%n"; - - ReflectionHarness rh = new ReflectionHarness(); - - TopicService ts; - MR_ClusterService mcs; - String locname = "central-onap"; - - @Before - public void setUp() throws Exception { - ts = new TopicService(); - mcs = new MR_ClusterService(); - } - - @After - public void tearDown() throws Exception { - } - - - @Test - public void test1() { - - - rh.reflect( "org.onap.dmaap.dbcapi.service.TopicService", "get", null ); - - } - - @Test - public void test2() { - String v = "Validate"; - rh.reflect( "org.onap.dmaap.dbcapi.service.TopicService", "set", v ); - - } - - @Test - public void test3() { - Topic topic = new Topic(); - ApiError err = new ApiError(); - topic.setTopicName( "test3" ); - topic.setFqtnStyle( FqtnType.Validator("none") ); - topic.getFqtn(); - Topic nTopic = ts.addTopic( topic, err ); - if ( nTopic != null ) { - assertTrue( nTopic.getTopicName().equals( topic.getTopicName() )); - } - - } - - @Test - public void test3a() { - Topic topic = new Topic(); - ApiError err = new ApiError(); - topic.setTopicName( "test3" ); - topic.setFqtnStyle( FqtnType.Validator("none") ); - topic.getFqtn(); - String t = "org.onap.dmaap.interestingTopic"; - String f = "mrc.onap.org:3904/events/org.onap.dmaap.interestingTopic"; - String c = "publisher"; - String[] a = { "sub", "view" }; - MR_Client sub = new MR_Client( locname, f, c, a ); - String[] b = { "pub", "view" }; - MR_Client pub = new MR_Client( "edge", f, c, b ); - ArrayList clients = new ArrayList(); - - clients.add( sub ); - clients.add( pub ); - - topic.setClients( clients ); - - ts.reviewTopic( topic ); - ts.checkForBridge( topic, err ); - - Topic nTopic = ts.addTopic( topic, err ); - if ( nTopic != null ) { - assertTrue( nTopic.getTopicName().equals( topic.getTopicName() )); - } - - - ts.removeTopic( "test3", err ); - } - - @Test - public void test4() { - List l = ts.getAllTopics(); - - } - - @Test - public void test5() { - ApiError err = new ApiError(); -/* - -TODO: find a null pointer in here... - String[] hl = { "host1", "host2", "host3" }; - String loc = "central-onap"; - MR_Cluster cluster = new MR_Cluster( loc, "localhost", "", hl ); - mcs.addMr_Cluster( cluster, err ); - Topic topic = new Topic(); - topic.setTopicName( "test5" ); - topic.setFqtnStyle( FqtnType.Validator("none") ); - topic.setReplicationCase( ReplicationType.Validator("none") ); - String f = topic.getFqtn(); - Topic nTopic = ts.updateTopic( topic, err ); -*/ - assertTrue( err.getCode() == 0 ); - } + private static final String TOPIC_FQTN = "topic_1"; + private static final String GLOBAL_MR_HOST = "global.mr.host"; + private TopicService topicService; + @Mock + private MR_ClientService clientService; + @Mock + private DmaapConfig dmaapConfig; + @Mock + private MR_ClusterService clusters; + @Mock + private DcaeLocationService locations; + @Mock + private MirrorMakerService bridge; + @Mock + private AafTopicSetupService aafTopicSetupService; + + @Before + public void setUp() throws Exception { + given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn(GLOBAL_MR_HOST); + given(aafTopicSetupService.aafTopicSetup(any(Topic.class))).willReturn(new ApiError(200, "OK")); + given(aafTopicSetupService.aafTopicCleanup(any(Topic.class))).willReturn(new ApiError(200, "OK")); + createTopicService(); + } + + @Test + public void getTopics_shouldReturnTopicsReceivedDuringServiceCreation() { + + ImmutableMap topics = ImmutableMap.of(TOPIC_FQTN, new Topic()); + topicService = new TopicService(topics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService); + + assertEquals(topics, topicService.getTopics()); + } + + @Test + public void getAllTopics_shouldReturnTopicsWithClients() { + + ArrayList mrClients = newArrayList(new MR_Client()); + given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients); + + List allTopics = topicService.getAllTopics(); + + assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN)); + assertEquals(mrClients, getOnlyElement(allTopics).getClients()); + } + + @Test + public void getAllTopicsWithoutClients_shouldReturnNoClients() { + + List allTopics = topicService.getAllTopicsWithoutClients(); + + assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN)); + assertNull(getOnlyElement(allTopics).getClients()); + verifyZeroInteractions(clientService); + } + + @Test + public void getAllTopics_shouldCacheClients() { + + ArrayList mrClients = newArrayList(new MR_Client()); + given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients); + + topicService.getAllTopics(); + List allTopics = topicService.getAllTopicsWithoutClients(); + + assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN)); + assertEquals(mrClients, getOnlyElement(allTopics).getClients()); + } + + @Test + public void getTopic_shouldReturnTopicByFqtn() { + + ApiError apiError = new ApiError(); + Topic topic = topicService.getTopic(TOPIC_FQTN, apiError); + + assertThat(topic, hasCorrectFqtn(TOPIC_FQTN)); + assertEquals(OK.getStatusCode(), apiError.getCode()); + } + + @Test + public void getTopic_shouldReturnTopicWithMrClients() { + + ArrayList mrClients = newArrayList(new MR_Client()); + given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients); + + Topic topic = topicService.getTopic(TOPIC_FQTN, new ApiError()); + + assertThat(topic, hasCorrectFqtn(TOPIC_FQTN)); + assertEquals(mrClients, topic.getClients()); + } + + @Test + public void getTopic_shouldReturnError() { + + ApiError apiError = new ApiError(); + Topic topic = topicService.getTopic("not_existing", apiError); + + assertNull(topic); + assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode()); + } + + @Test + public void addTopic_shouldAddNewTopic() { + Topic newTopic = createTopic(""); + + ApiError apiError = new ApiError(); + Topic addedTopic = topicService.addTopic(newTopic, apiError, true); + + assertSame(newTopic, addedTopic); + assertEquals(OK.getStatusCode(), apiError.getCode()); + assertNotNull(topicService.getTopic(addedTopic.getFqtn(), new ApiError())); + } + + @Test + public void addTopic_shouldReturnErrorWhenTopicAlreadyExists() { + Topic newTopic = createTopic(""); + + ApiError apiError = new ApiError(); + Topic addedTopic = topicService.addTopic(newTopic, apiError, false); + Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, false); + + assertNull(secondAddedTopic); + assertEquals(Response.Status.CONFLICT.getStatusCode(), apiError.getCode()); + } + + @Test + public void addTopic_shouldAddTheSameTopicWhenUseExistingIsSet() { + Topic newTopic = createTopic(""); + + ApiError apiError = new ApiError(); + Topic addedTopic = topicService.addTopic(newTopic, apiError, false); + Topic secondAddedTopic = topicService.addTopic(addedTopic, apiError, true); + + assertSame(addedTopic, secondAddedTopic); + assertEquals(OK.getStatusCode(), apiError.getCode()); + assertNotNull(topicService.getTopic(secondAddedTopic.getFqtn(), new ApiError())); + } + + + @Test + public void addTopic_shouldSetGlobalMrURL() { + Topic newTopic = createTopic(TOPIC_FQTN); + newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN); + + ApiError apiError = new ApiError(); + Topic addedTopic = topicService.addTopic(newTopic, apiError, true); + + assertEquals(OK.getStatusCode(), apiError.getCode()); + assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL()); + } + + @Test + public void addTopic_shouldReturnErrorWhenGlobalMrURLIsInvalid() { + given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn("invalid@host"); + createTopicService(); + Topic newTopic = createTopic(TOPIC_FQTN); + newTopic.setReplicationCase(REPLICATION_GLOBAL_TO_FQDN); + + ApiError apiError = new ApiError(); + Topic addedTopic = topicService.addTopic(newTopic, apiError, true); + + assertEquals(500, apiError.getCode()); + assertNull(addedTopic); + } + + @Test + public void removeTopic_shouldFailIfTopicDoesNotExist() { + ApiError apiError = new ApiError(); + + Topic removedTopic = topicService.removeTopic("not_existing_fqtn", apiError); + + assertNull(removedTopic); + assertEquals(NOT_FOUND.getStatusCode(), apiError.getCode()); + assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN)); + } + + @Test + public void removeTopic_shouldExecuteAafCleanup() { + ApiError apiError = new ApiError(); + + Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError); + + then(aafTopicSetupService).should().aafTopicCleanup(removedTopic); + assertEquals(OK.getStatusCode(), apiError.getCode()); + } + + @Test + public void removeTopic_shouldRemoveEachMrClientAssignedToTopic() { + ApiError apiError = new ApiError(); + MR_Client mrClient = new MR_Client(); + mrClient.setMrClientId("mrClientId"); + + given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(newArrayList(mrClient)); + + topicService.removeTopic(TOPIC_FQTN, apiError); + + then(clientService).should().removeMr_Client(mrClient.getMrClientId(), false, apiError); + assertEquals(OK.getStatusCode(), apiError.getCode()); + } + + @Test + public void removeTopic_shouldRemoveTopicFromCache() { + ApiError apiError = new ApiError(); + + topicService.removeTopic(TOPIC_FQTN, apiError); + + assertTrue(topicService.getTopics().isEmpty()); + assertEquals(OK.getStatusCode(), apiError.getCode()); + } + + @Test + public void removeTopic_shouldFailIfAafCleanupWasFailed() { + ApiError apiError = new ApiError(); + given(aafTopicSetupService.aafTopicCleanup(any(Topic.class))).willReturn(new ApiError(404, "sth went wrong")); + + Topic removedTopic = topicService.removeTopic(TOPIC_FQTN, apiError); + + assertNull(removedTopic); + assertEquals(404, apiError.getCode()); + assertTrue(topicService.getTopics().containsKey(TOPIC_FQTN)); + } + + private void createTopicService() { + Map mrTopics = new HashMap<>(); + mrTopics.put(TOPIC_FQTN, createTopic(TOPIC_FQTN)); + topicService = new TopicService(mrTopics, clientService, dmaapConfig, clusters, locations, bridge, aafTopicSetupService); + } + + private Topic createTopic(String fqtn) { + return new Topic(fqtn, "name", "desc", "tnxEnabled", "owner"); + } + + public static Matcher hasCorrectFqtn(final String fqtn) { + return new BaseMatcher() { + public boolean matches(Object o) { + return fqtn.equals(((Topic) o).getFqtn()); + } + + public void describeTo(Description description) { + description.appendText("Topics should should be equal. Expected fqtn: ").appendValue(fqtn); + } + }; + } }