           .send(testMetadata)
           .expect(201)
           .end (err, res) ->
-            if err
-              done err
-            else
-              res.body.errors.length.should.equal 1
-              res.body.successes.length.should.equal 4
-              done()
-                
-      it  "should fail to validate invalid metadata and return status 400 - bad request", (done) ->
+            return done err if err
+            
+            statusCheckObj = { Valid: 0, Conflict: 0, Error: 0 }
+            for row in res.body.rows
+              statusCheckObj[row.status] = statusCheckObj[row.status] + 1
+              
+            statusCheckObj.Valid.should.equal 4
+            statusCheckObj.Conflict.should.equal 0
+            statusCheckObj.Error.should.equal 1
+            done()
+      
+      
+      it  "should validate metadata with conflicts and return status 201", (done) ->
         testMetadata = {}
         testMetadata = JSON.parse JSON.stringify sampleMetadata
-        testMetadata.InvalidField = { "name": "value" }
-        request("https://localhost:8080")
-          .post("/metadata/validate")
-          .set("auth-username", testUtils.rootUser.email)
-          .set("auth-ts", authDetails.authTS)
-          .set("auth-salt", authDetails.authSalt)
-          .set("auth-token", authDetails.authToken)
-          .send(testMetadata)
-          .expect(400)
-          .end (err, res) ->
-            if err
-              done err
-            else
-              done()
+        
+        (new Channel sampleMetadata.Channels[0]).save (err, channel) ->
+          return done err if err
+          request("https://localhost:8080")
+            .post("/metadata/validate")
+            .set("auth-username", testUtils.rootUser.email)
+            .set("auth-ts", authDetails.authTS)
+            .set("auth-salt", authDetails.authSalt)
+            .set("auth-token", authDetails.authToken)
+            .send(testMetadata)
+            .expect(201)
+            .end (err, res) ->
+              return done err if err
+              
+              statusCheckObj = { Valid: 0, Conflict: 0, Error: 0 }
+              for row in res.body.rows
+                statusCheckObj[row.status] = statusCheckObj[row.status] + 1
+                
+              statusCheckObj.Valid.should.equal 4
+              statusCheckObj.Conflict.should.equal 1
+              statusCheckObj.Error.should.equal 0
+              Channel.remove ->
+                done()
+      
       
       it  "should not allow a non admin user to validate metadata", (done) ->
         request("https://localhost:8080")

describe "API Integration Tests", ->

  describe "Metadata REST Api Testing", ->

    before (done) ->
      auth.setupTestUsers (err) ->
        server.start apiPort: 8080, ->
          authDetails = auth.getAuthDetails()
          done()

    after (done) ->
      auth.cleanupTestUsers (err) ->
        User.remove ->
          Channel.remove ->
            Client.remove ->
              Mediator.remove ->
                ContactGroup.remove ->
                  server.stop ->
                    done()

    describe "*upsertMetadata", ->

      it  "should insert valid metadata and return status 201 - metadata created", (done) ->
        request("https://localhost:8080")
          .post("/metadata")
          .set("auth-username", testUtils.rootUser.email)
          .set("auth-ts", authDetails.authTS)
          .set("auth-salt", authDetails.authSalt)
          .set("auth-token", authDetails.authToken)
          .send(sampleMetadata)
          .expect(201)
          .end (err, res) ->
            if err
              done err
            else
              res.body.errors.length.should.equal 0
              res.body.successes.length.should.equal 5
              done()
      
      it  "should insert partially valid metadata and return status 201 - metadata created", (done) ->
        testMetadata = {}
        testMetadata = JSON.parse JSON.stringify sampleMetadata
        testMetadata.Channels = [{"fakeChannel"}]
        request("https://localhost:8080")
          .post("/metadata")
          .set("auth-username", testUtils.rootUser.email)
          .set("auth-ts", authDetails.authTS)
          .set("auth-salt", authDetails.authSalt)
          .set("auth-token", authDetails.authToken)
          .send(testMetadata)
          .expect(201)
          .end (err, res) ->
            if err
              done err
            else
              res.body.errors.length.should.equal 1
              res.body.successes.length.should.equal 4
              done()
                
      it  "should fail to insert invalid metadata and return status 400 - bad request", (done) ->
        testMetadata = {}
        testMetadata = JSON.parse JSON.stringify sampleMetadata
        testMetadata.InvalidField = { "name": "value" }
        request("https://localhost:8080")
          .post("/metadata")
          .set("auth-username", testUtils.rootUser.email)
          .set("auth-ts", authDetails.authTS)
          .set("auth-salt", authDetails.authSalt)
          .set("auth-token", authDetails.authToken)
          .send(testMetadata)
          .expect(400)
          .end (err, res) ->
            if err
              done err
            else
              done()
      
      it  "should not allow a non admin user to insert metadata", (done) ->
        request("https://localhost:8080")
          .post("/metadata")
          .set("auth-username", testUtils.nonRootUser.email)
          .set("auth-ts", authDetails.authTS)
          .set("auth-salt", authDetails.authSalt)
          .set("auth-token", authDetails.authToken)
          .send(sampleMetadata)
          .expect(403)
          .end (err, res) ->
            if err
              done err
            else
              done()  
      
      it  "should return 404 if not found", (done) ->
        request("https://localhost:8080")
          .post("/metadata/bleh")
          .set("auth-username", testUtils.rootUser.email)
          .set("auth-ts", authDetails.authTS)
          .set("auth-salt", authDetails.authSalt)
          .set("auth-token", authDetails.authToken)
          .send(sampleMetadata)
          .expect(404)
          .end (err, res) ->
            if err
              done err
            else
              done()


    describe "*getMetadata", ->

      it  "should fetch metadata and return status 200 - Ok", (done) ->
        request("https://localhost:8080")
          .get("/metadata")
          .set("auth-username", testUtils.rootUser.email)
          .set("auth-ts", authDetails.authTS)
          .set("auth-salt", authDetails.authSalt)
          .set("auth-token", authDetails.authToken)
          .send(sampleMetadata)
          .expect(200)
          .end (err, res) ->
            if err
              done err
            else
              res.body[0].Channels.length.should.equal 1
              res.body[0].Clients.length.should.equal 1
              res.body[0].Users.length.should.equal 4
              res.body[0].Mediators.length.should.equal 1
              res.body[0].ContactGroups.length.should.equal 1
              done()
      
      it  "should not allow a non admin user to get metadata", (done) ->
        request("https://localhost:8080")
          .get("/metadata")
          .set("auth-username", testUtils.nonRootUser.email)
          .set("auth-ts", authDetails.authTS)
          .set("auth-salt", authDetails.authSalt)
          .set("auth-token", authDetails.authToken)
          .send(sampleMetadata)
          .expect(403)
          .end (err, res) ->
            if err
              done err
            else
              done()  
      
      it  "should return 404 if not found", (done) ->
        request("https://localhost:8080")
          .get("/metadata/bleh")
          .set("auth-username", testUtils.rootUser.email)
          .set("auth-ts", authDetails.authTS)
          .set("auth-salt", authDetails.authSalt)
          .set("auth-token", authDetails.authToken)
          .send(sampleMetadata)
          .expect(404)
          .end (err, res) ->
            if err
              done err
            else
              done()      
                
    describe "*validateMetadata", ->

      it  "should validate metadata and return status 201 - metadata validated", (done) ->
        request("https://localhost:8080")
          .post("/metadata/validate")
          .set("auth-username", testUtils.rootUser.email)

          .send(sampleMetadata)
          .expect(201)
          .end (err, res) ->
            return done err if err
            
            statusCheckObj = { Valid: 0, Conflict: 0, Error: 0 }
            for row in res.body.rows
              statusCheckObj[row.status] = statusCheckObj[row.status] + 1
              
            statusCheckObj.Valid.should.equal 5
            statusCheckObj.Conflict.should.equal 0
            statusCheckObj.Error.should.equal 0
            done()
      
      
      it  "should validate partially valid metadata and return status 201", (done) ->
        testMetadata = {}
        testMetadata = JSON.parse JSON.stringify sampleMetadata
        testMetadata.Channels = [{ "Invalid Channel" }]
        
        request("https://localhost:8080")
          .post("/metadata/validate")
          .set("auth-username", testUtils.rootUser.email)
