Update for SNI Chcecking
[policy/apex-pdp.git] / examples / examples-grpc / src / test / java / org / onap / policy / apex / examples / grpc / GrpcTestServerSim.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020,2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 AT&T Intellectual Property. 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  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.examples.grpc;
23
24 import java.io.IOException;
25 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
26 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
27 import org.onap.policy.common.gson.GsonMessageBodyHandler;
28 import org.onap.policy.common.utils.network.NetworkUtil;
29 import org.onap.policy.simulators.CdsSimulator;
30
31 /**
32  * The Class GrpcTestServerSim that manages test servers for REST and gRPC requests for the test.
33  */
34 public class GrpcTestServerSim {
35     private static final String HOST = "localhost";
36     private HttpServletServer restServer;
37     private CdsSimulator grpcServer;
38
39     /**
40      * Instantiates a new REST simulator for DMaaP requests.
41      *
42      * @throws InterruptedException interrupted exception
43      * @throws IOException io exception
44      */
45     public GrpcTestServerSim() throws InterruptedException, IOException {
46         int restServerPort = 54321;
47         restServer = HttpServletServerFactoryInstance.getServerFactory().build("GrpcTestRestSimEndpoint", false, HOST,
48             restServerPort, false, "/GrpcTestRestSim", false, false);
49         restServer.addServletClass(null, GrpcTestRestSimEndpoint.class.getName());
50         restServer.setSerializationProvider(GsonMessageBodyHandler.class.getName());
51         restServer.start();
52         if (!NetworkUtil.isTcpPortOpen(HOST, restServerPort, 50, 200L)) {
53             throw new IllegalStateException("port " + restServerPort + " is still not in use");
54         }
55
56         int grpcServerPort = 54322;
57         grpcServer = new CdsSimulator(HOST, grpcServerPort);
58         grpcServer.start();
59         if (!NetworkUtil.isTcpPortOpen(HOST, grpcServerPort, 50, 200L)) {
60             throw new IllegalStateException("port " + grpcServerPort + " is still not in use");
61         }
62     }
63
64     /**
65      * Tear down.
66      */
67     public void tearDown() {
68         if (restServer != null) {
69             restServer.stop();
70         }
71         if (grpcServer != null) {
72             grpcServer.stop();
73         }
74     }
75 }