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)
049      throws ProcedureYieldException, ProcedureSuspendedException, InterruptedException {
050      return null;
051    }
052
053    @Override
054    protected void rollback(Object env) throws IOException, InterruptedException {
055    }
056
057    @Override
058    protected boolean abort(Object env) {
059      return false;
060    }
061
062    @Override
063    protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
064      ByteString byteString = ByteString.copyFrom(new byte[] { 'A' });
065      BytesValue state = BytesValue.newBuilder().setValue(byteString).build();
066      serializer.serialize(state);
067    }
068
069    @Override
070    protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
071    }
072  }
073
074  @Test
075  public void test() {
076    TestProcedure procedure = new TestProcedure();
077    String result = ProcedureDescriber.describe(procedure);
078
079    Date epoch = new Date(0);
080
081    assertEquals("{ ID => '-1', PARENT_ID => '-1', STATE => 'INITIALIZING', OWNER => '', "
082      + "TYPE => 'org.apache.hadoop.hbase.procedure.TestProcedureDescriber$TestProcedure', "
083      + "START_TIME => '" + epoch + "', LAST_UPDATE => '" + epoch + "', PARAMETERS => [ "
084      + "{ value => 'QQ==' } ] }", result);
085  }
086}