001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.procedure;
019
020import static org.junit.Assert.*;
021
022import java.io.IOException;
023import java.util.Date;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.master.procedure.ProcedureDescriber;
026import org.apache.hadoop.hbase.procedure2.Procedure;
027import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
028import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
029import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
030import org.apache.hadoop.hbase.testclassification.MasterTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036import org.apache.hbase.thirdparty.com.google.protobuf.ByteString;
037import org.apache.hbase.thirdparty.com.google.protobuf.BytesValue;
038
039@Category({MasterTests.class, SmallTests.class})
040public class TestProcedureDescriber {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044      HBaseClassTestRule.forClass(TestProcedureDescriber.class);
045
046  public static class TestProcedure extends Procedure {
047    @Override
048    protected Procedure[] execute(Object env) throws ProcedureYieldException,
049        ProcedureSuspendedException, InterruptedException {
050      return null;
051    }
052
053    @Override
054    protected void rollback(Object env)
055        throws IOException, InterruptedException {
056    }
057
058    @Override
059    protected boolean abort(Object env) {
060      return false;
061    }
062
063    @Override
064    protected void serializeStateData(ProcedureStateSerializer serializer)
065        throws IOException {
066      ByteString byteString = ByteString.copyFrom(new byte[] { 'A' });
067      BytesValue state = BytesValue.newBuilder().setValue(byteString).build();
068      serializer.serialize(state);
069    }
070
071    @Override
072    protected void deserializeStateData(ProcedureStateSerializer serializer)
073        throws IOException {
074    }
075  }
076
077  @Test
078  public void test() {
079    TestProcedure procedure = new TestProcedure();
080    String result = ProcedureDescriber.describe(procedure);
081
082    Date epoch = new Date(0);
083
084    assertEquals("{ ID => '-1', PARENT_ID => '-1', STATE => 'INITIALIZING', OWNER => '', "
085        + "TYPE => 'org.apache.hadoop.hbase.procedure.TestProcedureDescriber$TestProcedure', "
086        + "START_TIME => '" + epoch + "', LAST_UPDATE => '" + epoch + "', PARAMETERS => [ "
087        + "{ value => 'QQ==' } ] }", result);
088  }
089}