incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maha...@apache.org
Subject svn commit: r1431843 [8/10] - in /incubator/ambari/trunk: ./ ambari-agent/ ambari-agent/conf/unix/ ambari-agent/src/main/package/rpm/ ambari-agent/src/main/puppet/modules/configgenerator/manifests/ ambari-agent/src/main/puppet/modules/hdp-ganglia/manif...
Date Fri, 11 Jan 2013 03:02:41 GMT
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java?rev=1431843&r1=1431842&r2=1431843&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java Fri Jan 11 03:02:37 2013
@@ -22,153 +22,109 @@ package org.apache.ambari.server.api.ser
 
 import org.apache.ambari.server.api.handlers.RequestHandler;
 import org.apache.ambari.server.api.resources.ResourceInstance;
-import org.apache.ambari.server.api.services.serializers.ResultSerializer;
 import org.junit.Test;
-
-import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
-import static org.easymock.EasyMock.*;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
 
 /**
  * Unit tests for HostService.
  */
-public class HostServiceTest {
+public class HostServiceTest extends BaseServiceTest {
 
   @Test
   public void testGetHost() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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), isNull(String.class), eq(uriInfo), eq(Request.Type.GET),
-        eq(resourceDef))).andReturn(request);
+    registerExpectations(Request.Type.GET, null, 200, false);
+    replayMocks();
 
-    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.GET, serializedResult, true)).andReturn(response);
+    //test
+    HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.getHost(getHttpHeaders(), getUriInfo(), hostName);
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 200);
+  }
+
+  @Test
+  public void testGetHost__ErrorState() {
+    String clusterName = "clusterName";
+    String hostName = "hostName";
+
+    registerExpectations(Request.Type.GET, null, 500, true);
+    replayMocks();
 
     //test
-    HostService hostService = new TestHostService(resourceDef, clusterName, hostName, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.getHost(httpHeaders, uriInfo, hostName));
+    HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.getHost(getHttpHeaders(), getUriInfo(), hostName);
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
   public void testGetHosts() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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);
+    String clusterName = "clusterName";
 
-    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
-    UriInfo uriInfo = createNiceMock(UriInfo.class);
+    registerExpectations(Request.Type.GET, null, 200, false);
+    replayMocks();
 
-    String clusterName = "clusterName";
+    //test
+    HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.getHosts(getHttpHeaders(), getUriInfo());
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), isNull(String.class), eq(uriInfo), eq(Request.Type.GET),
-        eq(resourceDef))).andReturn(request);
+    verifyResults(response, 200);
+  }
 
-    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.GET, serializedResult, true)).andReturn(response);
+  @Test
+  public void testGetHosts__ErrorState() {
+    String clusterName = "clusterName";
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    registerExpectations(Request.Type.GET, null, 500, true);
+    replayMocks();
 
     //test
-    HostService hostService = new TestHostService(resourceDef, clusterName, null, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.getHosts(httpHeaders, uriInfo));
+    HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.getHosts(getHttpHeaders(), getUriInfo());
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
   public void testCreateHost() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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";
+    String body = "body";
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), isNull(String.class), 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(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);
+    registerExpectations(Request.Type.POST, body, 201, false);
+    replayMocks();
 
     //test
-    HostService hostService = new TestHostService(resourceDef, clusterName, hostName, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.createHost(null, httpHeaders, uriInfo, hostName));
+    HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.createHost(body, getHttpHeaders(), getUriInfo(), hostName);
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 201);
   }
 
   @Test
-  public void testCreateHosts() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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);
+  public void testCreateHost__ErrorState() {
+    String clusterName = "clusterName";
+    String hostName = "hostName";
+    String body = "body";
 
-    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
-    UriInfo uriInfo = createNiceMock(UriInfo.class);
+    registerExpectations(Request.Type.POST, body, 500, true);
+    replayMocks();
 
+    //test
+    HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.createHost(body, getHttpHeaders(), getUriInfo(), hostName);
+
+    verifyResults(response, 500);
+  }
+
+  @Test
+  public void testCreateHosts()  {
     String clusterName = "clusterName";
     String body = "[ " +
         "{\"Hosts\" : {" +
@@ -187,160 +143,138 @@ public class HostServiceTest {
         "          }" +
         "}]";
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq(body), eq(uriInfo), eq(Request.Type.POST),
-        eq(resourceDef))).andReturn(request);
+    registerExpectations(Request.Type.POST, body, 201, false);
+    replayMocks();
+
+    //test
+    HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.createHosts(body, getHttpHeaders(), getUriInfo());
+
+    verifyResults(response, 201);
+  }
 
-    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.POST, serializedResult, false)).andReturn(response);
+  @Test
+  public void testCreateHosts__ErrorState()  {
+    String clusterName = "clusterName";
+    String body = "body";
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    registerExpectations(Request.Type.POST, body, 500, true);
+    replayMocks();
 
     //test
-    HostService hostService = new TestHostService(resourceDef, clusterName, null, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.createHosts(body, httpHeaders, uriInfo));
+    HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.createHosts(body, getHttpHeaders(), getUriInfo());
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
   public void testUpdateHost() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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";
+    String body = "body";
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
-        eq(resourceDef))).andReturn(request);
+    registerExpectations(Request.Type.PUT, body, 200, false);
+    replayMocks();
 
-    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);
+    //test
+    HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.updateHost(body, getHttpHeaders(), getUriInfo(), hostName);
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 200);
+  }
+
+  @Test
+  public void testUpdateHost__ErrorState() {
+    String clusterName = "clusterName";
+    String hostName = "hostName";
+    String body = "body";
+
+    registerExpectations(Request.Type.PUT, body, 500, true);
+    replayMocks();
 
     //test
-    HostService hostService = new TestHostService(resourceDef, clusterName, hostName, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.updateHost("body", httpHeaders, uriInfo, hostName));
+    HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.updateHost(body, getHttpHeaders(), getUriInfo(), hostName);
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
   public void testUpdateHosts() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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);
+    String clusterName = "clusterName";
+    String body = "body";
 
-    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
-    UriInfo uriInfo = createNiceMock(UriInfo.class);
+    registerExpectations(Request.Type.PUT, body, 200, false);
+    replayMocks();
 
-    String clusterName = "clusterName";
+    //test
+    HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.updateHosts(body, getHttpHeaders(), getUriInfo());
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
-        eq(resourceDef))).andReturn(request);
+    verifyResults(response, 200);
+  }
 
-    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);
+  @Test
+  public void testUpdateHosts__ErrorState() {
+    String clusterName = "clusterName";
+    String body = "body";
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    registerExpectations(Request.Type.PUT, body, 500, true);
+    replayMocks();
 
     //test
-    HostService hostService = new TestHostService(resourceDef, clusterName, null, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.updateHosts("body", httpHeaders, uriInfo));
+    HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.updateHosts(body, getHttpHeaders(), getUriInfo());
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
   public void testDeleteHost() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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), isNull(String.class), eq(uriInfo), eq(Request.Type.DELETE),
-        eq(resourceDef))).andReturn(request);
+    registerExpectations(Request.Type.DELETE, null, 200, false);
+    replayMocks();
 
-    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.DELETE, serializedResult, false)).andReturn(response);
+    //test
+    HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.deleteHost(getHttpHeaders(), getUriInfo(), hostName);
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 200);
+  }
+
+  @Test
+  public void testDeleteHost__ErrorState() {
+    String clusterName = "clusterName";
+    String hostName = "hostName";
+
+    registerExpectations(Request.Type.DELETE, null, 500, true);
+    replayMocks();
 
     //test
-    HostService hostService = new TestHostService(resourceDef, clusterName, hostName, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.deleteHost(httpHeaders, uriInfo, hostName));
+    HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.deleteHost(getHttpHeaders(), getUriInfo(), hostName);
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
 
   private class TestHostService extends HostService {
     private RequestFactory m_requestFactory;
-    private ResponseFactory m_responseFactory;
     private RequestHandler m_requestHandler;
     private ResourceInstance m_resourceDef;
     private String m_clusterId;
     private String m_hostId;
 
     private TestHostService(ResourceInstance resourceDef, String clusterId, String hostId, RequestFactory requestFactory,
-                            ResponseFactory responseFactory, RequestHandler handler) {
+                            RequestHandler handler) {
       super(clusterId);
       m_resourceDef = resourceDef;
       m_clusterId = clusterId;
       m_hostId = hostId;
       m_requestFactory = requestFactory;
-      m_responseFactory = responseFactory;
       m_requestHandler = handler;
     }
 
@@ -357,12 +291,7 @@ public class HostServiceTest {
     }
 
     @Override
-    ResponseFactory getResponseFactory() {
-      return m_responseFactory;
-    }
-
-    @Override
-    RequestHandler getRequestHandler() {
+    RequestHandler getRequestHandler(Request.Type requestType) {
       return m_requestHandler;
     }
   }

Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistenceManagerImplTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistenceManagerImplTest.java?rev=1431843&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistenceManagerImplTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PersistenceManagerImplTest.java Fri Jan 11 03:02:37 2013
@@ -0,0 +1,205 @@
+/**
+ * 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.controller.spi.Request;
+import org.apache.ambari.server.api.query.Query;
+import org.apache.ambari.server.controller.spi.*;
+import org.apache.ambari.server.api.resources.ResourceDefinition;
+import org.apache.ambari.server.api.resources.ResourceInstance;
+import org.apache.ambari.server.api.services.persistence.PersistenceManagerImpl;
+import org.apache.ambari.server.controller.internal.RequestStatusImpl;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+
+/**
+ * PersistenceManagerImpl unit tests.
+ */
+public class PersistenceManagerImplTest {
+
+  @Test
+  public void testCreate() throws Exception {
+    ResourceInstance resource = createMock(ResourceInstance.class);
+    ResourceDefinition resourceDefinition = createMock(ResourceDefinition.class);
+    ClusterController controller = createMock(ClusterController.class);
+    Schema schema = createMock(Schema.class);
+    String clusterId = "clusterId";
+    String serviceId = "serviceId";
+    Request serverRequest = createStrictMock(Request.class);
+
+    Map<Resource.Type, String> mapResourceIds = new HashMap<Resource.Type, String>();
+    mapResourceIds.put(Resource.Type.Cluster, "clusterId");
+    mapResourceIds.put(Resource.Type.Service, "serviceId");
+
+    Set<Map<String, Object>> setProperties = new HashSet<Map<String, Object>>();
+    Map<String, Object> mapProperties = new HashMap<String, Object>();
+    mapProperties.put(clusterId, "clusterId");
+    mapProperties.put(serviceId, "serviceId");
+    mapProperties.put(PropertyHelper.getPropertyId("foo", "bar"), "value");
+    setProperties.add(mapProperties);
+
+    //expectations
+    expect(resource.getIds()).andReturn(mapResourceIds);
+    expect(resource.getResourceDefinition()).andReturn(resourceDefinition).atLeastOnce();
+    expect(resourceDefinition.getType()).andReturn(Resource.Type.Component);
+    expect(controller.getSchema(Resource.Type.Component)).andReturn(schema);
+    expect(schema.getKeyPropertyId(Resource.Type.Cluster)).andReturn(clusterId);
+    expect(schema.getKeyPropertyId(Resource.Type.Service)).andReturn(serviceId);
+
+    expect(controller.createResources(Resource.Type.Component, serverRequest)).andReturn(new RequestStatusImpl(null));
+
+    replay(resource, resourceDefinition, controller, schema, serverRequest);
+
+    new TestPersistenceManager(controller, setProperties, serverRequest).create(resource, setProperties);
+
+    verify(resource, resourceDefinition, controller, schema, serverRequest);
+  }
+
+  @Test
+  public void testCreate__MultipleResources() throws Exception {
+    ResourceInstance resource = createMock(ResourceInstance.class);
+    ResourceDefinition resourceDefinition = createMock(ResourceDefinition.class);
+    ClusterController controller = createMock(ClusterController.class);
+    Schema schema = createMock(Schema.class);
+    Request serverRequest = createStrictMock(Request.class);
+
+    String clusterId = "clusterId";
+    String serviceId = "serviceId";
+
+
+    Map<Resource.Type, String> mapResourceIds = new HashMap<Resource.Type, String>();
+    mapResourceIds.put(Resource.Type.Cluster, "clusterId");
+    mapResourceIds.put(Resource.Type.Service, "serviceId");
+
+    Set<Map<String, Object>> setProperties = new HashSet<Map<String, Object>>();
+
+    Map<String, Object> mapResourceProps1 = new HashMap<String, Object>();
+    mapResourceProps1.put(clusterId, "clusterId");
+    mapResourceProps1.put(serviceId, "serviceId");
+    mapResourceProps1.put(PropertyHelper.getPropertyId("foo", "bar"), "value");
+
+    Map<String, Object> mapResourceProps2 = new HashMap<String, Object>();
+    mapResourceProps2.put(clusterId, "clusterId");
+    mapResourceProps2.put(serviceId, "serviceId2");
+    mapResourceProps2.put(PropertyHelper.getPropertyId("foo", "bar2"), "value2");
+
+    setProperties.add(mapResourceProps1);
+    setProperties.add(mapResourceProps2);
+
+    //expectations
+    expect(resource.getIds()).andReturn(mapResourceIds);
+    expect(resource.getResourceDefinition()).andReturn(resourceDefinition);
+    expect(resourceDefinition.getType()).andReturn(Resource.Type.Component);
+    expect(controller.getSchema(Resource.Type.Component)).andReturn(schema);
+    expect(schema.getKeyPropertyId(Resource.Type.Cluster)).andReturn(clusterId).times(2);
+    expect(schema.getKeyPropertyId(Resource.Type.Service)).andReturn(serviceId).times(2);
+
+    expect(controller.createResources(Resource.Type.Component, serverRequest)).andReturn(new RequestStatusImpl(null));
+
+    replay(resource, resourceDefinition, controller, schema, serverRequest);
+
+    new TestPersistenceManager(controller, setProperties, serverRequest).create(resource, setProperties);
+
+    verify(resource, resourceDefinition, controller, schema, serverRequest);
+  }
+
+  @Test
+  public void testUpdate() throws Exception {
+    ResourceInstance resource = createMock(ResourceInstance.class);
+    ResourceDefinition resourceDefinition = createMock(ResourceDefinition.class);
+    ClusterController controller = createMock(ClusterController.class);
+    Schema schema = createMock(Schema.class);
+    Request serverRequest = createStrictMock(Request.class);
+    Query query = createMock(Query.class);
+    Predicate predicate = createMock(Predicate.class);
+
+    Set<Map<String, Object>> setProperties = new HashSet<Map<String, Object>>();
+    Map<String, Object> mapProperties = new HashMap<String, Object>();
+    mapProperties.put(PropertyHelper.getPropertyId("foo", "bar"), "value");
+    setProperties.add(mapProperties);
+
+    //expectations
+    expect(resource.getResourceDefinition()).andReturn(resourceDefinition);
+    expect(resourceDefinition.getType()).andReturn(Resource.Type.Component);
+    expect(resource.getQuery()).andReturn(query);
+    expect(query.getPredicate()).andReturn(predicate);
+
+    expect(controller.updateResources(Resource.Type.Component, serverRequest, predicate)).andReturn(new RequestStatusImpl(null));
+
+    replay(resource, resourceDefinition, controller, schema, serverRequest, query, predicate);
+
+    new TestPersistenceManager(controller, setProperties, serverRequest).update(resource, setProperties);
+
+    verify(resource, resourceDefinition, controller, schema, serverRequest, query, predicate);
+  }
+
+  @Test
+  public void testDelete() throws Exception {
+    ResourceInstance resource = createNiceMock(ResourceInstance.class);
+    ResourceDefinition resourceDefinition = createNiceMock(ResourceDefinition.class);
+    ClusterController controller = createMock(ClusterController.class);
+    Query query = createMock(Query.class);
+    Predicate predicate = createMock(Predicate.class);
+
+    //expectations
+    expect(resource.getResourceDefinition()).andReturn(resourceDefinition).anyTimes();
+    expect(resourceDefinition.getType()).andReturn(Resource.Type.Component).anyTimes();
+    expect(resource.getQuery()).andReturn(query).anyTimes();
+    expect(query.getPredicate()).andReturn(predicate).anyTimes();
+
+    expect(controller.deleteResources(Resource.Type.Component, predicate)).andReturn(new RequestStatusImpl(null));
+
+    replay(resource, resourceDefinition, controller, query, predicate);
+
+    new TestPersistenceManager(controller, null, null).delete(resource, null);
+
+    verify(resource, resourceDefinition, controller, query, predicate);
+  }
+
+
+  private class TestPersistenceManager extends PersistenceManagerImpl {
+
+    private Request m_request;
+    private Set<Map<String, Object>> m_setProperties;
+
+    private TestPersistenceManager(ClusterController controller,
+                                         Set<Map<String, Object>> setProperties,
+                                         Request controllerRequest) {
+      super(controller);
+      m_setProperties = setProperties;
+      m_request = controllerRequest;
+    }
+
+    @Override
+    protected Request createControllerRequest(Set<Map<String, Object>> setProperties) {
+      assertEquals(m_setProperties, setProperties);
+      return m_request;
+    }
+  }
+}

Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java?rev=1431843&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java Fri Jan 11 03:02:37 2013
@@ -0,0 +1,52 @@
+/**
+ * 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.junit.Test;
+
+import static org.junit.Assert.*;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.UriInfo;
+
+/**
+ * PostRequest unit tests
+ */
+public class PostRequestTest extends BaseRequestTest {
+  @Test
+  public void testRequestType() {
+    assertSame(Request.Type.POST, new PostRequest(null, null, null, null).getRequestType());
+  }
+
+  @Test
+  public void testGetQueryPredicate() throws Exception {
+    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";
+    super.testGetQueryPredicate(uri);
+  }
+
+  @Test
+  public void testGetFields() {
+    String fields = "prop,category/prop1,category2/category3/prop2[1,2,3],prop3[4,5,6],category4[7,8,9],sub-resource/*[10,11,12],finalProp";
+    super.testGetFields(fields);
+  }
+
+  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo) {
+    return new PostRequest(headers, body, uriInfo, null);
+  }
+}

Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java?rev=1431843&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java Fri Jan 11 03:02:37 2013
@@ -0,0 +1,52 @@
+/**
+ * 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.junit.Test;
+
+import static org.junit.Assert.*;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.UriInfo;
+
+/**
+ * PutRequest unit tests
+ */
+public class PutRequestTest extends BaseRequestTest {
+  @Test
+  public void testRequestType() {
+    assertSame(Request.Type.PUT, new PutRequest(null, null, null, null).getRequestType());
+  }
+
+  @Test
+  public void testGetQueryPredicate() throws Exception {
+    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";
+    super.testGetQueryPredicate(uri);
+  }
+
+  @Test
+  public void testGetFields() {
+    String fields = "prop,category/prop1,category2/category3/prop2[1,2,3],prop3[4,5,6],category4[7,8,9],sub-resource/*[10,11,12],finalProp";
+    super.testGetFields(fields);
+  }
+
+  protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo) {
+    return new PutRequest(headers, body, uriInfo, null);
+  }
+}

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java?rev=1431843&r1=1431842&r2=1431843&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java Fri Jan 11 03:02:37 2013
@@ -20,271 +20,213 @@ package org.apache.ambari.server.api.ser
 
 import org.apache.ambari.server.api.handlers.RequestHandler;
 import org.apache.ambari.server.api.resources.ResourceInstance;
-import org.apache.ambari.server.api.services.serializers.ResultSerializer;
 import org.junit.Test;
 
-import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
 
-import static org.easymock.EasyMock.*;
-import static org.easymock.EasyMock.verify;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
 
 /**
  * Unit tests for ServiceService.
  */
-public class ServiceServiceTest {
+public class ServiceServiceTest extends BaseServiceTest {
 
   @Test
   public void testGetService() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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 serviceName = "serviceName";
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), isNull(String.class), eq(uriInfo), eq(Request.Type.GET),
-        eq(resourceDef))).andReturn(request);
+    registerExpectations(Request.Type.GET, null, 200, false);
+    replayMocks();
 
-    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.GET, serializedResult, true)).andReturn(response);
+    //test
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.getService(getHttpHeaders(), getUriInfo(), serviceName);
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 200);
+  }
+
+  @Test
+  public void testGetService__ErrorState() {
+    String clusterName = "clusterName";
+    String serviceName = "serviceName";
+
+    registerExpectations(Request.Type.GET, null, 500, true);
+    replayMocks();
 
     //test
-    ServiceService hostService = new TestServiceService(resourceDef, clusterName, serviceName, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.getService(httpHeaders, uriInfo, serviceName));
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.getService(getHttpHeaders(), getUriInfo(), serviceName);
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
-  public void testGetServices() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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);
+  public void testGetServices()  {
+    String clusterName = "clusterName";
 
-    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
-    UriInfo uriInfo = createNiceMock(UriInfo.class);
+    registerExpectations(Request.Type.GET, null, 200, false);
+    replayMocks();
 
-    String clusterName = "clusterName";
+    //test
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.getServices(getHttpHeaders(), getUriInfo());
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), isNull(String.class), eq(uriInfo), eq(Request.Type.GET),
-        eq(resourceDef))).andReturn(request);
+    verifyResults(response, 200);
+  }
 
-    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.GET, serializedResult, true)).andReturn(response);
+  @Test
+  public void testGetServices__ErrorState(){
+    String clusterName = "clusterName";
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    registerExpectations(Request.Type.GET, null, 400, false);
+    replayMocks();
 
     //test
-    ServiceService hostService = new TestServiceService(resourceDef, clusterName, null, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.getServices(httpHeaders, uriInfo));
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.getServices(getHttpHeaders(), getUriInfo());
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 400);
   }
 
   @Test
   public void testCreateService() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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 serviceName = "serviceName";
+    String body = "{body}";
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.POST),
-        eq(resourceDef))).andReturn(request);
+    registerExpectations(Request.Type.POST, body, 201, false);
+    replayMocks();
 
-    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(Request.Type.POST, serializedResult, false)).andReturn(response);
+    //test
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.createService(body, getHttpHeaders(), getUriInfo(), serviceName);
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 201);
+  }
+
+  @Test
+  public void testCreateService__ErrorState() {
+    String clusterName = "clusterName";
+    String serviceName = "serviceName";
+    String body = "{body}";
+
+    registerExpectations(Request.Type.POST, body, 500, true);
+    replayMocks();
 
     //test
-    ServiceService hostService = new TestServiceService(resourceDef, clusterName, serviceName, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.createService("body", httpHeaders, uriInfo, serviceName));
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.createService(body, getHttpHeaders(), getUriInfo(), serviceName);
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
   public void testUpdateServices() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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);
+    String clusterName = "clusterName";
+    String body = "{body}";
 
-    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
-    UriInfo uriInfo = createNiceMock(UriInfo.class);
+    registerExpectations(Request.Type.PUT, body, 200, false);
+    replayMocks();
 
-    String clusterName = "clusterName";
+    //test
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.updateServices(body, getHttpHeaders(), getUriInfo());
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
-        eq(resourceDef))).andReturn(request);
+    verifyResults(response, 200);
+  }
 
-    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);
+  @Test
+  public void testUpdateServices__ErrorState() {
+    String clusterName = "clusterName";
+    String body = "{body}";
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    registerExpectations(Request.Type.PUT, body, 500, true);
+    replayMocks();
 
     //test
-    ServiceService service = new TestServiceService(resourceDef, clusterName, null, requestFactory, responseFactory, requestHandler);
-    assertSame(response, service.updateServices("body", httpHeaders, uriInfo));
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
+    Response response = hostService.updateServices(body, getHttpHeaders(), getUriInfo());
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
   public void testUpdateService() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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 serviceName = "serviceName";
+    String body = "{body}";
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), eq("body"), eq(uriInfo), eq(Request.Type.PUT),
-        eq(resourceDef))).andReturn(request);
+    registerExpectations(Request.Type.PUT, body, 200, false);
+    replayMocks();
 
-    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);
+    //test
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.updateService(body, getHttpHeaders(), getUriInfo(), serviceName);
+
+    verifyResults(response, 200);
+  }
+
+  @Test
+  public void testUpdateService__ErrorState() {
+    String clusterName = "clusterName";
+    String serviceName = "serviceName";
+    String body = "{body}";
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    registerExpectations(Request.Type.PUT, body, 500, true);
+    replayMocks();
 
     //test
-    ServiceService hostService = new TestServiceService(resourceDef, clusterName, serviceName, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.updateService("body", httpHeaders, uriInfo, serviceName));
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.updateService(body, getHttpHeaders(), getUriInfo(), serviceName);
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   @Test
   public void testDeleteService() {
-    ResourceInstance resourceDef = createStrictMock(ResourceInstance.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 serviceName = "serviceName";
 
-    // expectations
-    expect(requestFactory.createRequest(eq(httpHeaders), isNull(String.class), eq(uriInfo), eq(Request.Type.DELETE),
-        eq(resourceDef))).andReturn(request);
+    registerExpectations(Request.Type.DELETE, null, 200, false);
+    replayMocks();
+
+    //test
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.deleteService(getHttpHeaders(), getUriInfo(), serviceName);
+
+    verifyResults(response, 200);
+  }
 
-    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.DELETE, serializedResult, false)).andReturn(response);
+  @Test
+  public void testDeleteService__ErrorState(){
+    String clusterName = "clusterName";
+    String serviceName = "serviceName";
 
-    replay(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    registerExpectations(Request.Type.DELETE, null, 500, true);
+    replayMocks();
 
     //test
-    ServiceService hostService = new TestServiceService(resourceDef, clusterName, serviceName, requestFactory, responseFactory, requestHandler);
-    assertSame(response, hostService.deleteService(httpHeaders, uriInfo, serviceName));
+    ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
+    Response response = hostService.deleteService(getHttpHeaders(), getUriInfo(), serviceName);
 
-    verify(resourceDef, resultSerializer, requestFactory, responseFactory, request, requestHandler,
-        result, response, httpHeaders, uriInfo);
+    verifyResults(response, 500);
   }
 
   private class TestServiceService extends ServiceService {
     private RequestFactory m_requestFactory;
-    private ResponseFactory m_responseFactory;
     private RequestHandler m_requestHandler;
     private ResourceInstance m_resourceDef;
     private String m_clusterId;
     private String m_serviceId;
 
     private TestServiceService(ResourceInstance resourceDef, String clusterId, String serviceId, RequestFactory requestFactory,
-                               ResponseFactory responseFactory, RequestHandler handler) {
+                               RequestHandler handler) {
       super(clusterId);
       m_resourceDef = resourceDef;
       m_clusterId = clusterId;
       m_serviceId = serviceId;
       m_requestFactory = requestFactory;
-      m_responseFactory = responseFactory;
       m_requestHandler = handler;
     }
 
@@ -301,12 +243,7 @@ public class ServiceServiceTest {
     }
 
     @Override
-    ResponseFactory getResponseFactory() {
-      return m_responseFactory;
-    }
-
-    @Override
-    RequestHandler getRequestHandler() {
+    RequestHandler getRequestHandler(Request.Type requestType) {
       return m_requestHandler;
     }
   }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/UpdatePersistenceManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/UpdatePersistenceManagerTest.java?rev=1431843&r1=1431842&r2=1431843&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/UpdatePersistenceManagerTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/UpdatePersistenceManagerTest.java Fri Jan 11 03:02:37 2013
@@ -41,61 +41,120 @@ import static org.easymock.EasyMock.*;
  * Unit tests for UpdatePersistenceManager.
  */
 public class UpdatePersistenceManagerTest {
-  @Test
-  public void testPersist() throws Exception {
-    ResourceInstance resource = createMock(ResourceInstance.class);
-    ResourceDefinition resourceDefinition = createMock(ResourceDefinition.class);
-    ClusterController controller = createMock(ClusterController.class);
-    Schema schema = createMock(Schema.class);
-    Request serverRequest = createStrictMock(Request.class);
-    Query query = createMock(Query.class);
-    Predicate predicate = createMock(Predicate.class);
-
-    Set<Map<String, Object>> setProperties = new HashSet<Map<String, Object>>();
-    Map<String, Object> mapProperties = new HashMap<String, Object>();
-    mapProperties.put(PropertyHelper.getPropertyId("foo", "bar"), "value");
-    setProperties.add(mapProperties);
-
-    //expectations
-    expect(resource.getResourceDefinition()).andReturn(resourceDefinition);
-    expect(resourceDefinition.getType()).andReturn(Resource.Type.Component);
-    expect(resource.getQuery()).andReturn(query);
-    expect(query.getPredicate()).andReturn(predicate);
-
-    expect(controller.updateResources(Resource.Type.Component, serverRequest, predicate)).andReturn(new RequestStatusImpl(null));
-
-    replay(resource, resourceDefinition, controller, schema, serverRequest, query, predicate);
-
-    new TestUpdatePersistenceManager(controller, setProperties, serverRequest).persist(resource, setProperties);
-
-    verify(resource, resourceDefinition, controller, schema, serverRequest, query, predicate);
-  }
-
-  private class TestUpdatePersistenceManager extends UpdatePersistenceManager {
-
-    private ClusterController m_controller;
-    private Request m_request;
-    private Set<Map<String, Object>> m_setProperties;
-
-    private TestUpdatePersistenceManager(ClusterController controller,
-                                         Set<Map<String, Object>> setProperties,
-                                         Request controllerRequest) {
-      m_controller = controller;
-      m_setProperties = setProperties;
-      m_request = controllerRequest;
-    }
-
-    @Override
-    protected ClusterController getClusterController() {
-      return m_controller;
-    }
-
-    @Override
-    protected Request createControllerRequest(Set<Map<String, Object>> setProperties) {
-      assertEquals(1, setProperties.size());
-      assertEquals(m_setProperties, setProperties);
-      return m_request;
-    }
-  }
+//<<<<<<< Updated upstream
+//  @Test
+//  public void testPersist() throws Exception {
+//    ResourceInstance resource = createMock(ResourceInstance.class);
+//    ResourceDefinition resourceDefinition = createMock(ResourceDefinition.class);
+//    ClusterController controller = createMock(ClusterController.class);
+//    Schema schema = createMock(Schema.class);
+//    Request serverRequest = createStrictMock(Request.class);
+//    Query query = createMock(Query.class);
+//    Predicate predicate = createMock(Predicate.class);
+//
+//    Set<Map<String, Object>> setProperties = new HashSet<Map<String, Object>>();
+//    Map<String, Object> mapProperties = new HashMap<String, Object>();
+//    mapProperties.put(PropertyHelper.getPropertyId("foo", "bar"), "value");
+//    setProperties.add(mapProperties);
+//
+//    //expectations
+//    expect(resource.getResourceDefinition()).andReturn(resourceDefinition);
+//    expect(resourceDefinition.getType()).andReturn(Resource.Type.Component);
+//    expect(resource.getQuery()).andReturn(query);
+//    expect(query.getPredicate()).andReturn(predicate);
+//
+//    expect(controller.updateResources(Resource.Type.Component, serverRequest, predicate)).andReturn(new RequestStatusImpl(null));
+//
+//    replay(resource, resourceDefinition, controller, schema, serverRequest, query, predicate);
+//
+//    new TestUpdatePersistenceManager(controller, setProperties, serverRequest).persist(resource, setProperties);
+//
+//    verify(resource, resourceDefinition, controller, schema, serverRequest, query, predicate);
+//  }
+//
+//  private class TestUpdatePersistenceManager extends UpdatePersistenceManager {
+//
+//    private ClusterController m_controller;
+//    private Request m_request;
+//    private Set<Map<String, Object>> m_setProperties;
+//
+//    private TestUpdatePersistenceManager(ClusterController controller,
+//                                         Set<Map<String, Object>> setProperties,
+//                                         Request controllerRequest) {
+//      m_controller = controller;
+//      m_setProperties = setProperties;
+//      m_request = controllerRequest;
+//    }
+//
+//    @Override
+//    protected ClusterController getClusterController() {
+//      return m_controller;
+//    }
+//
+//    @Override
+//    protected Request createControllerRequest(Set<Map<String, Object>> setProperties) {
+//      assertEquals(1, setProperties.size());
+//      assertEquals(m_setProperties, setProperties);
+//      return m_request;
+//    }
+//  }
+//=======
+//  @Test
+//  public void testPersist() throws Exception {
+//    ResourceInstance resource = createMock(ResourceInstance.class);
+//    ResourceDefinition resourceDefinition = createMock(ResourceDefinition.class);
+//    ClusterController controller = createMock(ClusterController.class);
+//    Schema schema = createMock(Schema.class);
+//    Request serverRequest = createStrictMock(Request.class);
+//    Query query = createMock(Query.class);
+//    Predicate predicate = createMock(Predicate.class);
+//
+//    Set<Map<PropertyId, Object>> setProperties = new HashSet<Map<PropertyId, Object>>();
+//    Map<PropertyId, Object> mapProperties = new HashMap<PropertyId, Object>();
+//    mapProperties.put(PropertyHelper.getPropertyId("bar", "foo"), "value");
+//    setProperties.add(mapProperties);
+//
+//    //expectations
+//    expect(resource.getResourceDefinition()).andReturn(resourceDefinition);
+//    expect(resourceDefinition.getType()).andReturn(Resource.Type.Component);
+//    expect(resource.getQuery()).andReturn(query);
+//    expect(query.getPredicate()).andReturn(predicate);
+//
+//    expect(controller.updateResources(Resource.Type.Component, serverRequest, predicate)).andReturn(new RequestStatusImpl(null));
+//
+//    replay(resource, resourceDefinition, controller, schema, serverRequest, query, predicate);
+//
+//    new TestUpdatePersistenceManager(controller, setProperties, serverRequest).persist(resource, setProperties);
+//
+//    verify(resource, resourceDefinition, controller, schema, serverRequest, query, predicate);
+//  }
+//
+//  private class TestUpdatePersistenceManager extends UpdatePersistenceManager {
+//
+//    private ClusterController m_controller;
+//    private Request m_request;
+//    private Set<Map<PropertyId, Object>> m_setProperties;
+//
+//    private TestUpdatePersistenceManager(ClusterController controller,
+//                                         Set<Map<PropertyId, Object>> setProperties,
+//                                         Request controllerRequest) {
+//      m_controller = controller;
+//      m_setProperties = setProperties;
+//      m_request = controllerRequest;
+//    }
+//
+//    @Override
+//    protected ClusterController getClusterController() {
+//      return m_controller;
+//    }
+//
+//    @Override
+//    protected Request createControllerRequest(Set<Map<PropertyId, Object>> setProperties) {
+//      assertEquals(1, setProperties.size());
+//      assertEquals(m_setProperties, setProperties);
+//      return m_request;
+//    }
+//  }
+//>>>>>>> Stashed changes
 
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java?rev=1431843&r1=1431842&r2=1431843&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java Fri Jan 11 03:02:37 2013
@@ -20,6 +20,7 @@ package org.apache.ambari.server.api.ser
 
 import org.apache.ambari.server.api.services.Result;
 import org.apache.ambari.server.api.services.ResultImpl;
+import org.apache.ambari.server.api.services.ResultStatus;
 import org.apache.ambari.server.api.util.TreeNode;
 import org.apache.ambari.server.api.util.TreeNodeImpl;
 import org.apache.ambari.server.controller.spi.Resource;
@@ -44,6 +45,7 @@ public class JsonSerializerTest {
     //Resource resource2 = createMock(Resource.class);
 
     Result result = new ResultImpl(true);
+    result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK));
     TreeNode<Resource> tree = result.getResultTree();
     //tree.setName("items");
     TreeNode<Resource> child = tree.addChild(resource, "resource1");
@@ -86,7 +88,7 @@ public class JsonSerializerTest {
     replay(uriInfo, resource/*, resource2*/);
 
     //execute test
-    Object o = new JsonSerializer().serialize(result, uriInfo);
+    Object o = new JsonSerializer().serialize(result);
 
     String expected = "{\n" +
         "  \"prop2\" : \"value2\",\n" +



Mime
View raw message