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=========================================================
21 package org.onap.blueprintgenerator.models.dmaapbp;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
29 import java.util.ArrayList;
30 import java.util.List;
31 import org.junit.Test;
32 import org.onap.blueprintgenerator.models.blueprint.Blueprint;
33 import org.onap.blueprintgenerator.models.componentspec.Artifacts;
34 import org.onap.blueprintgenerator.models.componentspec.Auxilary;
35 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
36 import org.onap.blueprintgenerator.models.componentspec.Parameters;
37 import org.onap.blueprintgenerator.models.componentspec.Publishes;
38 import org.onap.blueprintgenerator.models.componentspec.Self;
39 import org.onap.blueprintgenerator.models.componentspec.Streams;
40 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
42 public class DmaapBlueprintTest {
44 private static final String MOCKED_NAME = "sample.name";
45 private static final String MESSAGE_ROUTER_TYPE_1 = "message_router";
46 private static final String MESSAGE_ROUTER_TYPE_2 = "message router";
47 private static final String DATA_ROUTER_TYPE_1 = "data_router";
48 private static final String DATA_ROUTER_TYPE_2 = "data router";
49 private static final String CONFIG_KEY_1 = "Configkey1";
50 private static final String CONFIG_KEY_2 = "Configkey2";
52 private static final String TOPIC_NODE_1 = CONFIG_KEY_1 + "_topic";
53 private static final String TOPIC_NODE_2 = CONFIG_KEY_2 + "_topic";
54 private static final String FEED_NODE_1 = CONFIG_KEY_1 + "_feed";
55 private static final String FEED_NODE_2 = CONFIG_KEY_2 + "_feed";
57 private static final String SAMPLE_FORMAT = "Format";
58 private static final String SAMPLE_VERSION = "1.0.0";
59 private static final String SAMPLE_ROUTE = "SampleRoute";
60 private static final String SAMPLE_DESCRIPTION = "sample description";
61 private static final String SAMPLE_PORTS = "8080:8080";
62 private static final String SAMPLE_ARTIFACT_TYPE = "test";
63 private static final String SAMPLE_ARTIFACT_URI = "test_uri";
66 public void dmaapBlueprintShouldHaveNodeTemplateWithDmaapNode() {
69 ComponentSpec componentSpec = getMockedComponentSpec();
70 DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
73 Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
76 assertTrue(resultBlueprint.getNode_templates().get(MOCKED_NAME) instanceof DmaapNode);
80 public void nodeTemplateHasTopicNodeWhenAddMessageRouterAsPublishes() {
82 ComponentSpec componentSpec = getMockedComponentSpec();
83 Streams streams = new Streams();
84 streams.setPublishes(getMessageRouterPublishes());
85 streams.setSubscribes(new Subscribes[0]);
87 when(componentSpec.getStreams()).thenReturn(streams);
88 DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
91 Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
94 assertNotNull(resultBlueprint.getNode_templates().get(TOPIC_NODE_1));
95 assertNotNull(resultBlueprint.getNode_templates().get(TOPIC_NODE_2));
100 public void nodeTemplateHasTopicNodeWhenAddMessageRouterAsSubscribes() {
102 ComponentSpec componentSpec = getMockedComponentSpec();
103 Streams streams = new Streams();
104 streams.setPublishes(new Publishes[0]);
105 streams.setSubscribes(getMessageRouterSubscribes());
107 when(componentSpec.getStreams()).thenReturn(streams);
108 DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
111 Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
114 assertNotNull(resultBlueprint.getNode_templates().get(TOPIC_NODE_1));
115 assertNotNull(resultBlueprint.getNode_templates().get(TOPIC_NODE_2));
120 public void nodeTemplateHasFeedNodeWhenAddDataRouterAsPublishes() {
122 ComponentSpec componentSpec = getMockedComponentSpec();
123 Streams streams = new Streams();
124 streams.setPublishes(getDataRouterPublishes());
125 streams.setSubscribes(new Subscribes[0]);
127 when(componentSpec.getStreams()).thenReturn(streams);
128 DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
131 Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
134 assertNotNull(resultBlueprint.getNode_templates().get(FEED_NODE_1));
135 assertNotNull(resultBlueprint.getNode_templates().get(FEED_NODE_2));
140 public void nodeTemplateHasFeedNodeWhenAddDataRouterAsSubscribes() {
142 ComponentSpec componentSpec = getMockedComponentSpec();
143 Streams streams = new Streams();
144 streams.setPublishes(new Publishes[0]);
145 streams.setSubscribes(getDataRouterSubscribes());
147 when(componentSpec.getStreams()).thenReturn(streams);
148 DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
151 Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
154 assertNotNull(resultBlueprint.getNode_templates().get(FEED_NODE_1));
155 assertNotNull(resultBlueprint.getNode_templates().get(FEED_NODE_2));
159 private Publishes[] getMessageRouterPublishes() {
160 List<Publishes> publishesList = new ArrayList<>();
162 publishesList.add(createSamplePublishes(MESSAGE_ROUTER_TYPE_1, CONFIG_KEY_1));
163 publishesList.add(createSamplePublishes(MESSAGE_ROUTER_TYPE_2, CONFIG_KEY_2));
164 return publishesList.toArray(new Publishes[0]);
167 private Subscribes[] getMessageRouterSubscribes() {
168 List<Subscribes> subscribesList = new ArrayList<>();
170 subscribesList.add(createSampleSubscribes(MESSAGE_ROUTER_TYPE_1, CONFIG_KEY_1));
171 subscribesList.add(createSampleSubscribes(MESSAGE_ROUTER_TYPE_2, CONFIG_KEY_2));
172 return subscribesList.toArray(new Subscribes[0]);
175 private Publishes[] getDataRouterPublishes() {
176 List<Publishes> publishesList = new ArrayList<>();
178 publishesList.add(createSamplePublishes(DATA_ROUTER_TYPE_1, CONFIG_KEY_1));
179 publishesList.add(createSamplePublishes(DATA_ROUTER_TYPE_2, CONFIG_KEY_2));
180 return publishesList.toArray(new Publishes[0]);
183 private Subscribes[] getDataRouterSubscribes() {
184 List<Subscribes> subscribesList = new ArrayList<>();
186 subscribesList.add(createSampleSubscribes(DATA_ROUTER_TYPE_1, CONFIG_KEY_1));
187 subscribesList.add(createSampleSubscribes(DATA_ROUTER_TYPE_2, CONFIG_KEY_2));
188 return subscribesList.toArray(new Subscribes[0]);
191 private Publishes createSamplePublishes(String type, String key) {
192 Publishes publishes = new Publishes();
194 publishes.setType(type);
195 publishes.setConfig_key(key);
196 publishes.setFormat(SAMPLE_FORMAT);
197 publishes.setVersion(SAMPLE_VERSION);
198 publishes.setRoute(SAMPLE_ROUTE);
203 private Subscribes createSampleSubscribes(String type, String key) {
204 Subscribes subscribes = new Subscribes();
206 subscribes.setType(type);
207 subscribes.setConfig_key(key);
208 subscribes.setFormat(SAMPLE_FORMAT);
209 subscribes.setVersion(SAMPLE_FORMAT);
210 subscribes.setRoute(SAMPLE_ROUTE);
215 private ComponentSpec getMockedComponentSpec() {
216 Self self = mock(Self.class);
217 when(self.getDescription()).thenReturn(SAMPLE_DESCRIPTION);
218 when(self.getName()).thenReturn(MOCKED_NAME);
220 Auxilary auxilary = mock(Auxilary.class);
221 ArrayList<Object> ports = new ArrayList<>();
222 ports.add(SAMPLE_PORTS);
223 when(auxilary.getPorts()).thenReturn(ports);
225 Streams streams = mock(Streams.class);
226 when(streams.getPublishes()).thenReturn(new Publishes[0]);
227 when(streams.getSubscribes()).thenReturn(new Subscribes[0]);
229 Artifacts artifact = new Artifacts();
230 artifact.setType(SAMPLE_ARTIFACT_TYPE);
231 artifact.setUri(SAMPLE_ARTIFACT_URI);
233 Artifacts[] arrayArtifacts = new Artifacts[10];
234 arrayArtifacts[0] = artifact;
236 ComponentSpec componentSpec = mock(ComponentSpec.class);
237 when(componentSpec.getSelf()).thenReturn(self);
238 when(componentSpec.getAuxilary()).thenReturn(auxilary);
239 when(componentSpec.getStreams()).thenReturn(streams);
240 when(componentSpec.getArtifacts()).thenReturn(arrayArtifacts);
241 when(componentSpec.getParameters()).thenReturn(new Parameters[0]);
242 return componentSpec;