Przeglądaj źródła

AMBARI-730. Add unit tests for jersey apis on the server. (mahadev)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/branches/AMBARI-666@1384625 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 12 lat temu
rodzic
commit
b07fc30275

+ 2 - 0
AMBARI-666-CHANGES.txt

@@ -12,6 +12,8 @@ AMBARI-666 branch (unreleased changes)
 
   NEW FEATURES
 
+  AMBARI-730. Add unit tests for jersey apis on the server. (mahadev)
+
   AMBARI-725. Add commandstatus/result/error objects into the rest API between
   server and agent. (mahadev)
 

+ 9 - 2
ambari-project/pom.xml

@@ -114,7 +114,7 @@
         <artifactId>postgresql</artifactId>
         <version>9.1-901.jdbc4</version>
       </dependency>
-     <dependency>
+      <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-core</artifactId>
         <version>1.8.5</version>
@@ -171,7 +171,13 @@
       </dependency>
       <dependency>
         <groupId>com.sun.jersey.jersey-test-framework</groupId>
-        <artifactId>jersey-test-framework-external</artifactId>
+        <artifactId>jersey-test-framework-core</artifactId>
+        <version>1.8</version>
+        <scope>test</scope>
+      </dependency>
+      <dependency>
+        <groupId>com.sun.jersey.jersey-test-framework</groupId>
+        <artifactId>jersey-test-framework-grizzly2</artifactId>
         <version>1.8</version>
       </dependency>
       <dependency>
@@ -233,6 +239,7 @@
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
+          <redirectTestOutputToFile>true</redirectTestOutputToFile>
           <forkMode>always</forkMode>
           <forkedProcessTimeoutInSeconds>900</forkedProcessTimeoutInSeconds>
         </configuration>

+ 7 - 2
ambari-server/pom.xml

@@ -125,13 +125,18 @@
       <groupId>com.sun.jersey.contribs</groupId>
       <artifactId>jersey-multipart</artifactId>
     </dependency>
-     <dependency>
+    <dependency>
       <groupId>com.sun.jersey.contribs</groupId>
       <artifactId>jersey-guice</artifactId>
     </dependency>
     <dependency>
       <groupId>com.sun.jersey.jersey-test-framework</groupId>
-      <artifactId>jersey-test-framework-external</artifactId>
+      <artifactId>jersey-test-framework-core</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.sun.jersey.jersey-test-framework</groupId>
+      <artifactId>jersey-test-framework-grizzly2</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/agent/HeartBeatHandler.java

@@ -32,7 +32,7 @@ public class HeartBeatHandler {
     HeartBeatResponse response = new HeartBeatResponse();
     response.setAgentCommands(new ArrayList<AgentCommand>());
     response.setClusterId("test");
-    response.setResponseId(999L);
+    response.setResponseId(0L);
     return response;    
   }
   

+ 7 - 4
ambari-server/src/main/java/org/apache/ambari/server/agent/Register.java

@@ -79,9 +79,12 @@ public class Register {
 
   @Override
   public String toString() {
-    return "responseId=" + responseId + "\n" +
-           "timestamp=" + timestamp + "\n" +
-           "hostname="  + hostname + "\n" +
-           "hardwareprofile=" + this.hardwareProfile.toString();
+    String ret = "responseId=" + responseId + "\n" +
+             "timestamp=" + timestamp + "\n" +
+             "hostname="  + hostname + "\n";
+      
+    if (hardwareProfile != null) 
+      ret = ret + "hardwareprofile=" + this.hardwareProfile.toString();
+    return ret;
   }
 }

+ 108 - 0
ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java

@@ -0,0 +1,108 @@
+/**
+ * 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.ambari.server.agent;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import javax.ws.rs.core.MediaType;
+
+import junit.framework.Assert;
+
+import org.apache.ambari.server.agent.rest.AgentResource;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.junit.Test;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.spi.container.servlet.ServletContainer;
+import com.sun.jersey.test.framework.JerseyTest;
+import com.sun.jersey.test.framework.WebAppDescriptor;
+
+public class AgentResourceTest extends JerseyTest {
+  static String PACKAGE_NAME = "org.apache.ambari.server.agent.rest";
+  private static Log LOG = LogFactory.getLog(AgentResourceTest.class);
+  HeartBeatHandler handler;
+  Injector injector;
+  
+  public AgentResourceTest() {
+    super(new WebAppDescriptor.Builder(PACKAGE_NAME).servletClass(ServletContainer.class)
+        .build());
+  }
+  
+  public class MockModule extends AbstractModule {
+
+    @Override
+    protected void configure() {
+      requestStaticInjection(AgentResource.class);
+    }    
+  }
+  
+  @Override
+  public void setUp() throws Exception {
+    super.setUp();
+    handler = mock(HeartBeatHandler.class);
+    injector = Guice.createInjector(new MockModule());
+    injector.injectMembers(handler);
+  }
+  
+  private JSONObject createDummyJSONRegister() throws JSONException {
+    JSONObject json = new JSONObject();
+    json.append("responseId" , -1);
+    json.append("timestamp" , System.currentTimeMillis());
+    json.append("hostname",   "dummyHost");
+    return json;
+  }
+  
+  private JSONObject createDummyHeartBeat() throws JSONException {
+    JSONObject json = new JSONObject();
+    json.put("responseId", -1);
+    json.put("timestamp" , System.currentTimeMillis());
+    json.put("hostname", "dummyHost");
+    return json;
+  }
+  
+  @Test
+  public void agentRegistration() throws UniformInterfaceException, JSONException {
+    RegistrationResponse response;
+    WebResource webResource = resource();
+    response = webResource.path("/register/dummyhost").type(MediaType.APPLICATION_JSON)
+      .post(RegistrationResponse.class, createDummyJSONRegister());
+    LOG.info("Returned from Server " + response.getResponseStatus());
+    Assert.assertEquals(response.getResponseStatus(), RegistrationStatus.OK);
+  }
+  
+  @Test
+  public void agentHeartBeat() throws UniformInterfaceException, JSONException {
+    HeartBeatResponse response;
+    WebResource resource = resource();
+    response = resource.path("/heartbeat/dummyhost").type(MediaType.APPLICATION_JSON)
+        .post(HeartBeatResponse.class, createDummyHeartBeat());
+    LOG.info("Returned from Server: " + "clusterid = " + response.getClusterId() 
+        + " responseid=" +   response.getResponseId());
+    Assert.assertEquals(response.getResponseId(), 0L);
+  }
+}

+ 19 - 0
ambari-server/src/test/resources/log4j.properties

@@ -0,0 +1,19 @@
+#   Licensed 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.
+
+# log4j configuration used during build and unit tests
+
+log4j.rootLogger=info,stdout
+log4j.threshhold=ALL
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2} (%F:%M(%L)) - %m%n