Merge "added a testcase"
[dmaap/messagerouter/mirroragent.git] / src / test / java / com / att / nsa / dmaapMMAgent / TestMirrorMakerAgent.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
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  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22
23 package com.att.nsa.dmaapMMAgent;
24
25 import static org.junit.Assert.*;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28
29 import java.util.ArrayList;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.powermock.modules.junit4.PowerMockRunner;
38
39 import com.att.nsa.dmaapMMAgent.dao.ListMirrorMaker;
40 import com.att.nsa.dmaapMMAgent.dao.MirrorMaker;
41 import com.google.gson.Gson;
42 import com.google.gson.internal.LinkedTreeMap;
43
44 @RunWith(PowerMockRunner.class)
45 public class TestMirrorMakerAgent {
46         MirrorMakerAgent mirrorMakerAgent = new MirrorMakerAgent();
47         ListMirrorMaker listMirrorMaker = new ListMirrorMaker();
48         MirrorMaker mirrorMaker = new MirrorMaker();
49         MirrorMaker mirrorMaker2 = new MirrorMaker();
50         ArrayList<MirrorMaker> listsMirrorMaker = new ArrayList<MirrorMaker>();
51         Gson g = new Gson();
52         @Mock
53         ListMirrorMaker mirrorMakers;
54         @InjectMocks
55         MirrorMakerAgent agent;
56
57         @Before
58         public void setUp() {
59                 MockitoAnnotations.initMocks(this);
60         }
61
62         @Test
63         public void testcheckStartup() {
64                 String currentDirectory = System.getProperty("user.dir");
65                 String MMAGENTHOME = currentDirectory + "/src/test/resources/";
66                 String parameters[] = { "-encrypt", "test" };
67                 String args[] = null;
68
69                 System.setProperty("MMAGENTHOME", MMAGENTHOME);
70                 mirrorMakerAgent.main(args);
71
72         }
73
74         @Test
75         public void testReadAgentTopics() {
76                 MirrorMakerAgent agent = spy(new MirrorMakerAgent());
77                 when(agent.subscribeTopic("60000")).thenReturn(null);
78                 agent.exitLoop = true;
79                 agent.readAgentTopic();
80         }
81
82         @Test
83         public void testReadCreateMirrorMaker() {
84                 agent = spy(new MirrorMakerAgent());
85                 when(agent.publishTopic(
86                                 "{\"messageID\":\"test\",\"listMirrorMaker\":[{\"name\":\"test\",\"consumer\":\"test\",\"producer\":\"test\",\"status\":\"STOPPED\"}]}"))
87                                                 .thenReturn(null);
88                 String topicMessage = "{ messageID:\"test\", createMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\"}}";
89                 LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class);
90                 agent.readAgent(object, topicMessage);
91
92         }
93
94         @Test
95         public void testReadUpdateMirrorMaker() {
96                 agent = spy(new MirrorMakerAgent());
97                 when(agent.publishTopic(
98                                 "{\"messageID\":\"test\",\"listMirrorMaker\":[{\"name\":\"test\",\"consumer\":\"test\",\"producer\":\"test\",\"status\":\"STOPPED\"}]}"))
99                                                 .thenReturn(null);
100                 String topicMessage = "{ messageID:\"test\", updateMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\"}}";
101                 LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class);
102                 testReadCreateMirrorMaker();
103                 agent.readAgent(object, topicMessage);
104
105         }
106
107         @Test
108         public void testReadDeleteMirrorMaker() {
109                 agent = spy(new MirrorMakerAgent());
110                 when(agent.publishTopic(
111                                 "{\"messageID\":\"test\",\"listMirrorMaker\":[{\"name\":\"test\",\"consumer\":\"test\",\"producer\":\"test\",\"status\":\"STOPPED\"}]}"))
112                                                 .thenReturn(null);
113                 String topicMessage = "{ messageID:\"test\", deleteMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\",  whitelist:\"test\",status:\"test\" }}";
114                 LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class);
115                 testReadCreateMirrorMaker();
116                 agent.readAgent(object, topicMessage);
117
118         }
119
120         @Test
121         public void testReadListMirrorMaker() {
122                 agent = spy(new MirrorMakerAgent());
123                 when(agent.publishTopic(
124                                 "{\"messageID\":\"test\",\"listMirrorMaker\":[{\"name\":\"test\",\"consumer\":\"test\",\"producer\":\"test\",\"status\":\"STOPPED\"}]}"))
125                                                 .thenReturn(null);
126                 String topicMessage = "{ messageID:\"test\", listAllMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\",  whitelist:\"test\",status:\"test\" }}";
127                 LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class);
128                 testReadCreateMirrorMaker();
129                 agent.readAgent(object, topicMessage);
130
131         }
132
133         @Test
134         public void testReadWhitelistMirrorMaker() {
135                 agent = spy(new MirrorMakerAgent());
136                 when(agent.publishTopic(
137                                 "{\"messageID\":\"test\",\"listMirrorMaker\":[{\"name\":\"test\",\"consumer\":\"test\",\"producer\":\"test\",\"status\":\"STOPPED\"}]}"))
138                                                 .thenReturn(null);
139                 String topicMessage = "{ messageID:\"test\", updateWhiteList: {   name:\"test\",   consumer:\"test\",  producer:\"test\",  whitelist:\"test\",status:\"test\" }}";
140                 LinkedTreeMap<?, ?> object = g.fromJson(topicMessage, LinkedTreeMap.class);
141                 testReadCreateMirrorMaker();
142                 agent.readAgent(object, topicMessage);
143
144         }
145
146         @Test
147         public void testCreateMirrorMaker() {
148                 mirrorMaker.setConsumer("consumer");
149                 mirrorMaker.setName("MirrorMaker1");
150                 mirrorMaker.setProducer("producer");
151                 mirrorMaker.setStatus("200");
152                 mirrorMaker.setWhitelist("whitelist");
153
154                 mirrorMaker2.setConsumer("consumer");
155                 mirrorMaker2.setName("MirrorMaker2");
156                 mirrorMaker2.setProducer("producer");
157                 mirrorMaker2.setStatus("200");
158                 mirrorMaker2.setWhitelist("whitelist");
159
160                 listsMirrorMaker.add(mirrorMaker2);
161                 listMirrorMaker.setListMirrorMaker(listsMirrorMaker);
162
163                 mirrorMakerAgent.mirrorMakers = listMirrorMaker;
164
165                 mirrorMakerAgent.createMirrorMaker(mirrorMaker);
166
167                 assertEquals(2, mirrorMakerAgent.mirrorMakers.getListMirrorMaker().size());
168         }
169
170 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use