1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/**
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.aai.cacher.dmaap.consumer;
import com.att.nsa.mr.client.MRConsumer;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import de.bwaldvogel.mongo.MongoServer;
import de.bwaldvogel.mongo.backend.memory.MemoryBackend;
import de.flapdoodle.embed.mongo.MongodProcess;
import org.hamcrest.Matchers;
import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.onap.aai.cacher.common.MongoHelperSingleton;
import org.onap.aai.cacher.egestion.printer.PayloadPrinterService;
import org.onap.aai.cacher.injestion.parser.InjestionTestComponent;
import org.onap.aai.cacher.injestion.parser.PayloadParserService;
import org.onap.aai.cacher.service.helper.RestClientHelperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@Configuration
@ContextConfiguration(classes = {InjestionTestComponent.class, AAIEventConsumerTest.class})
public class AAIEventConsumerTest {
private static final String DB_NAME = AAIEventConsumerTest.class.getSimpleName();
private static MongoDatabase mongoDb;
private static RestClientHelperService restClientHelperService;
private static DB db;
private static MongodProcess mongod;
private static MongoClient mongoC;
private AAIEventConsumer aaiEventConsumer;
@Autowired
private AAIDmaapEventProcessor aaiDmaapEventProcessor;
@Bean
public DB db() {
return db;
}
@Bean
public MongoDatabase mongoDatabase() {
return mongoDb;
}
@Bean
public RestClientHelperService restClientHelperService() {
return restClientHelperService;
}
@Bean
public MongoHelperSingleton mongoHelperSingleton(DB db, MongoDatabase mongoDb) {
return new MongoHelperSingleton(db, mongoDb);
}
@Bean
public AAIDmaapEventProcessor aaiDmaapEventProcessor(MongoHelperSingleton mongoHelperSingleton, PayloadParserService payloadParserService, PayloadPrinterService payloadPrinterService) {
return new AAIDmaapEventProcessor(mongoHelperSingleton, payloadParserService, payloadPrinterService);
}
MRConsumer client;
private String validEventMessage = "{'cambria.partition':'AAI','event-header':{'severity':'NORMAL','entity-type':'pserver','top-entity-type':'pserver','entity-link':'/aai/v14/cloud-infrastructure/pservers/pserver/pserver-1','event-type':'AAI-EVENT','domain':'JUNIT','action':'CREATE','sequence-number':'0','id':'0c3b336d-6554-4ddf-a4d7-90f97876a966','source-name':'JUNIT','version':'v14','timestamp':'20180209-21:02:20:344'},'entity':{'hostname':'pserver-1','in-maint':false}}";
private String validHeldEventMessage = "{'cambria.partition':'AAI','event-header':{'severity':'NORMAL','entity-type':'pserver','top-entity-type':'pserver','entity-link':'/aai/v14/cloud-infrastructure/pservers/pserver/pserver-1','event-type':'AAI-EVENT','domain':'JUNIT','action':'CREATE','sequence-number':'0','id':'0c3b336d-6554-4ddf-a4d7-90f97876a966','source-name':'JUNIT','version':'v14','timestamp':'20180209-21:02:20:344'},'entity':{'hostname':'pserver-1','in-maint':false}}";
DmaapConsumerSingleton singleton = DmaapConsumerSingleton.getInstance();
List<String> eventMessageList = new ArrayList<>();
@BeforeClass
public static void setup() throws IOException, InterruptedException {
MongoServer mongoServer = new MongoServer(new MemoryBackend());
InetSocketAddress serverAddress = mongoServer.bind();
MongoClient client = new MongoClient(new ServerAddress(serverAddress));
mongoDb = client.getDatabase(DB_NAME);
db = client.getDB(DB_NAME);
}
@Before
public void init() throws Exception {
eventMessageList.add(validEventMessage);
aaiEventConsumer = new AAIEventConsumer("aaiDmaaPEventConsumer.properties", true);
Properties prop = aaiEventConsumer.getDmaapEventConsumerProperties();
assertThat("dmaap group is generated", prop.getProperty("group"), Matchers.startsWith("cacher-"));
assertNotEquals("dmaap id generated", "NA", prop.getProperty("id"));
client = Mockito.mock(MRConsumer.class);
aaiEventConsumer.setConsumer(client);
}
@AfterClass
public static void tearDown() {
if (mongod != null && mongod.isProcessRunning()) {
mongod.stop();
}
}
@After
public void cleanup() {
final List<String> collectionNames = new ArrayList<>();
mongoDb.listCollections().iterator().forEachRemaining(document -> collectionNames.add(document.getString("name")));
collectionNames.forEach(collectionName -> mongoDb.getCollection(collectionName).drop());
}
@Test
public void startProcessing() throws IOException, Exception {
Mockito.when(client.fetch()).thenReturn(eventMessageList);
aaiEventConsumer.startProcessing(aaiDmaapEventProcessor);
}
@Test
public void startProcessingWaitWithHeldEventMessage() throws IOException, Exception {
singleton.setIsInitialized(true);
singleton.setFirstEventMessage(validHeldEventMessage);
Mockito.when(client.fetch()).thenReturn(eventMessageList);
aaiEventConsumer.startProcessing(aaiDmaapEventProcessor);
}
@Test
public void startProcessingNoWaitWithHeldEventMessage() throws IOException, Exception {
singleton.setProcessEvents(true);
singleton.setFirstEventMessage(validHeldEventMessage);
Mockito.when(client.fetch()).thenReturn(eventMessageList);
aaiEventConsumer.startProcessing(aaiDmaapEventProcessor);
}
}
|