Package org.apache.sling.cassandra.test.data.populator

Source Code of org.apache.sling.cassandra.test.data.populator.CassandraModifyResourceProviderDeleteTest

/*
* 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.sling.cassandra.test.data.populator;


import me.prettyprint.cassandra.model.CqlQuery;
import me.prettyprint.cassandra.model.CqlRows;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
import me.prettyprint.hector.api.query.QueryResult;
import org.apache.sling.cassandra.resource.provider.CassandraResourceProvider;
import org.apache.sling.cassandra.resource.provider.CassandraResourceResolver;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class CassandraModifyResourceProviderDeleteTest {
    private static Logger LOGGER = LoggerFactory.getLogger(CassandraModifyResourceProviderDeleteTest.class);

    @Test
    public void testDeleteData() {
        String cf = "p3";
        try {
            String path1 ="/content/cassandra/" + cf + "/c1";

            CassandraResourceProvider cassandraResourceProvider = new CassandraResourceProvider();
            createColumnFamily(cf, cassandraResourceProvider.getKeyspace(), new StringSerializer());
            cassandraResourceProvider.setColumnFamily(cf);

            Map<String,Object> map1 = new HashMap<String, Object>();
            map1.put("metadata", "resolutionPathInfo=json");
            map1.put("resourceType", "nt:cassandra0");
            map1.put("resourceSuperType", "nt:supercass1");

            CassandraResourceResolver resolver =  new CassandraResourceResolver();
            cassandraResourceProvider.create(resolver,path1,map1);
            Assert.assertNull("Before Commiting Resource should be null", cassandraResourceProvider.getResource(resolver, path1));
            cassandraResourceProvider.commit(resolver);
            Assert.assertNotNull("Commited Resource cannot be null", cassandraResourceProvider.getResource(resolver, path1));

            cassandraResourceProvider.delete(resolver,path1);
            Assert.assertNotNull("Uncommited deleted resource cannot be null",cassandraResourceProvider.getResource(resolver,path1));
            cassandraResourceProvider.commit(resolver);
            Assert.assertNull("Deleted resource should be null", cassandraResourceProvider.getResource(resolver, path1));

        } catch (Exception e) {
            LOGGER.info("Ignore err" + e.getMessage());
            Assert.fail("Failed to add data to cassandra");
        }

    }

    private void createColumnFamily(String cf, Keyspace keyspace, StringSerializer se) {
        String createCF = "CREATE COLUMNFAMILY " + cf + " (KEY varchar PRIMARY KEY,path varchar,resourceType varchar,resourceSuperType varchar,metadata varchar);";
        CqlQuery<String, String, String> cqlQuery = new CqlQuery<String, String, String>(keyspace, se, se, se);
        cqlQuery.setQuery(createCF);
        try{
        QueryResult<CqlRows<String, String, String>> result1 = cqlQuery.execute();
        LOGGER.info(result1.get().getList().size() + " Finished.!");
        }catch(HInvalidRequestException ignore){
            LOGGER.debug("Column Family already exists " + ignore.getMessage());
        }
    }

}
TOP

Related Classes of org.apache.sling.cassandra.test.data.populator.CassandraModifyResourceProviderDeleteTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.