org.codice.opendx.TestNITFInputTransformer.java Source code

Java tutorial

Introduction

Here is the source code for org.codice.opendx.TestNITFInputTransformer.java

Source

/**
 * Copyright (c) Lockheed Martin Corporation
 *
 * This is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or any later version. 
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details. A copy of the GNU Lesser General Public License is distributed along with this program and can be found at
 * <http://www.gnu.org/licenses/lgpl.html>.
 *
 **/

package org.codice.opendx;

import ddf.catalog.data.Metacard;
import ddf.catalog.data.MetacardImpl;
import ddf.catalog.operation.CreateRequest;
import ddf.catalog.operation.CreateResponseImpl;
import joms.oms.DataInfo;
import joms.oms.Init;
import joms.oms.Util;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Init.class, DataInfo.class, Util.class })
@SuppressStaticInitializationFor("joms.oms.jomsJNI")
public class TestNITFInputTransformer {

    @Spy
    private NITFInputTransformer transformer = new NITFInputTransformer();
    private static ddf.catalog.CatalogFramework catalog;

    public NITFInputTransformer createTransformer() throws Exception {
        PowerMockito.mockStatic(Init.class);
        PowerMockito.mockStatic(DataInfo.class);
        PowerMockito.mockStatic(Util.class);

        DataInfo dataInfo = mock(DataInfo.class);
        doReturn(getInfo()).when(dataInfo).getInfo();

        PowerMockito.whenNew(DataInfo.class).withNoArguments().thenThrow(new IOException("error message"));

        Mockito.when(Util.writeImageSpaceThumbnail(any(String.class), any(Integer.class), any(String.class),
                any(String.class), any(Integer.class), any(Integer.class), any(String.class), any(String.class),
                any(Boolean.class))).thenReturn(true);

        doReturn(getThumbnailBytes()).when(transformer).loadFile(any(File.class));
        doReturn(dataInfo).when(transformer).dataInfoForFile(anyString());

        catalog = mock(ddf.catalog.CatalogFramework.class);
        List<Metacard> metacards = new ArrayList<Metacard>();
        MetacardImpl metacard = new MetacardImpl();
        metacards.add(metacard);

        return transformer;
    }

    @Test()
    public void testTransform() throws Exception {
        NITFInputTransformer transformer = createTransformer();
        Thread.currentThread().getContextClassLoader().getResource("i_3001a.ntf");
        String file = Thread.currentThread().getContextClassLoader().getResource("i_3001a.ntf").getFile();

        MetacardImpl metacard = (MetacardImpl) transformer.transform(new FileInputStream(new File(file)));

        assertNotNull(metacard);

        assertEquals(metacard.getContentTypeName(), "image/nitf");
        assertEquals(metacard.getLocation(), "POINT (85.00013888888888 32.98319444444446)");
        assertEquals(new String(Base64.encodeBase64(metacard.getThumbnail())), getTestEncoding());
    }

    @Test()
    public void testCreateThumbnail() throws Exception {
        NITFInputTransformer transformer = createTransformer();

        String tempJpeg = transformer.createThumbnail(
                Thread.currentThread().getContextClassLoader().getResource("i_3001a.ntf").getFile());
        assertNotSame(tempJpeg, "");
    }

    @Test()
    public void testEncodeThumbnail() throws Exception {
        NITFInputTransformer transformer = createTransformer();
        String tempJpeg = transformer.createThumbnail(
                Thread.currentThread().getContextClassLoader().getResource("i_3001a.ntf").getFile());
        assertNotSame(tempJpeg, "");
        String encoding = transformer.encodeThumbnailToBase64Binary(tempJpeg);
        assertEquals(encoding, getTestEncoding());
    }

    private static String getTestEncoding() {
        return "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQE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Z7781NX1DxFL8YP2c/jN4h0bUPEN/wCIdXsPhR8arPWrsrZQ+O/hjL4e8FeNPC9xDEI/7IHxA+HF7o3i3UZrCK3tLrX/ABTrUVrYgI4k96i8U6LefFv4f+HFl8W23ifxHrc/wz8L3TTJdaZY3N9FoWrabey+H7u3uv7KsbvR7y98SXgtbaF9KuLwfam0+RPs16Af/9k=";
    }

    private static byte[] getThumbnailBytes() throws IOException {
        DataInputStream is = new DataInputStream(new FileInputStream(
                Thread.currentThread().getContextClassLoader().getResource("binfile.dat").getFile()));
        byte[] thumbnail = new byte[is.available()];
        is.readFully(thumbnail);
        is.close();
        return thumbnail;
    }

    private static String getInfo() {
        return "<oms>\n" + "   <dataSets>\n" + "      <RasterDataSet>\n" + "         <fileObjects>\n"
                + "            <RasterFile type=\"main\" format=\"nitf\">\n"
                + "                <name>/Users/kwplummer/Documents/Workspace/nitf-input-transformer/target/test-classes/i_3001a.ntf</name>\n"
                + "            </RasterFile>\n" + "         </fileObjects>\n" + "         <rasterEntries>\n"
                + "            <RasterEntry>\n" + "               <entryId>0</entryId>\n"
                + "               <width>1024</width>\n" + "               <height>1024</height>\n"
                + "               <numberOfBands>1</numberOfBands>\n"
                + "               <numberOfResLevels>1</numberOfResLevels>\n"
                + "               <bitDepth>8</bitDepth>\n" + "               <dataType>uint</dataType>\n"
                + "               <gsd unit=\"meters\" dx=\"0.0301635883183536\" dy=\"0.0301635883183522\"/>\n"
                + "               <groundGeom srs=\"epsg:4326\">POLYGON((85.0000001356337 32.9833331976997,85.0002776421441 32.9833331976997,85.0002776421441 32.9830556911892,85.0000001356337 32.9830556911892,85.0000001356337 32.9833331976997))</groundGeom>\n"
                + "               <TiePointSet version='1'>\n" + "                  <Image>\n"
                + "                     <coordinates>0,0 1023,0 1023,1023 0,1023</coordinates>\n"
                + "                  </Image>\n" + "                  <Ground>\n"
                + "                     <coordinates>85.0000001356337,32.9833331976997 85.0002776421441,32.9833331976997 85.0002776421441,32.9830556911892 85.0000001356337,32.9830556911892</coordinates>\n"
                + "                  </Ground>\n" + "               </TiePointSet>\n"
                + "               <metadata>\n"
                + "                  <filename>/Users/kwplummer/Documents/Workspace/nitf-input-transformer/target/test-classes/i_3001a.ntf</filename>\n"
                + "                  <imageId></imageId>\n"
                + "                  <imageRepresentation>MONO    </imageRepresentation>\n"
                + "                  <targetId></targetId>\n" + "                  <productId></productId>\n"
                + "                  <beNumber>          </beNumber>\n"
                + "                  <sensorId></sensorId>\n"
                + "                  <missionId>Unknown                                   </missionId>\n"
                + "                  <countryCode></countryCode>\n"
                + "                  <imageCategory>VIS     </imageCategory>\n"
                + "                  <azimuthAngle>0</azimuthAngle>\n"
                + "                  <grazingAngle></grazingAngle>\n"
                + "                  <securityClassification>U</securityClassification>\n"
                + "                  <title>- BASE IMAGE -                                                                  </title>\n"
                + "                  <organization></organization>\n"
                + "                  <description></description>\n" + "                  <niirs></niirs>\n" + "\n"
                + "   <NITF>\n" + "      <abpp>08</abpp>\n" + "      <bmrlnth>0</bmrlnth>\n"
                + "      <clevel>03</clevel>\n" + "      <comrat>    </comrat>\n" + "      <encryp>0</encryp>\n"
                + "      <fdt>19971217102630</fdt>\n" + "      <fhdr>NITF02.10</fhdr>\n"
                + "      <fsclas>U</fsclas>\n" + "      <fscop>00000</fscop>\n" + "      <fscpys>00000</fscpys>\n"
                + "      <ftitle>Checks an uncompressed 1024x1024 8 bit mono image with GEOcentric data. Airfield</ftitle>\n"
                + "      <ialvl>000</ialvl>\n" + "      <ic>NC</ic>\n" + "      <icat>VIS     </icat>\n"
                + "      <icom/>\n" + "      <icords>G</icords>\n" + "      <idatim>19961217102630</idatim>\n"
                + "      <idlvl>001</idlvl>\n" + "      <ifc001>N</ifc001>\n"
                + "      <igeolo>325900N0850000E325900N0850001E325859N0850001E325859N0850000E</igeolo>\n"
                + "      <iid1>Missing ID</iid1>\n"
                + "      <iid2>- BASE IMAGE -                                                                  </iid2>\n"
                + "      <iloc>0000000000</iloc>\n" + "      <im>IM</im>\n" + "      <imag>1.0 </imag>\n"
                + "      <imdatoff>0</imdatoff>\n" + "      <imflt001/>\n" + "      <imode>B</imode>\n"
                + "      <irep>MONO    </irep>\n" + "      <irepband001>M </irepband001>\n"
                + "      <iscatp> </iscatp>\n" + "      <iscaut>                                        </iscaut>\n"
                + "      <isclas>U</isclas>\n" + "      <isclsy>  </isclsy>\n"
                + "      <iscltx>                                           </iscltx>\n"
                + "      <iscode>           </iscode>\n" + "      <iscrsn> </iscrsn>\n"
                + "      <isctlh>  </isctlh>\n" + "      <isctln>               </isctln>\n"
                + "      <isdcdt>        </isdcdt>\n" + "      <isdctp>  </isdctp>\n"
                + "      <isdcxm>    </isdcxm>\n" + "      <isdg> </isdg>\n" + "      <isdgdt>        </isdgdt>\n"
                + "      <isorce>Unknown                                   </isorce>\n"
                + "      <isrel>                    </isrel>\n" + "      <issrdt>        </issrdt>\n"
                + "      <isubcat001/>\n" + "      <isync>0</isync>\n" + "      <ixshdl>00000</ixshdl>\n"
                + "      <ixsofl>   </ixsofl>\n" + "      <nbands>1</nbands>\n" + "      <nbpc>0001</nbpc>\n"
                + "      <nbpp>08</nbpp>\n" + "      <nbpr>0001</nbpr>\n" + "      <ncols>00001024</ncols>\n"
                + "      <neluts001/>\n" + "      <nicom>0</nicom>\n" + "      <nluts001>0</nluts001>\n"
                + "      <nppbh>1024</nppbh>\n" + "      <nppbv>1024</nppbv>\n" + "      <nrows>00001024</nrows>\n"
                + "      <ostaid>i_3001a   </ostaid>\n" + "      <pjust>R</pjust>\n"
                + "      <pvtype>INT</pvtype>\n" + "      <stype>BF01</stype>\n"
                + "      <tgtid>                 </tgtid>\n" + "      <type>ossimNitfFileHeaderV2_1</type>\n"
                + "      <udidl>00000</udidl>\n" + "      <udofl>000</udofl>\n" + "      <xbands>     </xbands>\n"
                + "   </NITF>\n" + "\n" + "                  <fileType>nitf</fileType>\n"
                + "                  <className>ossimNitfTileSource</className>\n" + "               </metadata>\n"
                + "<TimeStamp>   <when>1996-12-17T10:26:30Z</when></TimeStamp>            </RasterEntry>\n"
                + "         </rasterEntries>\n" + "      </RasterDataSet>\n" + "   </dataSets>\n" + "</oms>";
    }
}