684657cda0abd9a0c230e7d50e09c176848cb04e
[dcaegen2/platform.git] / mod / bpgenerator / src / test / java / org / onap / blueprintgenerator / models / dmaapbp / DmaapNodeTest.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 package org.onap.blueprintgenerator.models.dmaapbp;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Map;
29 import java.util.TreeMap;
30 import org.junit.Test;
31 import org.onap.blueprintgenerator.core.TestComponentSpec;
32 import org.onap.blueprintgenerator.models.componentspec.Auxilary;
33 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
34 import org.onap.blueprintgenerator.models.componentspec.Publishes;
35 import org.onap.blueprintgenerator.models.componentspec.Streams;
36 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
37
38 public class DmaapNodeTest {
39
40     private static final String EXPECTED_DMAAP_NODE_TYPE = "dcae.nodes.ContainerizedServiceComponentUsingDmaap";
41
42     private static final String DATA_ROUTER_TYPE = "data_router";
43     private static final String MESSAGE_ROUTER_TYPE = "message_router";
44
45     private static final String CONFIG_KEY = "Configkey";
46
47     private static final String SAMPLE_FORMAT = "Format";
48     private static final String SAMPLE_VERSION = "1.0.0";
49     private static final String SAMPLE_ROUTE = "SampleRoute";
50     private static final String TYPE = "type";
51     private static final String TARGET = "target";
52
53
54     @Test
55     public void dmaapNodeShouldHaveExpectedNodeType() {
56
57         ComponentSpec mockedComponentSpec = getSpiedComponentSpecWithoutRelationships();
58
59         DmaapNode dmaapNode = new DmaapNode();
60         dmaapNode.createDmaapNode(mockedComponentSpec, new TreeMap<>(), "");
61
62         assertEquals(EXPECTED_DMAAP_NODE_TYPE, dmaapNode.getType());
63     }
64
65     @Test
66     public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForMessageRouterPublishes() {
67         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
68
69         Streams streams = new Streams();
70         streams.setSubscribes(new Subscribes[0]);
71         streams.setPublishes(createSamplePublishes(MESSAGE_ROUTER_TYPE));
72
73         when(componentSpec.getStreams()).thenReturn(streams);
74         DmaapNode dmaapNode = new DmaapNode();
75         dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
76
77         Map<String, String> relationship = dmaapNode.getRelationships().get(0);
78
79         assertNotNull(relationship.get(TYPE));
80         assertNotNull(relationship.get(TARGET));
81     }
82
83     @Test
84     public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForDataRouterPublishes() {
85         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
86
87         Streams streams = new Streams();
88         streams.setSubscribes(new Subscribes[0]);
89         streams.setPublishes(createSamplePublishes(DATA_ROUTER_TYPE));
90
91         when(componentSpec.getStreams()).thenReturn(streams);
92         DmaapNode dmaapNode = new DmaapNode();
93         dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
94
95         Map<String, String> relationship = dmaapNode.getRelationships().get(0);
96
97         assertNotNull(relationship.get(TYPE));
98         assertNotNull(relationship.get(TARGET));
99     }
100
101     @Test
102     public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForMessageRouterSubscribes() {
103         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
104
105         Streams streams = new Streams();
106         streams.setSubscribes(createSampleSubscribes(MESSAGE_ROUTER_TYPE));
107         streams.setPublishes(new Publishes[0]);
108
109         when(componentSpec.getStreams()).thenReturn(streams);
110         DmaapNode dmaapNode = new DmaapNode();
111         dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
112
113         Map<String, String> relationship = dmaapNode.getRelationships().get(0);
114
115         assertNotNull(relationship.get(TYPE));
116         assertNotNull(relationship.get(TARGET));
117     }
118
119     @Test
120     public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForDataRouterSubscribes() {
121         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
122
123         Streams streams = new Streams();
124         streams.setSubscribes(createSampleSubscribes(DATA_ROUTER_TYPE));
125         streams.setPublishes(new Publishes[0]);
126
127         when(componentSpec.getStreams()).thenReturn(streams);
128         DmaapNode dmaapNode = new DmaapNode();
129         dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
130
131         Map<String, String> relationship = dmaapNode.getRelationships().get(0);
132
133         assertNotNull(relationship.get(TYPE));
134         assertNotNull(relationship.get(TARGET));
135     }
136
137     @Test
138     public void createFeedNodeShouldSetFeedNodeType() {
139         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
140
141         DmaapNode dmaapNode = new DmaapNode();
142         dmaapNode.createFeedNode(componentSpec, new TreeMap<>() ,"");
143
144         assertTrue(dmaapNode.getType().endsWith("Feed"));
145     }
146
147     @Test
148     public void createTopicNodeShouldSetTopicNodeType() {
149
150         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
151
152         DmaapNode dmaapNode = new DmaapNode();
153         dmaapNode.createTopicNode(componentSpec, new TreeMap<>() ,"");
154
155         assertTrue(dmaapNode.getType().endsWith("Topic"));
156     }
157
158     private Publishes[] createSamplePublishes(String type) {
159         Publishes publishes = new Publishes();
160
161         publishes.setType(type);
162         publishes.setConfig_key(CONFIG_KEY);
163         publishes.setFormat(SAMPLE_FORMAT);
164         publishes.setVersion(SAMPLE_VERSION);
165         publishes.setRoute(SAMPLE_ROUTE);
166
167         return new Publishes[]{publishes};
168     }
169
170     private Subscribes[] createSampleSubscribes(String type) {
171         Subscribes subscribes = new Subscribes();
172
173         subscribes.setType(type);
174         subscribes.setConfig_key(CONFIG_KEY);
175         subscribes.setFormat(SAMPLE_FORMAT);
176         subscribes.setVersion(SAMPLE_VERSION);
177         subscribes.setRoute(SAMPLE_ROUTE);
178
179         return new Subscribes[]{subscribes};
180     }
181
182     private ComponentSpec getSpiedComponentSpecWithoutRelationships() {
183         ComponentSpec baseComponentSpec = new ComponentSpec();
184         baseComponentSpec.createComponentSpecFromString(new TestComponentSpec().getCs());
185         ComponentSpec componentSpec = spy(baseComponentSpec);
186
187         Streams streams = new Streams();
188         streams.setSubscribes(new Subscribes[0]);
189         streams.setPublishes(new Publishes[0]);
190         when(componentSpec.getStreams()).thenReturn(streams);
191
192         Auxilary auxilary = spy(baseComponentSpec.getAuxilary());
193         when(auxilary.getDatabases()).thenReturn(null);
194
195         when(componentSpec.getAuxilary()).thenReturn(auxilary);
196         when(componentSpec.getPolicyInfo()).thenReturn(null);
197
198         return componentSpec;
199
200     }
201 }