Raise JUnit coverage common-be
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ConfigMgrServlet.java
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.servlets;
22
23 import javax.servlet.ServletContext;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MediaType;
34
35 import org.openecomp.sdc.be.config.Configuration;
36 import org.openecomp.sdc.be.config.ConfigurationManager;
37 import org.openecomp.sdc.common.api.Constants;
38 import org.openecomp.sdc.common.servlets.BasicServlet;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.jcabi.aspects.Loggable;
43
44 /**
45  * Root resource (exposed at "/" path)
46  */
47 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
48 @Path("/configmgr")
49 public class ConfigMgrServlet extends BasicServlet {
50
51     private static final Logger log = LoggerFactory.getLogger(ConfigMgrServlet.class);
52
53     @GET
54     @Path("/get")
55     @Produces(MediaType.APPLICATION_JSON)
56     public String getConfig(@Context final HttpServletRequest request, @QueryParam("type") String type) {
57
58         String result = null;
59
60         ServletContext context = request.getSession().getServletContext();
61
62         ConfigurationManager configurationManager = (ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR);
63
64         if (type == null || type.equals("configuration")) {
65
66             Configuration configuration = configurationManager.getConfiguration();
67             if (configuration == null) {
68                 log.warn("Configuration of type {} was not found", Configuration.class);
69             } else {
70                 log.info("The value returned from getConfig is {}", configuration);
71
72                 result = gson.toJson(configuration);
73
74             }
75         }
76
77         return result;
78
79     }
80
81     @POST
82     @Path("/set1")
83     @Produces(MediaType.TEXT_PLAIN)
84     @Consumes(MediaType.APPLICATION_JSON)
85     public String setConfig1(@Context final HttpServletRequest request, Configuration configuration) {
86
87         log.debug("{}", configuration);
88
89         return "ok";
90
91     }
92
93     @POST
94     @Path("/set2")
95     @Produces(MediaType.TEXT_PLAIN)
96     @Consumes(MediaType.APPLICATION_JSON)
97     public void setConfig2(@Context final HttpServletRequest request, Configuration configuration) {
98
99         log.debug("{}", configuration);
100
101     }
102
103     @PUT
104     @Path("/setput1")
105     @Produces(MediaType.TEXT_PLAIN)
106     @Consumes(MediaType.APPLICATION_JSON)
107     public String setConfig3(@Context final HttpServletRequest request, Configuration configuration) {
108
109         log.debug("{}", configuration);
110
111         return "ok";
112
113     }
114
115     @PUT
116     @Path("/setput2")
117     @Produces(MediaType.TEXT_PLAIN)
118     @Consumes(MediaType.APPLICATION_JSON)
119     public void setConfig4(@Context final HttpServletRequest request, Configuration configuration) {
120
121         log.debug("{}", configuration);
122
123     }
124
125 }