1 /*============LICENSE_START=======================================================
3 ================================================================================
4 Copyright (c) 2020 Nokia. All rights reserved.
5 ================================================================================
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ============LICENSE_END=========================================================
20 package org.onap.blueprintgenerator.models.dmaapbp;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.CONTENERIZED_SERVICE_COMPONENT_USING_DMAAP;
29 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.FEED;
30 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.SUBSCRIBE_TO_EVENTS;
31 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.SUBSCRIBE_TO_FILES;
32 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.TOPIC;
35 import java.util.TreeMap;
36 import org.junit.Test;
37 import org.onap.blueprintgenerator.core.TestComponentSpec;
38 import org.onap.blueprintgenerator.models.componentspec.Auxilary;
39 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
40 import org.onap.blueprintgenerator.models.componentspec.Publishes;
41 import org.onap.blueprintgenerator.models.componentspec.Streams;
42 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
44 public class DmaapNodeTest {
46 private static final String DATA_ROUTER_TYPE = "data_router";
47 private static final String MESSAGE_ROUTER_TYPE = "message_router";
49 private static final String CONFIG_KEY = "Configkey";
51 private static final String SAMPLE_FORMAT = "Format";
52 private static final String SAMPLE_VERSION = "1.0.0";
53 private static final String SAMPLE_ROUTE = "SampleRoute";
54 private static final String TYPE = "type";
55 private static final String TARGET = "target";
59 public void dmaapNodeShouldHaveExpectedNodeType() {
61 ComponentSpec mockedComponentSpec = getSpiedComponentSpecWithoutRelationships();
63 DmaapNode dmaapNode = new DmaapNode();
64 dmaapNode.createDmaapNode(mockedComponentSpec, new TreeMap<>(), "");
66 assertEquals(CONTENERIZED_SERVICE_COMPONENT_USING_DMAAP, dmaapNode.getType());
70 public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForMessageRouterPublishes() {
71 ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
73 Streams streams = new Streams();
74 streams.setSubscribes(new Subscribes[0]);
75 streams.setPublishes(createSamplePublishes(MESSAGE_ROUTER_TYPE));
77 when(componentSpec.getStreams()).thenReturn(streams);
78 DmaapNode dmaapNode = new DmaapNode();
79 dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
81 Map<String, String> relationship = dmaapNode.getRelationships().get(0);
83 assertNotNull(relationship.get(TYPE));
84 assertNotNull(relationship.get(TARGET));
88 public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForDataRouterPublishes() {
89 ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
91 Streams streams = new Streams();
92 streams.setSubscribes(new Subscribes[0]);
93 streams.setPublishes(createSamplePublishes(DATA_ROUTER_TYPE));
95 when(componentSpec.getStreams()).thenReturn(streams);
96 DmaapNode dmaapNode = new DmaapNode();
97 dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
99 Map<String, String> relationship = dmaapNode.getRelationships().get(0);
101 assertNotNull(relationship.get(TYPE));
102 assertNotNull(relationship.get(TARGET));
106 public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForMessageRouterSubscribes() {
107 ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
109 Streams streams = new Streams();
110 streams.setSubscribes(createSampleSubscribes(MESSAGE_ROUTER_TYPE));
111 streams.setPublishes(new Publishes[0]);
113 when(componentSpec.getStreams()).thenReturn(streams);
114 DmaapNode dmaapNode = new DmaapNode();
115 dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
117 Map<String, String> relationship = dmaapNode.getRelationships().get(0);
119 assertEquals(SUBSCRIBE_TO_EVENTS, relationship.get(TYPE));
120 assertNotNull(relationship.get(TARGET));
124 public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForDataRouterSubscribes() {
125 ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
127 Streams streams = new Streams();
128 streams.setSubscribes(createSampleSubscribes(DATA_ROUTER_TYPE));
129 streams.setPublishes(new Publishes[0]);
131 when(componentSpec.getStreams()).thenReturn(streams);
132 DmaapNode dmaapNode = new DmaapNode();
133 dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
135 Map<String, String> relationship = dmaapNode.getRelationships().get(0);
137 assertEquals(SUBSCRIBE_TO_FILES, relationship.get(TYPE));
138 assertNotNull(relationship.get(TARGET));
142 public void createFeedNodeShouldSetFeedNodeType() {
143 ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
145 DmaapNode dmaapNode = new DmaapNode();
146 dmaapNode.createFeedNode(componentSpec, new TreeMap<>(), "");
148 assertEquals(FEED, dmaapNode.getType());
152 public void feedNodePropertiesShouldHaveUseExistingField() {
153 ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
155 DmaapNode dmaapNode = new DmaapNode();
156 dmaapNode.createFeedNode(componentSpec, new TreeMap<>() ,"");
158 assertTrue(dmaapNode.getProperties().getUseExisting());
162 public void createTopicNodeShouldSetTopicNodeType() {
164 ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
166 DmaapNode dmaapNode = new DmaapNode();
167 dmaapNode.createTopicNode(componentSpec, new TreeMap<>(), "");
169 assertEquals(TOPIC, dmaapNode.getType());
173 public void topicNodePropertiesShouldNotHaveUseExistingField() {
174 ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
176 DmaapNode dmaapNode = new DmaapNode();
177 dmaapNode.createTopicNode(componentSpec, new TreeMap<>() ,"");
179 assertNull(dmaapNode.getProperties().getUseExisting());
182 private Publishes[] createSamplePublishes(String type) {
183 Publishes publishes = new Publishes();
185 publishes.setType(type);
186 publishes.setConfig_key(CONFIG_KEY);
187 publishes.setFormat(SAMPLE_FORMAT);
188 publishes.setVersion(SAMPLE_VERSION);
189 publishes.setRoute(SAMPLE_ROUTE);
191 return new Publishes[]{publishes};
194 private Subscribes[] createSampleSubscribes(String type) {
195 Subscribes subscribes = new Subscribes();
197 subscribes.setType(type);
198 subscribes.setConfig_key(CONFIG_KEY);
199 subscribes.setFormat(SAMPLE_FORMAT);
200 subscribes.setVersion(SAMPLE_VERSION);
201 subscribes.setRoute(SAMPLE_ROUTE);
203 return new Subscribes[]{subscribes};
206 private ComponentSpec getSpiedComponentSpecWithoutRelationships() {
207 ComponentSpec baseComponentSpec = new ComponentSpec();
208 baseComponentSpec.createComponentSpecFromString(new TestComponentSpec().getComponentSpecAsString());
209 ComponentSpec componentSpec = spy(baseComponentSpec);
211 Streams streams = new Streams();
212 streams.setSubscribes(new Subscribes[0]);
213 streams.setPublishes(new Publishes[0]);
214 when(componentSpec.getStreams()).thenReturn(streams);
216 Auxilary auxilary = spy(baseComponentSpec.getAuxilary());
217 when(auxilary.getDatabases()).thenReturn(null);
219 when(componentSpec.getAuxilary()).thenReturn(auxilary);
220 when(componentSpec.getPolicyInfo()).thenReturn(null);
222 return componentSpec;