Repository: ambari
Updated Branches:
  refs/heads/trunk 2fb2a17ec -> 5dff4516a


http://git-wip-us.apache.org/repos/asf/ambari/blob/5dff4516/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestOperationLevelTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestOperationLevelTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestOperationLevelTest.java
new file mode 100644
index 0000000..d832015
--- /dev/null
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestOperationLevelTest.java
@@ -0,0 +1,58 @@
+/**
+ * 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.controller.internal;
+
+import junit.framework.TestCase;
+import org.junit.Test;
+import static junit.framework.TestCase.*;
+
+public class RequestOperationLevelTest {
+
+  private final String host_component = "HOST_COMPONENT";
+  private final String hostComponent = "HostComponent";
+
+  @Test
+  public void testGetInternalLevelName() throws Exception {
+    String internal = 
RequestOperationLevel.getInternalLevelName(host_component);
+    assertEquals(internal, hostComponent);
+    // Check case-insensitivity
+    internal = 
RequestOperationLevel.getInternalLevelName(host_component.toLowerCase());
+    assertEquals(internal, hostComponent);
+    // Check wrong param
+    try {
+      RequestOperationLevel.getInternalLevelName("Wrong_param");
+      fail("Should throw exception");
+    } catch (IllegalArgumentException e) {
+      // expected
+    }
+  }
+
+  @Test
+  public void testGetExternalLevelName() throws Exception {
+    String external = 
RequestOperationLevel.getExternalLevelName(hostComponent);
+    assertEquals(external, host_component);
+    // Check wrong param
+    try {
+      RequestOperationLevel.getExternalLevelName("Wrong_param");
+      fail("Should throw exception");
+    } catch (IllegalArgumentException e) {
+      // expected
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/5dff4516/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
index c0ad8ce..2312383 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
@@ -30,7 +30,10 @@ import 
org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException;
 import org.apache.ambari.server.controller.spi.ResourceProvider;
+import org.apache.ambari.server.controller.spi.SystemException;
+import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.state.Cluster;
@@ -914,8 +917,8 @@ public class RequestResourceProviderTest {
 
     Map<String, String> requestInfoProperties = new HashMap<String, String>();
 
-    requestInfoProperties.put("/parameters/param1", "value1");
-    requestInfoProperties.put("/parameters/param2", "value2");
+    requestInfoProperties.put("parameters/param1", "value1");
+    requestInfoProperties.put("parameters/param2", "value2");
 
     String[] expectedHosts = new String[]{"host1", "host2", "host3"};
     Map<String, String> expectedParams = new HashMap<String, String>() {{
@@ -965,4 +968,99 @@ public class RequestResourceProviderTest {
       Assert.assertEquals(expectedParams.get(key), 
capturedRequest.getParameters().get(key));
     }
   }
+
+  @Test
+  public void testCreateResourcesForCommandsWithOpLvl() throws Exception {
+    Resource.Type type = Resource.Type.Request;
+
+    Capture<ExecuteActionRequest> actionRequest = new 
Capture<ExecuteActionRequest>();
+    Capture<HashMap<String, String>> propertyMap = new Capture<HashMap<String, 
String>>();
+
+    AmbariManagementController managementController = 
createMock(AmbariManagementController.class);
+    RequestStatusResponse response = 
createNiceMock(RequestStatusResponse.class);
+
+    expect(managementController.createAction(capture(actionRequest), 
capture(propertyMap)))
+            .andReturn(response).anyTimes();
+
+    // replay
+    replay(managementController);
+
+    // add the property map to a set for the request.  add more maps for 
multiple creates
+    Set<Map<String, Object>> propertySet = new LinkedHashSet<Map<String, 
Object>>();
+
+    Map<String, Object> properties = new LinkedHashMap<String, Object>();
+
+    String c1 = "c1";
+    String host_component = "HOST_COMPONENT";
+    String service_id = "HDFS";
+    String hostcomponent_id = "Namenode";
+    String host_id = "host1";
+
+    properties.put(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID, 
c1);
+
+    Set<Map<String, Object>> filterSet = new HashSet<Map<String, Object>>();
+    Map<String, Object> filterMap = new HashMap<String, Object>();
+    filterMap.put(RequestResourceProvider.SERVICE_ID, service_id);
+    filterMap.put(RequestResourceProvider.HOSTS_ID, host_id);
+    filterSet.add(filterMap);
+
+    properties.put(RequestResourceProvider.REQUEST_RESOURCE_FILTER_ID, 
filterSet);
+
+    propertySet.add(properties);
+
+    Map<String, String> requestInfoProperties = new HashMap<String, String>();
+    requestInfoProperties.put(RequestResourceProvider.COMMAND_ID, "RESTART");
+
+    requestInfoProperties.put(RequestResourceProvider.OPERATION_LEVEL_ID,
+            host_component);
+    requestInfoProperties.put(RequestResourceProvider.OPERATION_CLUSTER_ID, 
c1);
+    requestInfoProperties.put(RequestResourceProvider.OPERATION_SERVICE_ID,
+            service_id);
+    
requestInfoProperties.put(RequestResourceProvider.OPERATION_HOSTCOMPONENT_ID,
+            hostcomponent_id);
+    requestInfoProperties.put(RequestResourceProvider.OPERATION_HOST_ID,
+            host_id);
+
+    Request request = PropertyHelper.getCreateRequest(propertySet, 
requestInfoProperties);
+    ResourceProvider provider = 
AbstractControllerResourceProvider.getResourceProvider(
+            type,
+            PropertyHelper.getPropertyIds(type),
+            PropertyHelper.getKeyPropertyIds(type),
+            managementController);
+
+    // Check exception wrong operation level is specified
+    requestInfoProperties.put(RequestResourceProvider.OPERATION_LEVEL_ID,
+            "wrong_value");
+    try {
+      provider.createResources(request);
+      Assert.fail("Should throw an exception");
+    } catch (UnsupportedOperationException e) {
+      // expected
+    }
+    requestInfoProperties.put(RequestResourceProvider.OPERATION_LEVEL_ID,
+            host_component);
+
+    // Check exception when cluster name is not specified
+    requestInfoProperties.remove(RequestResourceProvider.OPERATION_CLUSTER_ID);
+    try {
+      provider.createResources(request);
+      Assert.fail("Should throw an exception");
+    } catch (UnsupportedOperationException e) {
+      // expected
+    }
+    requestInfoProperties.put(RequestResourceProvider.OPERATION_CLUSTER_ID, 
c1);
+
+    // create request in a normal way (positive scenario)
+    provider.createResources(request);
+    Assert.assertTrue(actionRequest.hasCaptured());
+    ExecuteActionRequest capturedRequest = actionRequest.getValue();
+    RequestOperationLevel level = capturedRequest.getOperationLevel();
+    Assert.assertEquals(level.getLevel().toString(), "HostComponent");
+    Assert.assertEquals(level.getClusterName(), c1);
+    Assert.assertEquals(level.getServiceName(), service_id);
+    Assert.assertEquals(level.getHostComponentName(), hostcomponent_id);
+    Assert.assertEquals(level.getHostName(), host_id);
+  }
+
+
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/5dff4516/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog161Test.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog161Test.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog161Test.java
new file mode 100644
index 0000000..415951f
--- /dev/null
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog161Test.java
@@ -0,0 +1,196 @@
+/*
+ * 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.upgrade;
+
+import com.google.inject.Binder;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.orm.DBAccessor;
+import org.easymock.Capture;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.assertTrue;
+import static org.easymock.EasyMock.capture;
+import static org.easymock.EasyMock.createMockBuilder;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+/**
+ * UpgradeCatalog161 unit tests.
+ */
+public class UpgradeCatalog161Test {
+
+  @Test
+  public void testExecuteDDLUpdates() throws Exception {
+
+    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
+    Configuration configuration = createNiceMock(Configuration.class);
+    Capture<List<DBAccessor.DBColumnInfo>> operationLevelEntitycolumnCapture = 
new Capture<List<DBAccessor.DBColumnInfo>>();
+
+    
expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
+
+    setOperationLevelEntityConfigExpectations(dbAccessor, 
operationLevelEntitycolumnCapture);
+
+    replay(dbAccessor, configuration);
+    AbstractUpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
+    Class<?> c = AbstractUpgradeCatalog.class;
+    Field f = c.getDeclaredField("configuration");
+    f.setAccessible(true);
+    f.set(upgradeCatalog, configuration);
+
+    upgradeCatalog.executeDDLUpdates();
+    verify(dbAccessor, configuration);
+
+    assertOperationLevelEntityColumns(operationLevelEntitycolumnCapture);
+  }
+
+
+  @Test
+  public void testExecuteDMLUpdates() throws Exception {
+    Configuration configuration = createNiceMock(Configuration.class);
+    DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
+
+    Method m = AbstractUpgradeCatalog.class.getDeclaredMethod
+      ("updateConfigurationProperties", String.class, Map.class, 
boolean.class);
+
+    UpgradeCatalog161 upgradeCatalog = 
createMockBuilder(UpgradeCatalog161.class)
+      .addMockedMethod(m).createMock();
+
+    
expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
+
+    replay(upgradeCatalog, dbAccessor, configuration);
+
+    Class<?> c = AbstractUpgradeCatalog.class;
+    Field f = c.getDeclaredField("configuration");
+    f.setAccessible(true);
+    f.set(upgradeCatalog, configuration);
+    f = c.getDeclaredField("dbAccessor");
+    f.setAccessible(true);
+    f.set(upgradeCatalog, dbAccessor);
+
+    upgradeCatalog.executeDMLUpdates();
+
+    verify(upgradeCatalog, dbAccessor, configuration);
+  }
+
+
+  @Test
+  public void testGetTargetVersion() throws Exception {
+    final DBAccessor dbAccessor     = createNiceMock(DBAccessor.class);
+    UpgradeCatalog   upgradeCatalog = getUpgradeCatalog(dbAccessor);
+
+    Assert.assertEquals("1.6.1", upgradeCatalog.getTargetVersion());
+  }
+
+
+  private AbstractUpgradeCatalog getUpgradeCatalog(final DBAccessor 
dbAccessor) {
+    Module module = new Module() {
+      @Override
+      public void configure(Binder binder) {
+        binder.bind(DBAccessor.class).toInstance(dbAccessor);
+      }
+    };
+    Injector injector = Guice.createInjector(module);
+    return injector.getInstance(UpgradeCatalog161.class);
+  }
+
+
+  private void setOperationLevelEntityConfigExpectations(DBAccessor dbAccessor,
+                    Capture<List<DBAccessor.DBColumnInfo>> 
operationLevelEntitycolumnCapture)
+          throws SQLException {
+
+    dbAccessor.createTable(eq("requestoperationlevel"),
+            capture(operationLevelEntitycolumnCapture), 
eq("operation_level_id"));
+
+    dbAccessor.addFKConstraint("requestoperationlevel", 
"FK_req_op_level_req_id",
+            "request_id", "request", "request_id", true);
+  }
+
+
+  private void 
assertOperationLevelEntityColumns(Capture<List<DBAccessor.DBColumnInfo>> 
operationLevelEntitycolumnCapture) {
+    List<DBAccessor.DBColumnInfo> columns = 
operationLevelEntitycolumnCapture.getValue();
+    assertEquals(7, columns.size());
+
+    DBAccessor.DBColumnInfo column = columns.get(0);
+    assertEquals("operation_level_id", column.getName());
+    assertNull(column.getLength());
+    assertEquals(Long.class, column.getType());
+    assertNull(column.getDefaultValue());
+    assertFalse(column.isNullable());
+
+    column = columns.get(1);
+    assertEquals("request_id", column.getName());
+    assertNull(column.getLength());
+    assertEquals(Long.class, column.getType());
+    assertNull(column.getDefaultValue());
+    assertFalse(column.isNullable());
+
+    column = columns.get(2);
+    assertEquals("level_name", column.getName());
+    assertEquals(255, (int) column.getLength());
+    assertEquals(String.class, column.getType());
+    assertNull(column.getDefaultValue());
+    assertTrue(column.isNullable());
+
+    column = columns.get(3);
+    assertEquals("cluster_name", column.getName());
+    assertEquals(255, (int) column.getLength());
+    assertEquals(String.class, column.getType());
+    assertNull(column.getDefaultValue());
+    assertTrue(column.isNullable());
+
+    column = columns.get(4);
+    assertEquals("service_name", column.getName());
+    assertEquals(255, (int) column.getLength());
+    assertEquals(String.class, column.getType());
+    assertNull(column.getDefaultValue());
+    assertTrue(column.isNullable());
+
+    column = columns.get(5);
+    assertEquals("host_component_name", column.getName());
+    assertEquals(255, (int) column.getLength());
+    assertEquals(String.class, column.getType());
+    assertNull(column.getDefaultValue());
+    assertTrue(column.isNullable());
+
+    column = columns.get(6);
+    assertEquals("host_name", column.getName());
+    assertEquals(255, (int) column.getLength());
+    assertEquals(String.class, column.getType());
+    assertNull(column.getDefaultValue());
+    assertTrue(column.isNullable());
+
+  }
+
+}

Reply via email to