Merge "Improve code quality in ResourceConfig"
[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.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;
33
34 import java.util.Map;
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;
43
44 public class DmaapNodeTest {
45
46     private static final String DATA_ROUTER_TYPE = "data_router";
47     private static final String MESSAGE_ROUTER_TYPE = "message_router";
48
49     private static final String CONFIG_KEY = "Configkey";
50
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";
56
57
58     @Test
59     public void dmaapNodeShouldHaveExpectedNodeType() {
60
61         ComponentSpec mockedComponentSpec = getSpiedComponentSpecWithoutRelationships();
62
63         DmaapNode dmaapNode = new DmaapNode();
64         dmaapNode.createDmaapNode(mockedComponentSpec, new TreeMap<>(), "");
65
66         assertEquals(CONTENERIZED_SERVICE_COMPONENT_USING_DMAAP, dmaapNode.getType());
67     }
68
69     @Test
70     public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForMessageRouterPublishes() {
71         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
72
73         Streams streams = new Streams();
74         streams.setSubscribes(new Subscribes[0]);
75         streams.setPublishes(createSamplePublishes(MESSAGE_ROUTER_TYPE));
76
77         when(componentSpec.getStreams()).thenReturn(streams);
78         DmaapNode dmaapNode = new DmaapNode();
79         dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
80
81         Map<String, String> relationship = dmaapNode.getRelationships().get(0);
82
83         assertNotNull(relationship.get(TYPE));
84         assertNotNull(relationship.get(TARGET));
85     }
86
87     @Test
88     public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForDataRouterPublishes() {
89         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
90
91         Streams streams = new Streams();
92         streams.setSubscribes(new Subscribes[0]);
93         streams.setPublishes(createSamplePublishes(DATA_ROUTER_TYPE));
94
95         when(componentSpec.getStreams()).thenReturn(streams);
96         DmaapNode dmaapNode = new DmaapNode();
97         dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
98
99         Map<String, String> relationship = dmaapNode.getRelationships().get(0);
100
101         assertNotNull(relationship.get(TYPE));
102         assertNotNull(relationship.get(TARGET));
103     }
104
105     @Test
106     public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForMessageRouterSubscribes() {
107         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
108
109         Streams streams = new Streams();
110         streams.setSubscribes(createSampleSubscribes(MESSAGE_ROUTER_TYPE));
111         streams.setPublishes(new Publishes[0]);
112
113         when(componentSpec.getStreams()).thenReturn(streams);
114         DmaapNode dmaapNode = new DmaapNode();
115         dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
116
117         Map<String, String> relationship = dmaapNode.getRelationships().get(0);
118
119         assertEquals(SUBSCRIBE_TO_EVENTS, relationship.get(TYPE));
120         assertNotNull(relationship.get(TARGET));
121     }
122
123     @Test
124     public void createdDmaapNodeShouldHaveRelationshipWithTypeAndTargetForDataRouterSubscribes() {
125         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
126
127         Streams streams = new Streams();
128         streams.setSubscribes(createSampleSubscribes(DATA_ROUTER_TYPE));
129         streams.setPublishes(new Publishes[0]);
130
131         when(componentSpec.getStreams()).thenReturn(streams);
132         DmaapNode dmaapNode = new DmaapNode();
133         dmaapNode.createDmaapNode(componentSpec, new TreeMap<>(), "");
134
135         Map<String, String> relationship = dmaapNode.getRelationships().get(0);
136
137         assertEquals(SUBSCRIBE_TO_FILES, relationship.get(TYPE));
138         assertNotNull(relationship.get(TARGET));
139     }
140
141     @Test
142     public void createFeedNodeShouldSetFeedNodeType() {
143         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
144
145         DmaapNode dmaapNode = new DmaapNode();
146         dmaapNode.createFeedNode(componentSpec, new TreeMap<>(), "");
147
148         assertEquals(FEED, dmaapNode.getType());
149     }
150
151     @Test
152     public void feedNodePropertiesShouldHaveUseExistingField() {
153         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
154
155         DmaapNode dmaapNode = new DmaapNode();
156         dmaapNode.createFeedNode(componentSpec, new TreeMap<>() ,"");
157
158         assertTrue(dmaapNode.getProperties().getUseExisting());
159     }
160
161     @Test
162     public void createTopicNodeShouldSetTopicNodeType() {
163
164         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
165
166         DmaapNode dmaapNode = new DmaapNode();
167         dmaapNode.createTopicNode(componentSpec, new TreeMap<>(), "");
168
169         assertEquals(TOPIC, dmaapNode.getType());
170     }
171
172     @Test
173     public void topicNodePropertiesShouldNotHaveUseExistingField() {
174         ComponentSpec componentSpec = getSpiedComponentSpecWithoutRelationships();
175
176         DmaapNode dmaapNode = new DmaapNode();
177         dmaapNode.createTopicNode(componentSpec, new TreeMap<>() ,"");
178
179         assertNull(dmaapNode.getProperties().getUseExisting());
180     }
181
182     private Publishes[] createSamplePublishes(String type) {
183         Publishes publishes = new Publishes();
184
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);
190
191         return new Publishes[]{publishes};
192     }
193
194     private Subscribes[] createSampleSubscribes(String type) {
195         Subscribes subscribes = new Subscribes();
196
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);
202
203         return new Subscribes[]{subscribes};
204     }
205
206     private ComponentSpec getSpiedComponentSpecWithoutRelationships() {
207         ComponentSpec baseComponentSpec = new ComponentSpec();
208         baseComponentSpec.createComponentSpecFromString(new TestComponentSpec().getComponentSpecAsString());
209         ComponentSpec componentSpec = spy(baseComponentSpec);
210
211         Streams streams = new Streams();
212         streams.setSubscribes(new Subscribes[0]);
213         streams.setPublishes(new Publishes[0]);
214         when(componentSpec.getStreams()).thenReturn(streams);
215
216         Auxilary auxilary = spy(baseComponentSpec.getAuxilary());
217         when(auxilary.getDatabases()).thenReturn(null);
218
219         when(componentSpec.getAuxilary()).thenReturn(auxilary);
220         when(componentSpec.getPolicyInfo()).thenReturn(null);
221
222         return componentSpec;
223     }
224 }