b3a254f531040f85f5201853f402bb50ff4ee64c
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.distribution.engine;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26
27 import java.util.List;
28
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.openecomp.sdc.be.components.BaseConfDependentTest;
33 import org.openecomp.sdc.be.components.distribution.engine.CambriaErrorResponse;
34 import org.openecomp.sdc.be.components.distribution.engine.CambriaHandler;
35 import org.openecomp.sdc.be.components.distribution.engine.DistributionEngineClusterHealth;
36 import org.openecomp.sdc.be.components.distribution.engine.UebHealthCheckCall;
37 import org.openecomp.sdc.be.config.ConfigurationManager;
38 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
39
40 import com.google.gson.Gson;
41 import com.google.gson.GsonBuilder;
42
43 public class DistributionEngineHealthCheckTest extends BaseConfDependentTest {
44
45         @Mock
46         private CambriaHandler cambriaHandler = Mockito.mock(CambriaHandler.class);
47
48         DistributionEngineClusterHealth distributionEngineClusterHealth = new DistributionEngineClusterHealth();
49
50         Gson gson = new Gson();
51
52         Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
53
54         //
55         // @Test
56         // public void validateDownWhenEnvAreDown() {
57         //
58         // Map<String, AtomicBoolean> envNamePerStatus = new HashMap<>();
59         // envNamePerStatus.put("PROD1", new AtomicBoolean(false));
60         // envNamePerStatus.put("PROD2", new AtomicBoolean(false));
61         //
62         // distributionEngineClusterHealth.startHealthCheckTask(envNamePerStatus);
63         //
64         // HealthCheckInfo healthCheckInfo =
65         // distributionEngineClusterHealth.getHealthCheckInfo();
66         // assertEquals("verify down", HealthCheckStatus.DOWN,
67         // healthCheckInfo.getHealthCheckStatus());
68         // assertEquals("verify DE component", HealthCheckComponent.DE,
69         // healthCheckInfo.getHealthCheckComponent());
70         //
71         // }
72
73         @Test
74         public void validateUpWhenQuerySucceed() {
75
76                 // Map<String, AtomicBoolean> envNamePerStatus = new HashMap<>();
77                 // envNamePerStatus.put("PROD1", new AtomicBoolean(true));
78                 // envNamePerStatus.put("PROD2", new AtomicBoolean(false));
79                 //
80                 // distributionEngineClusterHealth.startHealthCheckTask(envNamePerStatus,
81                 // false);
82
83                 CambriaErrorResponse cambriaOkResponse = new CambriaErrorResponse(CambriaOperationStatus.OK, 200);
84                 CambriaErrorResponse cambriaErrorResponse = new CambriaErrorResponse(CambriaOperationStatus.INTERNAL_SERVER_ERROR, 500);
85                 CambriaErrorResponse cambriaNotErrorResponse = new CambriaErrorResponse(CambriaOperationStatus.AUTHENTICATION_ERROR, 403);
86
87                 List<String> uebServers = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getUebServers();
88                 if (uebServers.size() >= 2) {
89                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(0)), Mockito.any(String.class))).thenReturn(cambriaOkResponse);
90                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(1)), Mockito.any(String.class))).thenReturn(cambriaOkResponse);
91                 }
92
93                 UebHealthCheckCall healthCheckCall1 = new UebHealthCheckCall(uebServers.get(0), "publicKey");
94                 healthCheckCall1.setCambriaHandler(cambriaHandler);
95                 Boolean call1 = healthCheckCall1.call();
96                 assertTrue("check response okay", call1);
97
98                 UebHealthCheckCall healthCheckCall2 = new UebHealthCheckCall(uebServers.get(1), "publicKey");
99                 healthCheckCall2.setCambriaHandler(cambriaHandler);
100
101                 Boolean call2 = healthCheckCall2.call();
102                 assertTrue("check response okay", call2);
103
104                 if (uebServers.size() >= 2) {
105                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(0)), Mockito.any(String.class))).thenReturn(cambriaErrorResponse);
106                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(1)), Mockito.any(String.class))).thenReturn(cambriaOkResponse);
107                 }
108                 healthCheckCall1 = new UebHealthCheckCall(uebServers.get(0), "publicKey");
109                 healthCheckCall1.setCambriaHandler(cambriaHandler);
110
111                 call1 = healthCheckCall1.call();
112                 assertFalse("check response okay", call1);
113
114                 healthCheckCall2 = new UebHealthCheckCall(uebServers.get(1), "publicKey");
115                 healthCheckCall2.setCambriaHandler(cambriaHandler);
116
117                 call2 = healthCheckCall2.call();
118                 assertTrue("check response okay", call2);
119
120                 if (uebServers.size() >= 2) {
121                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(0)), Mockito.any(String.class))).thenReturn(cambriaErrorResponse);
122                         when(cambriaHandler.getApiKey(Mockito.eq(uebServers.get(1)), Mockito.any(String.class))).thenReturn(cambriaNotErrorResponse);
123                 }
124                 healthCheckCall1 = new UebHealthCheckCall(uebServers.get(0), "publicKey");
125                 healthCheckCall1.setCambriaHandler(cambriaHandler);
126
127                 call1 = healthCheckCall1.call();
128                 assertFalse("check response okay", call1);
129
130                 healthCheckCall2 = new UebHealthCheckCall(uebServers.get(1), "publicKey");
131                 healthCheckCall2.setCambriaHandler(cambriaHandler);
132
133                 call2 = healthCheckCall2.call();
134                 assertTrue("check response okay", call2);
135
136         }
137
138 }