incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maha...@apache.org
Subject svn commit: r1406489 [15/19] - in /incubator/ambari/branches/AMBARI-666: ./ ambari-agent/ ambari-agent/conf/ ambari-agent/conf/unix/ ambari-agent/src/main/puppet/manifestloader/ ambari-agent/src/main/puppet/modules/configgenerator/manifests/ ambari-age...
Date Wed, 07 Nov 2012 08:13:24 GMT
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostComponentServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostComponentServiceTest.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostComponentServiceTest.java
(original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostComponentServiceTest.java
Wed Nov  7 08:13:12 2012
@@ -1,3 +1,22 @@
+/**
+ * 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.api.services;
 
 import org.apache.ambari.server.api.handlers.RequestHandler;
@@ -15,11 +34,7 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertSame;
 
 /**
- * Created with IntelliJ IDEA.
- * User: john
- * Date: 9/12/12
- * Time: 11:56 AM
- * To change this template use File | Settings | File Templates.
+ * Unit tests for HostComponentService.
  */
 public class HostComponentServiceTest {
   @Test
@@ -48,7 +63,8 @@ public class HostComponentServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(true).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.GET, serializedResult, true)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -87,7 +103,8 @@ public class HostComponentServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(true).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.GET, serializedResult, true)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -102,7 +119,7 @@ public class HostComponentServiceTest {
   }
 
   @Test
-  public void testPutHostComponent() {
+  public void testCreateHostComponent() {
     ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
     ResultSerializer resultSerializer = createStrictMock(ResultSerializer.class);
     Object serializedResult = new Object();
@@ -121,13 +138,14 @@ public class HostComponentServiceTest {
     String hostComponentName = "hostComponentName";
 
     // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.POST),
         eq(resourceDef))).andReturn(request);
 
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.POST, serializedResult, false)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -142,7 +160,7 @@ public class HostComponentServiceTest {
   }
 
   @Test
-  public void testPostHostComponent() {
+  public void testUpdateHostComponent() {
     ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
     ResultSerializer resultSerializer = createStrictMock(ResultSerializer.class);
     Object serializedResult = new Object();
@@ -161,13 +179,14 @@ public class HostComponentServiceTest {
     String hostComponentName = "hostComponentName";
 
     // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.POST),
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
         eq(resourceDef))).andReturn(request);
 
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.PUT, serializedResult, false)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -182,6 +201,46 @@ public class HostComponentServiceTest {
   }
 
   @Test
+  public void testUpdateHostComponents() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultSerializer resultSerializer = createStrictMock(ResultSerializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+    String hostName = "hostName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(request.getResultSerializer()).andReturn(resultSerializer);
+    expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.PUT, serializedResult, false)).andReturn(response);
+
+    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    HostComponentService hostComponentService = new TestHostComponentService(resourceDef,
clusterName, hostName, null,
+        requestFactory, responseFactory, requestHandler);
+    assertSame(response, hostComponentService.updateHostComponents("body", httpHeaders, uriInfo));
+
+    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  @Test
   public void testDeleteHostComponent() {
     ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
     ResultSerializer resultSerializer = createStrictMock(ResultSerializer.class);
@@ -207,7 +266,8 @@ public class HostComponentServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.DELETE, serializedResult, false)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java
(original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java
Wed Nov  7 08:13:12 2012
@@ -1,3 +1,22 @@
+/**
+ * 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.api.services;
 
 
@@ -15,11 +34,7 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertSame;
 
 /**
- * Created with IntelliJ IDEA.
- * User: john
- * Date: 6/21/12
- * Time: 2:06 PM
- * To change this template use File | Settings | File Templates.
+ * Unit tests for HostService.
  */
 public class HostServiceTest {
 
@@ -48,7 +63,8 @@ public class HostServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(true).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.GET, serializedResult, true)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -85,7 +101,8 @@ public class HostServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(true).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.GET, serializedResult, true)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -117,13 +134,14 @@ public class HostServiceTest {
     String hostName = "hostName";
 
     // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.POST),
         eq(resourceDef))).andReturn(request);
 
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.POST, serializedResult, false)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -155,13 +173,14 @@ public class HostServiceTest {
     String hostName = "hostName";
 
     // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.POST),
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
         eq(resourceDef))).andReturn(request);
 
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.PUT, serializedResult, false)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -175,6 +194,44 @@ public class HostServiceTest {
   }
 
   @Test
+  public void testUpdateHosts() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultSerializer resultSerializer = createStrictMock(ResultSerializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(request.getResultSerializer()).andReturn(resultSerializer);
+    expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.PUT, serializedResult, false)).andReturn(response);
+
+    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    HostService hostService = new TestHostService(resourceDef, clusterName, null, requestFactory,
responseFactory, requestHandler);
+    assertSame(response, hostService.updateHosts("body", httpHeaders, uriInfo));
+
+    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  @Test
   public void testDeleteHost() {
     ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
     ResultSerializer resultSerializer = createStrictMock(ResultSerializer.class);
@@ -199,7 +256,8 @@ public class HostServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.DELETE, serializedResult, false)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java
(added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistServiceTest.java
Wed Nov  7 08:13:12 2012
@@ -0,0 +1,94 @@
+/**
+ * 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.api.services;
+
+import java.io.IOException;
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.apache.ambari.server.utils.StageUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codehaus.jettison.json.JSONException;
+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.Client;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+import com.sun.jersey.api.json.JSONConfiguration;
+import com.sun.jersey.spi.container.servlet.ServletContainer;
+import com.sun.jersey.test.framework.JerseyTest;
+import com.sun.jersey.test.framework.WebAppDescriptor;
+
+public class PersistServiceTest extends JerseyTest {
+  static String PACKAGE_NAME = "org.apache.ambari.server.api.services";
+  private static Log LOG = LogFactory.getLog(PersistServiceTest.class);
+  Injector injector;
+  protected Client client;
+
+  public  PersistServiceTest() {
+    super(new WebAppDescriptor.Builder(PACKAGE_NAME).servletClass(ServletContainer.class)
+        .initParam("com.sun.jersey.api.json.POJOMappingFeature", "true")
+        .build());
+  }
+
+  public class MockModule extends AbstractModule {
+
+
+    @Override
+    protected void configure() {
+      requestStaticInjection(PersistKeyValueService.class);
+    }
+  }
+
+  @Override
+  public void setUp() throws Exception {
+    super.setUp();
+    PersistKeyValueImpl impl = new PersistKeyValueImpl();
+    injector = Guice.createInjector(new MockModule());
+    injector.injectMembers(impl);
+  }
+
+  @Test
+  public void testPersist() throws UniformInterfaceException, JSONException,
+    IOException {
+    ClientConfig clientConfig = new DefaultClientConfig();
+    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
+    client = Client.create(clientConfig);
+    WebResource webResource = client.resource("http://localhost:9998/persist");
+    
+    webResource.post("{\"xyx\" : \"t\"}");
+    LOG.info("Done posting to the server");
+    String output = webResource.get(String.class);
+    LOG.info("All key values " + output);
+    Map<String, String> jsonOutput = StageUtils.fromJson(output, Map.class);
+    String value = jsonOutput.get("xyx");
+    Assert.assertEquals("t", value);
+    webResource = client.resource("http://localhost:9998/persist/xyx");
+    output = webResource.get(String.class);
+    Assert.assertEquals("t", output);
+    LOG.info("Value for xyx " + output);
+  }
+}

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/RequestImplTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/RequestImplTest.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/RequestImplTest.java
(original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/RequestImplTest.java
Wed Nov  7 08:13:12 2012
@@ -33,11 +33,19 @@ public class RequestImplTest {
   @Test
   public void testGetQueryPredicate() {
 
-    Request request = new TestRequest(null, null, null, Request.Type.GET, null);
+    String uri = "http://foo.bar.com/api/v1/clusters?foo=bar&orProp1=5|orProp2!=6|orProp3<100&prop!=5&prop2>10&prop3>=20&prop4<500&prop5<=1&fields=field1,category/field2";
+    Request request = new TestRequest(null, null, null, Request.Type.GET, null, uri);
     Predicate predicate = request.getQueryPredicate();
 
     Set<Predicate> setPredicates = new HashSet<Predicate>();
     setPredicates.add(new EqualsPredicate(PropertyHelper.getPropertyId("foo"), "bar"));
+
+    Set<Predicate> setOrPredicates = new HashSet<Predicate>();
+    setOrPredicates.add(new EqualsPredicate(PropertyHelper.getPropertyId("orProp1"), "5"));
+    setOrPredicates.add(new NotPredicate(new EqualsPredicate(PropertyHelper.getPropertyId("orProp2"),
"6")));
+    setOrPredicates.add(new LessPredicate(PropertyHelper.getPropertyId("orProp3"), "100"));
+    setPredicates.add(new OrPredicate(setOrPredicates.toArray(new BasePredicate[3])));
+
     setPredicates.add(new NotPredicate(new EqualsPredicate(PropertyHelper.getPropertyId("prop"),
"5")));
     setPredicates.add(new GreaterPredicate(PropertyHelper.getPropertyId("prop2"), "10"));
     setPredicates.add(new GreaterEqualsPredicate(PropertyHelper.getPropertyId("prop3"), "20"));
@@ -59,7 +67,7 @@ public class RequestImplTest {
 
     replay(uriInfo, mapQueryParams);
 
-    Request request =  new TestRequest(null, null, uriInfo, Request.Type.GET, null);
+    Request request =  new TestRequest(null, null, uriInfo, Request.Type.GET, null, null);
     Map<PropertyId, TemporalInfo> mapFields = request.getFields();
 
     assertEquals(7, mapFields.size());
@@ -98,13 +106,15 @@ public class RequestImplTest {
 
 
   private class TestRequest extends RequestImpl {
-    private TestRequest(HttpHeaders headers, String body, UriInfo uriInfo, Type requestType,
ResourceDefinition resourceDefinition) {
+    private String m_uri;
+    private TestRequest(HttpHeaders headers, String body, UriInfo uriInfo, Type requestType,
ResourceDefinition resourceDefinition, String uri) {
       super(headers, body, uriInfo, requestType, resourceDefinition);
+      m_uri = uri;
     }
 
     @Override
     public String getURI() {
-      return ("http://foo.bar.com/api/v1/clusters?foo=bar&prop!=5&prop2>10&prop3>=20&prop4<500&prop5<=1&fields=field1,category/field2");
+      return m_uri;
     }
   }
 }

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java
(original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java
Wed Nov  7 08:13:12 2012
@@ -1,3 +1,21 @@
+/**
+ * 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.api.services;
 
 import org.apache.ambari.server.api.handlers.RequestHandler;
@@ -15,11 +33,7 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertSame;
 
 /**
- * Created with IntelliJ IDEA.
- * User: john
- * Date: 9/12/12
- * Time: 11:30 AM
- * To change this template use File | Settings | File Templates.
+ * Unit tests for ServiceService.
  */
 public class ServiceServiceTest {
 
@@ -48,7 +62,8 @@ public class ServiceServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(true).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.GET, serializedResult, true)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -85,7 +100,8 @@ public class ServiceServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(true).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.GET, serializedResult, true)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -117,13 +133,14 @@ public class ServiceServiceTest {
     String serviceName = "serviceName";
 
     // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.POST),
         eq(resourceDef))).andReturn(request);
 
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(responseFactory.createResponse(Request.Type.POST, serializedResult, false)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -137,6 +154,44 @@ public class ServiceServiceTest {
   }
 
   @Test
+  public void testUpdateServices() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultSerializer resultSerializer = createStrictMock(ResultSerializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(request.getResultSerializer()).andReturn(resultSerializer);
+    expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
+    expect(result.isSynchronous()).andReturn(true).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.PUT, serializedResult, true)).andReturn(response);
+
+    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    ServiceService service = new TestServiceService(resourceDef, clusterName, null, requestFactory,
responseFactory, requestHandler);
+    assertSame(response, service.updateServices("body", httpHeaders, uriInfo));
+
+    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  @Test
   public void testUpdateService() {
     ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
     ResultSerializer resultSerializer = createStrictMock(ResultSerializer.class);
@@ -155,13 +210,14 @@ public class ServiceServiceTest {
     String serviceName = "serviceName";
 
     // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.POST),
+    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
         eq(resourceDef))).andReturn(request);
 
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(true).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.PUT, serializedResult, true)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);
@@ -199,7 +255,8 @@ public class ServiceServiceTest {
     expect(requestHandler.handleRequest(request)).andReturn(result);
     expect(request.getResultSerializer()).andReturn(resultSerializer);
     expect(resultSerializer.serialize(result, uriInfo)).andReturn(serializedResult);
-    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    expect(result.isSynchronous()).andReturn(false).atLeastOnce();
+    expect(responseFactory.createResponse(Request.Type.DELETE, serializedResult, false)).andReturn(response);
 
     replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
         result, response, httpHeaders, uriInfo);

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/UpdatePersistenceManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/UpdatePersistenceManagerTest.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/UpdatePersistenceManagerTest.java
(original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/UpdatePersistenceManagerTest.java
Wed Nov  7 08:13:12 2012
@@ -20,6 +20,7 @@ package org.apache.ambari.server.api.ser
 
 
 import org.apache.ambari.server.api.resources.ResourceDefinition;
+import org.apache.ambari.server.controller.internal.RequestStatusImpl;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.api.query.Query;
 import org.apache.ambari.server.controller.spi.*;
@@ -56,7 +57,7 @@ public class UpdatePersistenceManagerTes
     expect(resource.getQuery()).andReturn(query);
     expect(query.getInternalPredicate()).andReturn(predicate);
 
-    controller.updateResources(Resource.Type.Component, serverRequest, predicate);
+    expect(controller.updateResources(Resource.Type.Component, serverRequest, predicate)).andReturn(new
RequestStatusImpl(null));
 
     replay(resource, controller, schema, serverRequest, query, predicate);
 

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
(original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
Wed Nov  7 08:13:12 2012
@@ -18,16 +18,87 @@
 
 package org.apache.ambari.server.api.services.serializers;
 
+import org.apache.ambari.server.api.services.Result;
+import org.apache.ambari.server.api.services.ResultImpl;
+import org.apache.ambari.server.api.util.TreeNode;
+import org.apache.ambari.server.api.util.TreeNodeImpl;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Test;
+
+import javax.ws.rs.core.UriInfo;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertEquals;
+
 /**
  * JSONSerializer unit tests
  */
 public class JsonSerializerTest {
-  // FIXME unit test serializer
-//  @Test
-//  public void testSerialize() throws Exception {
-//    Result result = createMock(Result.class);
-//    UriInfo uriInfo = createMock(UriInfo.class);
+  @Test
+  public void testSerialize() throws Exception {
+    UriInfo uriInfo = createMock(UriInfo.class);
+    Resource resource = createMock(Resource.class);
+    //Resource resource2 = createMock(Resource.class);
+
+    Result result = new ResultImpl(true);
+    TreeNode<Resource> tree = result.getResultTree();
+    //tree.setName("items");
+    TreeNode<Resource> child = tree.addChild(resource, "resource1");
+    //child.addChild(resource2, "sub-resource");
+
+    // resource properties
+    HashMap<String, Object> mapRootProps = new HashMap<String, Object>();
+    mapRootProps.put("prop1", "value1");
+    mapRootProps.put("prop2", "value2");
+
+    HashMap<String, Object> mapCategoryProps = new HashMap<String, Object>();
+    mapCategoryProps.put("catProp1", "catValue1");
+    mapCategoryProps.put("catProp2", "catValue2");
+
+    TreeNode<Map<String, Object>> treeProps1 = new TreeNodeImpl<Map<String,
Object>>(
+        null, mapRootProps, null);
+
+    treeProps1.addChild(mapCategoryProps, "category");
+
+//    // resource2 properties
+//    HashMap<String, Object> map2RootProps = new HashMap<String, Object>();
+//    map2RootProps.put("2prop1", "2value1");
 //
+//    HashMap<String, Object> map2CategoryProps = new HashMap<String, Object>();
+//    map2CategoryProps.put("2catProp1", "2catValue1");
 //
-//  }
+//    TreeNode<Map<String, Object>> treeProps2 = new TreeNodeImpl<Map<String,
Object>>(
+//        null, map2RootProps, null);
+//
+//    treeProps2.addChild(mapCategoryProps, "little-category");
+
+
+    //expectations
+    expect(resource.getProperties()).andReturn(treeProps1).anyTimes();
+    expect(resource.getType()).andReturn(Resource.Type.Cluster).anyTimes();
+
+//    expect(resource2.getProperties()).andReturn(treeProps2).anyTimes();
+//    expect(resource2.getType()).andReturn(Resource.Type.Service).anyTimes();
+
+    replay(uriInfo, resource/*, resource2*/);
+
+    //execute test
+    Object o = new JsonSerializer().serialize(result, uriInfo);
+
+    String expected = "{\n" +
+        "  \"prop2\" : \"value2\",\n" +
+        "  \"prop1\" : \"value1\",\n" +
+        "  \"category\" : {\n" +
+        "    \"catProp1\" : \"catValue1\",\n" +
+        "    \"catProp2\" : \"catValue2\"\n" +
+        "  }\n" +
+        "}";
+
+    assertEquals(expected, o);
+
+    verify(uriInfo, resource/*, resource2*/);
+  }
 }



Mime
View raw message