anuragaw closed pull request #2891: Fizz buzz test api
URL: https://github.com/apache/cloudstack/pull/2891
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/api/src/com/cloud/fizzbuzz/FizzBuzzService.java 
b/api/src/com/cloud/fizzbuzz/FizzBuzzService.java
new file mode 100644
index 00000000000..2a9cc3d4c23
--- /dev/null
+++ b/api/src/com/cloud/fizzbuzz/FizzBuzzService.java
@@ -0,0 +1,12 @@
+package com.cloud.fizzbuzz;
+
+public interface FizzBuzzService {
+    /**
+     * Takes in an argument and returns fizz if n is divisible by 3, buzz if n 
is divisible by 5, fizzbuzz if n is
+     * divisible by both. If null is passed we use the active number of VMs 
instead of n.
+     *
+     * @param n an argument from user of API
+     * @return a string based on logic above
+     */
+    String getDisplayText(Integer n);
+}
diff --git 
a/api/src/org/apache/cloudstack/api/command/user/fizzbuzz/FizzBuzzCmd.java 
b/api/src/org/apache/cloudstack/api/command/user/fizzbuzz/FizzBuzzCmd.java
new file mode 100644
index 00000000000..d0401c4f569
--- /dev/null
+++ b/api/src/org/apache/cloudstack/api/command/user/fizzbuzz/FizzBuzzCmd.java
@@ -0,0 +1,74 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+package org.apache.cloudstack.api.command.user.fizzbuzz;
+
+import com.cloud.fizzbuzz.FizzBuzzService;
+import com.cloud.user.Account;
+import org.apache.cloudstack.acl.RoleType;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.response.SuccessResponse;
+import org.apache.log4j.Logger;
+
+import javax.inject.Inject;
+
+@APICommand(name = FizzBuzzCmd.APINAME,
+        description = "the classic fizzBuzz test",
+        responseObject = SuccessResponse.class,
+        requestHasSensitiveInfo = false,
+        responseHasSensitiveInfo = false,
+        since = "4.11",
+        authorized = {RoleType.User})
+public class FizzBuzzCmd extends BaseCmd {
+    public static final Logger s_logger = 
Logger.getLogger(FizzBuzzCmd.class.getName());
+
+    public static final String APINAME = "fizzBuzz";
+
+    @Parameter(name = "number",
+            type = CommandType.INTEGER,
+            required = false,
+            description = "A number passed by user to do FizzBuzz test on.")
+    private Integer number;
+
+    @Inject
+    private FizzBuzzService _fizzBuzzService;
+
+    // MARK - Implementation
+
+    @Override
+    public String getCommandName() {
+        return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX;
+    }
+
+    @Override
+    public long getEntityOwnerId() {
+        return Account.ACCOUNT_ID_SYSTEM;
+    }
+
+    @Override
+    public void execute() {
+        final String responseText = 
_fizzBuzzService.getDisplayText(this.number);
+        s_logger.debug("FizzBuzz: " + number + " response " + responseText);
+        final SuccessResponse response = new SuccessResponse(getCommandName());
+        response.setDisplayText(responseText);
+        this.setResponseObject(response);
+    }
+
+}
+
diff --git 
a/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml
 
b/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml
index c7715a866c4..d98c6febf4a 100644
--- 
a/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml
+++ 
b/server/resources/META-INF/cloudstack/core/spring-server-core-managers-context.xml
@@ -298,4 +298,6 @@
     <bean id="indirectAgentLBService" 
class="org.apache.cloudstack.agent.lb.IndirectAgentLBServiceImpl" />
 
     <bean id="directDownloadManager" 
class="org.apache.cloudstack.direct.download.DirectDownloadManagerImpl" />
+
+    <bean id="fizzBuzzService" 
class="org.apache.cloudstack.fizzbuzz.FizzBuzzServiceImpl"/>
 </beans>
diff --git a/server/src/com/cloud/api/ResponseObjectTypeAdapter.java 
b/server/src/com/cloud/api/ResponseObjectTypeAdapter.java
index 44baedc933b..2fee5514a22 100644
--- a/server/src/com/cloud/api/ResponseObjectTypeAdapter.java
+++ b/server/src/com/cloud/api/ResponseObjectTypeAdapter.java
@@ -38,6 +38,7 @@ public JsonElement serialize(ResponseObject responseObj, Type 
typeOfResponseObj,
 
         if (responseObj instanceof SuccessResponse) {
             obj.addProperty("success", 
((SuccessResponse)responseObj).getSuccess());
+            obj.addProperty("text", 
((SuccessResponse)responseObj).getDisplayText());
             return obj;
         } else if (responseObj instanceof ExceptionResponse) {
             obj.addProperty("errorcode", 
((ExceptionResponse)responseObj).getErrorCode());
diff --git a/server/src/com/cloud/api/response/ApiResponseSerializer.java 
b/server/src/com/cloud/api/response/ApiResponseSerializer.java
index 2b9717de1e2..b0587db6a4d 100644
--- a/server/src/com/cloud/api/response/ApiResponseSerializer.java
+++ b/server/src/com/cloud/api/response/ApiResponseSerializer.java
@@ -132,8 +132,8 @@ public static String toJSONSerializedString(ResponseObject 
result, StringBuilder
                     log.append("}");
                 }
             } else if (result instanceof SuccessResponse) {
-                
sb.append("{\"success\":\"").append(((SuccessResponse)result).getSuccess()).append("\"}");
-                
log.append("{\"success\":\"").append(((SuccessResponse)result).getSuccess()).append("\"}");
+                
sb.append("{\"success\":\"").append(((SuccessResponse)result).getSuccess()).append("\",\"text\":\"").append(((SuccessResponse)result).getDisplayText()).append("\"}");
+                
log.append("{\"success\":\"").append(((SuccessResponse)result).getSuccess()).append("\",\"text\":\"").append(((SuccessResponse)result).getDisplayText()).append("\"}");
             } else if (result instanceof ExceptionResponse) {
                 String jsonErrorText = responseBuilder.toJson(result);
                 jsonErrorText = unescape(jsonErrorText);
diff --git a/server/src/com/cloud/server/StatsCollector.java 
b/server/src/com/cloud/server/StatsCollector.java
index b66fa5f0600..1e6a214899e 100644
--- a/server/src/com/cloud/server/StatsCollector.java
+++ b/server/src/com/cloud/server/StatsCollector.java
@@ -593,6 +593,10 @@ public VmStats getVmStats(long id) {
         return _VmStats.get(id);
     }
 
+    public int getNumberOfVms() {
+        return _VmStats.size();
+    }
+
     class VmDiskStatsUpdaterTask extends ManagedContextRunnable {
         @Override
         protected void runInContext() {
diff --git a/server/src/org/apache/cloudstack/fizzbuzz/FizzBuzzServiceImpl.java 
b/server/src/org/apache/cloudstack/fizzbuzz/FizzBuzzServiceImpl.java
new file mode 100644
index 00000000000..2899870b704
--- /dev/null
+++ b/server/src/org/apache/cloudstack/fizzbuzz/FizzBuzzServiceImpl.java
@@ -0,0 +1,33 @@
+package org.apache.cloudstack.fizzbuzz;
+
+import com.cloud.fizzbuzz.FizzBuzzService;
+import com.cloud.server.StatsCollector;
+import com.cloud.utils.component.ManagerBase;
+import com.cloud.utils.component.PluggableService;
+import org.apache.cloudstack.api.command.user.fizzbuzz.FizzBuzzCmd;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+public class FizzBuzzServiceImpl extends ManagerBase implements 
FizzBuzzService, PluggableService {
+    @Override
+    public String getDisplayText(Integer n) {
+        return (n == null) ? getFizzBuzzOnVms() : getFizzBuzzOnN(n);
+    }
+
+    @Override
+    public List<Class<?>> getCommands() {
+        final List<Class<?>> cmdList = new ArrayList<>();
+        cmdList.add(FizzBuzzCmd.class);
+        return cmdList;
+    }
+
+    private String getFizzBuzzOnVms() {
+        return getFizzBuzzOnN(StatsCollector.getInstance().getNumberOfVms());
+    }
+
+    private String getFizzBuzzOnN(int n) {
+        return (n % 15 == 0) ? "fizzbuzz" : (n % 3 == 0) ? "fizz" : (n % 5 == 
0) ? "buzz" : String.valueOf(new Random().nextInt(99) + 1);
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to