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