aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpkaras <piotr.karas@nokia.com>2019-04-30 12:51:24 +0200
committerpkaras <piotr.karas@nokia.com>2019-05-02 10:29:36 +0200
commit1ef01702fd6e5b1742cb67a6614b97948e263e5f (patch)
treeb20bb1cfb3d556883d210f7fe9b7ecb91c63d7ed
parent682f1965193e672620bc8766fec6bcb2648aa8e5 (diff)
Junits for TopicService
Change-Id: Ie0a1223b7f5ff6871ff65bc88faa2ee92d57b7ce Issue-ID: DMAAP-1190 Signed-off-by: piotr.karas <piotr.karas@nokia.com>
-rw-r--r--src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java33
-rw-r--r--src/test/java/org/onap/dmaap/dbcapi/service/TopicServiceTest.java377
2 files changed, 227 insertions, 183 deletions
diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java b/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
index 68dfd51..08e58be 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
@@ -59,13 +59,13 @@ public class TopicService extends BaseLoggingClass {
// REF: https://wiki.web.att.com/pages/viewpage.action?pageId=519703122
private static String defaultGlobalMrHost;
- private Map<String, Topic> mr_topics = DatabaseClass.getTopics();
+ private Map<String, Topic> mr_topics;
private static DmaapService dmaapSvc = new DmaapService();
- private MR_ClientService clientService = new MR_ClientService();
- private MR_ClusterService clusters = new MR_ClusterService();
- private DcaeLocationService locations = new DcaeLocationService();
- private MirrorMakerService bridge = new MirrorMakerService();
+ private MR_ClientService clientService;
+ private MR_ClusterService clusters;
+ private DcaeLocationService locations;
+ private MirrorMakerService bridge;
private static String centralCname;
private static boolean createTopicRoles;
@@ -74,22 +74,33 @@ public class TopicService extends BaseLoggingClass {
public TopicService(){
- DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
+ this(DatabaseClass.getTopics(), new MR_ClientService(), (DmaapConfig)DmaapConfig.getConfig(),
+ new MR_ClusterService(), new DcaeLocationService(), new MirrorMakerService());
+
+ }
+
+ TopicService(Map<String, Topic> mr_topics, MR_ClientService clientService, DmaapConfig p,
+ MR_ClusterService clusters, DcaeLocationService locations, MirrorMakerService bridge) {
+ this.mr_topics = mr_topics;
+ this.clientService = clientService;
defaultGlobalMrHost = p.getProperty("MR.globalHost", "global.host.not.set");
centralCname = p.getProperty("MR.CentralCname");
createTopicRoles = "true".equalsIgnoreCase(p.getProperty("aaf.CreateTopicRoles", "true"));
String unit_test = p.getProperty( "UnitTest", "No" );
- if ( unit_test.equals( "Yes" ) ) {
+ if ( "Yes".equals(unit_test)) {
strictGraph = false;
}
mmPerMR = "true".equalsIgnoreCase(p.getProperty("MirrorMakerPerMR", "true"));
- logger.info( "TopicService properties: CentralCname=" + centralCname +
+ logger.info( "TopicService properties: CentralCname=" + centralCname +
" defaultGlobarlMrHost=" + defaultGlobalMrHost +
" createTopicRoles=" + createTopicRoles +
" mmPerMR=" + mmPerMR );
+ this.clusters = clusters;
+ this.locations = locations;
+ this.bridge = bridge;
}
-
- public Map<String, Topic> getTopics() {
+
+ public Map<String, Topic> getTopics() {
return mr_topics;
}
@@ -104,7 +115,7 @@ public class TopicService extends BaseLoggingClass {
ArrayList<Topic> topics = new ArrayList<>(mr_topics.values());
if ( withClients ) {
for( Topic topic: topics ) {
- topic.setClients( clientService.getAllMrClients(topic.getFqtn()));
+ topic.setClients(clientService.getAllMrClients(topic.getFqtn()));
}
}
return topics;
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 f571520..33a5411 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,190 +17,223 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
-package org.onap.dmaap.dbcapi.service;
-
-import org.onap.dmaap.dbcapi.model.*;
-import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
-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 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.mockito.BDDMockito.given;
+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";
- private static DmaapObjectFactory factory = new DmaapObjectFactory();
- ReflectionHarness rh = new ReflectionHarness();
-
- private TopicService ts;
- private MR_ClusterService mcs;
- private MR_ClientService cls;
- private DcaeLocationService dls;
-
- DmaapService ds;
- String locname;
-
- @Before
- public void setUp() throws Exception {
- ts = new TopicService();
- assert( ts != null );
- mcs = new MR_ClusterService();
- assert( mcs != null );
- Dmaap nd = factory.genDmaap();
- ds = new DmaapService();
- ds.addDmaap( nd );
- ts = new TopicService();
- mcs = new MR_ClusterService();
- cls = new MR_ClientService();
-
- dls = new DcaeLocationService();
- DcaeLocation loc = factory.genDcaeLocation( "central" );
- locname = loc.getDcaeLocationName();
- dls.addDcaeLocation( loc );
- loc = factory.genDcaeLocation( "edge");
-
- ApiError err = new ApiError();
-
- MR_Cluster node = factory.genMR_Cluster( "central" );
- mcs.addMr_Cluster( node, err);
- node = factory.genMR_Cluster("edge" );
- mcs.addMr_Cluster(node, err);
- }
-
- @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() {
- String t = "test3";
- Topic topic = factory.genSimpleTopic( t );
- ApiError err = new ApiError();
-
- Topic nTopic = ts.addTopic( topic, err, false );
- if ( nTopic != null ) {
- assertTrue( nTopic.getTopicName().equals( t ));
- }
-
- }
-
- @Test
- public void test3a() {
-
-
- ApiError err = new ApiError();
-
- String t = "org.onap.dmaap.interestingTopic";
- Topic topic = factory.genSimpleTopic(t);
- String f = "mrc.onap.org:3904/events/org.onap.dmaap.interestingTopic";
- String c = "publisher";
- String[] a = { "sub", "view" };
- MR_Client sub = factory.genMR_Client("central", f, c, a );
- String[] b = { "pub", "view" };
- MR_Client pub = factory.genMR_Client( "edge", f, c, b );
- ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
-
- clients.add( sub );
- clients.add( pub );
-
- topic.setClients( clients );
-
- ts.reviewTopic( topic );
- ts.checkForBridge( topic, err );
-
- Topic nTopic = ts.addTopic( topic, err, false );
- if ( nTopic != null ) {
- assertTrue( nTopic.getTopicName().equals( t ));
- }
-
-
- ts.removeTopic( "test3", err );
- }
-
- @Test
- public void test4() {
- List<Topic> l = ts.getAllTopics();
-
- }
-
- @Test
- public void test5() {
- ApiError err = new ApiError();
-
- Topic topic = factory.genSimpleTopic("test5");
- Topic nTopic = ts.updateTopic( topic, err );
-
- assertTrue( err.getCode() == 200 );
- }
-
- @Test
- public void bridgeTest6() {
- ApiError err = new ApiError();
+ 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;
+
+ @Before
+ public void setUp() throws Exception {
+ given(dmaapConfig.getProperty("MR.globalHost", "global.host.not.set")).willReturn(GLOBAL_MR_HOST);
+ createTopicService();
+ }
+
+ @Test
+ public void getTopics_shouldReturnTopicsReceivedDuringServiceCreation() {
+
+ ImmutableMap<String, Topic> topics = ImmutableMap.of(TOPIC_FQTN, new Topic());
+ topicService = new TopicService(topics, clientService, dmaapConfig, clusters, locations, bridge);
+
+ assertEquals(topics, topicService.getTopics());
+ }
+
+ @Test
+ public void getAllTopics_shouldReturnTopicsWithClients() {
+
+ ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
+ given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
+
+ List<Topic> allTopics = topicService.getAllTopics();
+
+ assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
+ assertEquals(mrClients, getOnlyElement(allTopics).getClients());
+ }
+
+ @Test
+ public void getAllTopicsWithoutClients_shouldReturnNoClients() {
+
+ List<Topic> allTopics = topicService.getAllTopicsWithoutClients();
+
+ assertThat(getOnlyElement(allTopics), hasCorrectFqtn(TOPIC_FQTN));
+ assertNull(getOnlyElement(allTopics).getClients());
+ verifyZeroInteractions(clientService);
+ }
+
+ @Test
+ public void getAllTopics_shouldCacheClients() {
+
+ ArrayList<MR_Client> mrClients = newArrayList(new MR_Client());
+ given(clientService.getAllMrClients(TOPIC_FQTN)).willReturn(mrClients);
+
+ topicService.getAllTopics();
+ List<Topic> 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(Response.Status.OK.getStatusCode(), apiError.getCode());
+ }
+
+ @Test
+ public void getTopic_shouldReturnTopicWithMrClients() {
+
+ ArrayList<MR_Client> 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(Response.Status.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(Response.Status.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(Response.Status.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);
- String t = "org.onap.dmaap.bridgingTopic";
- Topic topic = factory.genSimpleTopic(t);
- topic.setReplicationCase( ReplicationType.REPLICATION_EDGE_TO_CENTRAL );
+ ApiError apiError = new ApiError();
+ Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
- String c = "publisher";
- String[] a = { "sub", "view" };
- MR_Client sub = factory.genMR_Client("central", topic.getFqtn(), c, a );
- String[] b = { "pub", "view" };
- MR_Client pub = factory.genMR_Client( "edge", topic.getFqtn(), c, b );
- ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
+ assertEquals(Response.Status.OK.getStatusCode(), apiError.getCode());
+ assertEquals(GLOBAL_MR_HOST, addedTopic.getGlobalMrURL());
+ }
- clients.add( sub );
- clients.add( pub );
+ @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);
- topic.setClients( clients );
+ ApiError apiError = new ApiError();
+ Topic addedTopic = topicService.addTopic(newTopic, apiError, true);
- Topic nTopic = ts.updateTopic( topic, err );
+ assertEquals(500, apiError.getCode());
+ assertNull(addedTopic);
+ }
- assertTrue( err.getCode() == 200 );
- }
- @Test
- public void bridgeTest7() {
- ApiError err = new ApiError();
-
- String t = "org.onap.dmaap.bridgingTopic7";
- Topic topic = factory.genSimpleTopic(t);
- topic.setReplicationCase( ReplicationType.REPLICATION_CENTRAL_TO_EDGE );
-
- String c = "publisher";
- String[] a = { "sub", "view" };
- MR_Client sub = factory.genMR_Client("edge", topic.getFqtn(), c, a );
- String[] b = { "pub", "view" };
- MR_Client pub = factory.genMR_Client( "central", topic.getFqtn(), c, b );
- ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
-
- clients.add( sub );
- clients.add( pub );
+ private void createTopicService() {
+ Map<String, Topic> mrTopics = new HashMap<>();
+ mrTopics.put(TOPIC_FQTN, createTopic(TOPIC_FQTN));
+ topicService = new TopicService(mrTopics, clientService, dmaapConfig, clusters, locations, bridge);
+ }
- topic.setClients( clients );
+ private Topic createTopic(String fqtn) {
+ return new Topic(fqtn, "name", "desc", "tnxEnabled", "owner");
+ }
- Topic nTopic = ts.updateTopic( topic, err );
+ public static Matcher<Topic> hasCorrectFqtn(final String fqtn) {
+ return new BaseMatcher<Topic>() {
+ public boolean matches(Object o) {
+ return fqtn.equals(((Topic) o).getFqtn());
+ }
- assertTrue( err.getCode() == 200 );
- }
+ public void describeTo(Description description) {
+ description.appendText("Topics should should be equal. Expected fqtn: ").appendValue(fqtn);
+ }
+ };
+ }
}