updating docker repository to onap nexus
[dcaegen2/platform.git] / mod / bpgenerator / src / test / java / org / onap / blueprintgenerator / models / dmaapbp / DmaapBlueprintTest.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
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
9
10  http://www.apache.org/licenses/LICENSE-2.0
11
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=========================================================
18  */
19
20
21 package org.onap.blueprintgenerator.models.dmaapbp;
22
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;
27
28
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;
41
42 public class DmaapBlueprintTest {
43
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";
51
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";
56
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";
64
65     @Test
66     public void dmaapBlueprintShouldHaveNodeTemplateWithDmaapNode() {
67
68         //given
69         ComponentSpec componentSpec = getMockedComponentSpec();
70         DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
71
72         //when
73         Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
74
75         //then
76         assertTrue(resultBlueprint.getNode_templates().get(MOCKED_NAME) instanceof DmaapNode);
77     }
78
79     @Test
80     public void nodeTemplateHasTopicNodeWhenAddMessageRouterAsPublishes() {
81         //given
82         ComponentSpec componentSpec = getMockedComponentSpec();
83         Streams streams = new Streams();
84         streams.setPublishes(getMessageRouterPublishes());
85         streams.setSubscribes(new Subscribes[0]);
86
87         when(componentSpec.getStreams()).thenReturn(streams);
88         DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
89
90         //when
91         Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
92
93         //then
94         assertNotNull(resultBlueprint.getNode_templates().get(TOPIC_NODE_1));
95         assertNotNull(resultBlueprint.getNode_templates().get(TOPIC_NODE_2));
96
97     }
98
99     @Test
100     public void nodeTemplateHasTopicNodeWhenAddMessageRouterAsSubscribes() {
101         //given
102         ComponentSpec componentSpec = getMockedComponentSpec();
103         Streams streams = new Streams();
104         streams.setPublishes(new Publishes[0]);
105         streams.setSubscribes(getMessageRouterSubscribes());
106
107         when(componentSpec.getStreams()).thenReturn(streams);
108         DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
109
110         //when
111         Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
112
113         //then
114         assertNotNull(resultBlueprint.getNode_templates().get(TOPIC_NODE_1));
115         assertNotNull(resultBlueprint.getNode_templates().get(TOPIC_NODE_2));
116
117     }
118
119     @Test
120     public void nodeTemplateHasFeedNodeWhenAddDataRouterAsPublishes() {
121         //given
122         ComponentSpec componentSpec = getMockedComponentSpec();
123         Streams streams = new Streams();
124         streams.setPublishes(getDataRouterPublishes());
125         streams.setSubscribes(new Subscribes[0]);
126
127         when(componentSpec.getStreams()).thenReturn(streams);
128         DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
129
130         //when
131         Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
132
133         //then
134         assertNotNull(resultBlueprint.getNode_templates().get(FEED_NODE_1));
135         assertNotNull(resultBlueprint.getNode_templates().get(FEED_NODE_2));
136
137     }
138
139     @Test
140     public void nodeTemplateHasFeedNodeWhenAddDataRouterAsSubscribes() {
141         //given
142         ComponentSpec componentSpec = getMockedComponentSpec();
143         Streams streams = new Streams();
144         streams.setPublishes(new Publishes[0]);
145         streams.setSubscribes(getDataRouterSubscribes());
146
147         when(componentSpec.getStreams()).thenReturn(streams);
148         DmaapBlueprint dmaapBlueprint = new DmaapBlueprint();
149
150         //when
151         Blueprint resultBlueprint = dmaapBlueprint.createDmaapBlueprint(componentSpec, "", "");
152
153         //then
154         assertNotNull(resultBlueprint.getNode_templates().get(FEED_NODE_1));
155         assertNotNull(resultBlueprint.getNode_templates().get(FEED_NODE_2));
156
157     }
158
159     private Publishes[] getMessageRouterPublishes() {
160         List<Publishes> publishesList = new ArrayList<>();
161
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]);
165     }
166
167     private Subscribes[] getMessageRouterSubscribes() {
168         List<Subscribes> subscribesList = new ArrayList<>();
169
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]);
173     }
174
175     private Publishes[] getDataRouterPublishes() {
176         List<Publishes> publishesList = new ArrayList<>();
177
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]);
181     }
182
183     private Subscribes[] getDataRouterSubscribes() {
184         List<Subscribes> subscribesList = new ArrayList<>();
185
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]);
189     }
190
191     private Publishes createSamplePublishes(String type, String key) {
192         Publishes publishes = new Publishes();
193
194         publishes.setType(type);
195         publishes.setConfig_key(key);
196         publishes.setFormat(SAMPLE_FORMAT);
197         publishes.setVersion(SAMPLE_VERSION);
198         publishes.setRoute(SAMPLE_ROUTE);
199
200         return publishes;
201     }
202
203     private Subscribes createSampleSubscribes(String type, String key) {
204         Subscribes subscribes = new Subscribes();
205
206         subscribes.setType(type);
207         subscribes.setConfig_key(key);
208         subscribes.setFormat(SAMPLE_FORMAT);
209         subscribes.setVersion(SAMPLE_FORMAT);
210         subscribes.setRoute(SAMPLE_ROUTE);
211
212         return subscribes;
213     }
214
215     private ComponentSpec getMockedComponentSpec() {
216         Self self = mock(Self.class);
217         when(self.getDescription()).thenReturn(SAMPLE_DESCRIPTION);
218         when(self.getName()).thenReturn(MOCKED_NAME);
219
220         Auxilary auxilary = mock(Auxilary.class);
221         ArrayList<Object> ports = new ArrayList<>();
222         ports.add(SAMPLE_PORTS);
223         when(auxilary.getPorts()).thenReturn(ports);
224
225         Streams streams = mock(Streams.class);
226         when(streams.getPublishes()).thenReturn(new Publishes[0]);
227         when(streams.getSubscribes()).thenReturn(new Subscribes[0]);
228
229         Artifacts artifact = new Artifacts();
230         artifact.setType(SAMPLE_ARTIFACT_TYPE);
231         artifact.setUri(SAMPLE_ARTIFACT_URI);
232
233         Artifacts[] arrayArtifacts = new Artifacts[10];
234         arrayArtifacts[0] = artifact;
235
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;
243     }
244 }