/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed 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 com.proofpoint.cloudmanagement.service;

import com.proofpoint.experimental.units.DataSize;
import com.proofpoint.experimental.units.DataSize.Unit;
import com.proofpoint.json.JsonCodec;
import org.jclouds.openstack.nova.domain.Flavor;
import org.testng.annotations.Test;

import java.util.Map;

import static com.proofpoint.testing.EquivalenceTester.equivalenceTester;
import static org.testng.Assert.assertEquals;

public class TestSize
{

    private JsonCodec<Size> sizeJsonCodec = JsonCodec.jsonCodec(Size.class);
    private JsonCodec<Map<String, Object>> mapJsonCodec = JsonCodec.mapJsonCodec(String.class, Object.class);

    @Test
    public void testEquivalence()
    {
        equivalenceTester()
                .addEquivalentGroup(new Size("a", 1, new DataSize(1, Unit.BYTE), new DataSize(1, Unit.BYTE)),
                                    new Size("a", 1, new DataSize(1, Unit.BYTE), new DataSize(1, Unit.BYTE)))
                .addEquivalentGroup(new Size("a", 2, new DataSize(1, Unit.BYTE), new DataSize(1, Unit.BYTE)),
                                    new Size("a", 2, new DataSize(1, Unit.BYTE), new DataSize(1, Unit.BYTE)))
                .addEquivalentGroup(new Size("a", 1, new DataSize(2, Unit.BYTE), new DataSize(1, Unit.BYTE)),
                                    new Size("a", 1, new DataSize(2, Unit.BYTE), new DataSize(1, Unit.BYTE)))
                .addEquivalentGroup(new Size("a", 1, new DataSize(1, Unit.BYTE), new DataSize(2, Unit.BYTE)),
                                    new Size("a", 1, new DataSize(1, Unit.BYTE), new DataSize(2, Unit.BYTE)))
                .check();
    }

    @Test
    public void testJsonMarshalling()
    {
        Size testSize = new Size("m1.xlarge", 8, new DataSize(8, Unit.GIGABYTE), new DataSize(1, Unit.TERABYTE));
        String json = sizeJsonCodec.toJson(testSize);
        Map<String, Object> encodedSize = mapJsonCodec.fromJson(json);

        assertEquals(testSize.getName(), encodedSize.get("size"));
        assertEquals(testSize.getCores(), encodedSize.get("cores"));
        assertEquals(testSize.getMemory(), encodedSize.get("memory"));
        assertEquals(testSize.getDisk(), encodedSize.get("disk"));
    }

    @Test
    public void testConstructionFromFlavor()
    {
        Flavor testFlavor = new Flavor(0, "m1.xlarge", 1024, 8192, 8);
        Size testSize = new Size("m1.xlarge", 8, new DataSize(8, Unit.GIGABYTE), new DataSize(1, Unit.TERABYTE));
        assertEquals(Size.fromFlavor(testFlavor), testSize);
    }
}

@@ -2536,28 +2536,31 @@
 tEquals(
-test
+encoded
 Size.get
 Name(), 
@@ -2555,31 +2555,30 @@
 .get
-Name(), encoded
+(%22name%22), test
 Size.get
 (%22si
@@ -2577,15 +2577,13 @@
 .get
-(%22size%22
+Name(
 ));%0A
@@ -2603,28 +2603,31 @@
 tEquals(
-test
+encoded
 Size.get
 Cores(),
@@ -2622,32 +2622,31 @@
 .get
-C
+(%22c
 ores
-(), encoded
+%22), test
 Size.get
 (%22co
@@ -2641,24 +2641,22 @@
 Size.get
-(%22c
+C
 ores
-%22
+(
 ));%0A    
@@ -2672,28 +2672,31 @@
 tEquals(
-test
+encoded
 Size.get
 Memory()
@@ -2691,33 +2691,32 @@
 .get
-M
+(%22m
 emory
-(), encoded
+%22), test
 Size.get
 (%22me
@@ -2711,25 +2711,23 @@
 Size.get
-(%22m
+M
 emory
-%22
+(
 ));%0A    
@@ -2743,28 +2743,31 @@
 tEquals(
-test
+encoded
 Size.get
 Disk(), 
@@ -2762,31 +2762,30 @@
 .get
-Disk(), encoded
+(%22disk%22), test
 Size.get
 (%22di
@@ -2780,23 +2780,21 @@
 Size.get
-(%22disk%22
+Disk(
 ));%0A    

Fix broken size test